1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import org.apache.tools.ant.BuildException; 26 import org.apache.tools.ant.Task; 27 import org.apache.tools.ant.types.Resource; 28 import org.apache.tools.ant.types.ResourceCollection; 29 import org.apache.tools.ant.types.resources.FileResource; 30 31 36 37 public abstract class Pack extends Task { 38 39 protected File zipFile; 41 protected File source; 42 private Resource src; 44 45 49 public void setZipfile(File zipFile) { 50 this.zipFile = zipFile; 51 } 52 53 57 public void setDestfile(File zipFile) { 58 setZipfile(zipFile); 59 } 60 61 65 public void setSrc(File src) { 66 setSrcResource(new FileResource(src)); 67 } 68 69 73 public void setSrcResource(Resource src) { 74 if (src.isDirectory()) { 75 throw new BuildException("the source can't be a directory"); 76 } 77 if (src instanceof FileResource) { 78 source = ((FileResource) src).getFile(); 79 } else if (!supportsNonFileResources()) { 80 throw new BuildException("Only FileSystem resources are" 81 + " supported."); 82 } 83 this.src = src; 84 } 85 86 90 public void addConfigured(ResourceCollection a) { 91 if (a.size() != 1) { 92 throw new BuildException("only single argument resource collections" 93 + " are supported as archives"); 94 } 95 setSrcResource((Resource) a.iterator().next()); 96 } 97 98 102 private void validate() throws BuildException { 103 if (zipFile == null) { 104 throw new BuildException("zipfile attribute is required", getLocation()); 105 } 106 107 if (zipFile.isDirectory()) { 108 throw new BuildException("zipfile attribute must not " 109 + "represent a directory!", getLocation()); 110 } 111 112 if (getSrcResource() == null) { 113 throw new BuildException("src attribute or nested resource is" 114 + " required", getLocation()); 115 } 116 } 117 118 122 public void execute() throws BuildException { 123 validate(); 124 125 Resource s = getSrcResource(); 126 if (!s.isExists()) { 127 log("Nothing to do: " + s.toString() 128 + " doesn't exist."); 129 } else if (zipFile.lastModified() < s.getLastModified()) { 130 log("Building: " + zipFile.getAbsolutePath()); 131 pack(); 132 } else { 133 log("Nothing to do: " + zipFile.getAbsolutePath() 134 + " is up to date."); 135 } 136 } 137 138 144 private void zipFile(InputStream in, OutputStream zOut) 145 throws IOException { 146 byte[] buffer = new byte[8 * 1024]; 147 int count = 0; 148 do { 149 zOut.write(buffer, 0, count); 150 count = in.read(buffer, 0, buffer.length); 151 } while (count != -1); 152 } 153 154 160 protected void zipFile(File file, OutputStream zOut) 161 throws IOException { 162 zipResource(new FileResource(file), zOut); 163 } 164 165 171 protected void zipResource(Resource resource, OutputStream zOut) 172 throws IOException { 173 InputStream rIn = resource.getInputStream(); 174 try { 175 zipFile(rIn, zOut); 176 } finally { 177 rIn.close(); 178 } 179 } 180 181 184 protected abstract void pack(); 185 186 191 public Resource getSrcResource() { 192 return src; 193 } 194 195 202 protected boolean supportsNonFileResources() { 203 return false; 204 } 205 } 206 | Popular Tags |