You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ManifestWriter.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.buildhelpers;
  17. import java.io.BufferedReader;
  18. import java.io.File;
  19. import java.io.FileInputStream;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.InputStreamReader;
  24. import java.util.Date;
  25. import java.util.jar.Manifest;
  26. public class ManifestWriter {
  27. StringBuffer buffer = new StringBuffer();
  28. public ManifestWriter() {
  29. }
  30. /**
  31. * Writes a manifest attribute to a temporary buffer.
  32. *
  33. * @param name
  34. * Attribute name
  35. * @param value
  36. * Attribute value
  37. */
  38. public void writeAttribute(String name, String value) {
  39. int linelen = name.length() + 2;
  40. buffer.append(name);
  41. buffer.append(": ");
  42. String remainingValue = value;
  43. while (linelen + remainingValue.length() > 72) {
  44. int fitsLine = 72 - linelen;
  45. buffer.append(remainingValue.substring(0, fitsLine) + "\n ");
  46. remainingValue = remainingValue.substring(fitsLine);
  47. linelen = 1;
  48. }
  49. buffer.append(remainingValue + "\n");
  50. }
  51. /**
  52. * Writes the manifest to given JAR file.
  53. *
  54. * The manifest must be created with {@code #writeAttribute(String, String)}
  55. * before calling this write.
  56. *
  57. * @param jarFilename
  58. * File name of the JAR in which the manifest is written
  59. * @return 0 on success, nonzero value on error
  60. */
  61. int updateJar(String jarFilename) {
  62. int status = 0;
  63. // Determine a temporary file name
  64. String newMfPrefix = "vaadin-manifest-" + (new Date()).getTime();
  65. File newMfFile = null;
  66. try {
  67. newMfFile = File.createTempFile(newMfPrefix, ".mf");
  68. } catch (IOException e) {
  69. System.err.println("Creating temp file failed");
  70. status = 1;
  71. }
  72. // Write the manifest to the temporary file
  73. if (status == 0) {
  74. FileOutputStream fos = null;
  75. try {
  76. fos = new FileOutputStream(newMfFile);
  77. fos.write(getBytes());
  78. fos.close();
  79. } catch (IOException e) {
  80. System.err.println(
  81. "Writing to file '" + newMfFile.getAbsolutePath()
  82. + "' failed because: " + e.getMessage());
  83. status = 1;
  84. }
  85. }
  86. // Check that the manifest is OK
  87. if (status == 0) {
  88. Manifest checkMf = new Manifest();
  89. FileInputStream is;
  90. try {
  91. is = new FileInputStream(newMfFile);
  92. checkMf.read(is);
  93. } catch (IOException e) {
  94. System.err.println(
  95. "Reading from file '" + newMfFile.getAbsolutePath()
  96. + "' failed because: " + e.getMessage());
  97. status = 1;
  98. }
  99. }
  100. // Update the manifest in the Jar
  101. if (status == 0) {
  102. System.out.println("Updating manifest in JAR " + jarFilename);
  103. try {
  104. // The "mf" order must correspond with manifest-jarfile order
  105. Process process = Runtime.getRuntime()
  106. .exec(new String[] { "jar", "umf",
  107. newMfFile.getAbsolutePath(), jarFilename });
  108. int exitValue = process.waitFor();
  109. if (exitValue != 0) {
  110. InputStream jarErr = process.getErrorStream();
  111. BufferedReader reader = new BufferedReader(
  112. new InputStreamReader(jarErr));
  113. while (reader.ready()) {
  114. System.err.println("jar: " + reader.readLine());
  115. }
  116. System.err.println(
  117. "The 'jar' command returned with exit value "
  118. + exitValue);
  119. status = 1;
  120. }
  121. } catch (IOException e) {
  122. System.err.println(
  123. "Failed to execute 'jar' command. " + e.getMessage());
  124. status = 1;
  125. } catch (InterruptedException e) {
  126. System.err
  127. .println("Execution of 'jar' command was interrupted. "
  128. + e.getMessage());
  129. status = 1;
  130. }
  131. }
  132. // Remove the temporary file
  133. if (newMfFile != null) {
  134. newMfFile.delete();
  135. }
  136. return status;
  137. }
  138. @Override
  139. public String toString() {
  140. return buffer.toString();
  141. }
  142. public byte[] getBytes() {
  143. return buffer.toString().getBytes();
  144. }
  145. }