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

Pack.java 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant.taskdefs;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.OutputStream;
  23. import org.apache.tools.ant.BuildException;
  24. import org.apache.tools.ant.Task;
  25. import org.apache.tools.ant.types.Resource;
  26. import org.apache.tools.ant.types.ResourceCollection;
  27. import org.apache.tools.ant.types.resources.FileResource;
  28. /**
  29. * Abstract Base class for pack tasks.
  30. *
  31. * @since Ant 1.5
  32. */
  33. public abstract class Pack extends Task {
  34. protected File zipFile;
  35. protected File source;
  36. private Resource src;
  37. /**
  38. * the required destination file.
  39. * @param zipFile the destination file
  40. */
  41. public void setZipfile(File zipFile) {
  42. this.zipFile = zipFile;
  43. }
  44. /**
  45. * the required destination file.
  46. * @param zipFile the destination file
  47. */
  48. public void setDestfile(File zipFile) {
  49. setZipfile(zipFile);
  50. }
  51. /**
  52. * the file to compress; required.
  53. * @param src the source file
  54. */
  55. public void setSrc(File src) {
  56. setSrcResource(new FileResource(src));
  57. }
  58. /**
  59. * The resource to pack; required.
  60. * @param src resource to expand
  61. */
  62. public void setSrcResource(Resource src) {
  63. if (src.isDirectory()) {
  64. throw new BuildException("the source can't be a directory");
  65. }
  66. if (src instanceof FileResource) {
  67. source = ((FileResource) src).getFile();
  68. } else if (!supportsNonFileResources()) {
  69. throw new BuildException("Only FileSystem resources are"
  70. + " supported.");
  71. }
  72. this.src = src;
  73. }
  74. /**
  75. * Set the source resource.
  76. * @param a the resource to pack as a single element Resource collection.
  77. */
  78. public void addConfigured(ResourceCollection a) {
  79. if (a.size() != 1) {
  80. throw new BuildException("only single argument resource collections"
  81. + " are supported as archives");
  82. }
  83. setSrcResource((Resource) a.iterator().next());
  84. }
  85. /**
  86. * validation routine
  87. * @throws BuildException if anything is invalid
  88. */
  89. private void validate() throws BuildException {
  90. if (zipFile == null) {
  91. throw new BuildException("zipfile attribute is required", getLocation());
  92. }
  93. if (zipFile.isDirectory()) {
  94. throw new BuildException("zipfile attribute must not "
  95. + "represent a directory!", getLocation());
  96. }
  97. if (getSrcResource() == null) {
  98. throw new BuildException("src attribute or nested resource is"
  99. + " required", getLocation());
  100. }
  101. }
  102. /**
  103. * validate, then hand off to the subclass
  104. * @throws BuildException on error
  105. */
  106. public void execute() throws BuildException {
  107. validate();
  108. Resource s = getSrcResource();
  109. if (!s.isExists()) {
  110. log("Nothing to do: " + s.toString()
  111. + " doesn't exist.");
  112. } else if (zipFile.lastModified() < s.getLastModified()) {
  113. log("Building: " + zipFile.getAbsolutePath());
  114. pack();
  115. } else {
  116. log("Nothing to do: " + zipFile.getAbsolutePath()
  117. + " is up to date.");
  118. }
  119. }
  120. /**
  121. * zip a stream to an output stream
  122. * @param in the stream to zip
  123. * @param zOut the output stream
  124. * @throws IOException
  125. */
  126. private void zipFile(InputStream in, OutputStream zOut)
  127. throws IOException {
  128. byte[] buffer = new byte[8 * 1024];
  129. int count = 0;
  130. do {
  131. zOut.write(buffer, 0, count);
  132. count = in.read(buffer, 0, buffer.length);
  133. } while (count != -1);
  134. }
  135. /**
  136. * zip a file to an output stream
  137. * @param file the file to zip
  138. * @param zOut the output stream
  139. * @throws IOException on error
  140. */
  141. protected void zipFile(File file, OutputStream zOut)
  142. throws IOException {
  143. zipResource(new FileResource(file), zOut);
  144. }
  145. /**
  146. * zip a resource to an output stream
  147. * @param resource the resource to zip
  148. * @param zOut the output stream
  149. * @throws IOException on error
  150. */
  151. protected void zipResource(Resource resource, OutputStream zOut)
  152. throws IOException {
  153. InputStream rIn = resource.getInputStream();
  154. try {
  155. zipFile(rIn, zOut);
  156. } finally {
  157. rIn.close();
  158. }
  159. }
  160. /**
  161. * subclasses must implement this method to do their compression
  162. */
  163. protected abstract void pack();
  164. /**
  165. * The source resource.
  166. * @return the source.
  167. * @since Ant 1.7
  168. */
  169. public Resource getSrcResource() {
  170. return src;
  171. }
  172. /**
  173. * Whether this task can deal with non-file resources.
  174. *
  175. * <p>This implementation returns false.</p>
  176. * @return false.
  177. * @since Ant 1.7
  178. */
  179. protected boolean supportsNonFileResources() {
  180. return false;
  181. }
  182. }