1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import org.apache.tools.ant.Project; 22 import org.apache.tools.ant.BuildException; 23 import org.apache.tools.ant.filters.ChainableReader; 24 import org.apache.tools.ant.types.RedirectorElement; 25 import org.apache.tools.ant.types.FilterChain; 26 import org.apache.tools.ant.types.Path; 27 import org.apache.tools.ant.types.resources.FileResource; 28 29 import java.util.Iterator ; 30 import java.io.File ; 31 import java.io.Reader ; 32 import java.io.IOException ; 33 34 41 42 public class VerifyJar extends AbstractJarSignerTask { 43 46 public static final String ERROR_NO_FILE = "Not found :"; 47 48 51 private static final String VERIFIED_TEXT = "jar verified."; 52 53 56 private boolean certificates = false; 57 private BufferingOutputFilter outputCache = new BufferingOutputFilter(); 58 59 public static final String ERROR_NO_VERIFY = "Failed to verify "; 60 61 65 public void setCertificates(boolean certificates) { 66 this.certificates = certificates; 67 } 68 69 73 public void execute() throws BuildException { 74 final boolean hasJar = jar != null; 76 77 if (!hasJar && !hasResources()) { 78 throw new BuildException(ERROR_NO_SOURCE); 79 } 80 81 beginExecution(); 82 83 RedirectorElement redirector = getRedirector(); 85 redirector.setAlwaysLog(true); 86 FilterChain outputFilterChain = redirector.createOutputFilterChain(); 87 outputFilterChain.add(outputCache); 88 89 try { 90 Path sources = createUnifiedSourcePath(); 91 Iterator iter = sources.iterator(); 92 while (iter.hasNext()) { 93 FileResource fr = (FileResource) iter.next(); 94 verifyOneJar(fr.getFile()); 95 } 96 97 } finally { 98 endExecution(); 99 } 100 101 } 102 103 108 private void verifyOneJar(File jar) { 109 if (!jar.exists()) { 110 throw new BuildException(ERROR_NO_FILE + jar); 111 } 112 final ExecTask cmd = createJarSigner(); 113 114 setCommonOptions(cmd); 115 bindToKeystore(cmd); 116 117 addValue(cmd, "-verify"); 119 120 if (certificates) { 121 addValue(cmd, "-certs"); 122 } 123 124 addValue(cmd, jar.getPath()); 126 127 log("Verifying JAR: " + jar.getAbsolutePath()); 128 outputCache.clear(); 129 BuildException ex = null; 130 try { 131 cmd.execute(); 132 } catch (BuildException e) { 133 ex = e; 134 } 135 String results = outputCache.toString(); 136 if (ex != null) { 138 if (results.indexOf("zip file closed") >= 0) { 139 log("You are running " + JARSIGNER_COMMAND + " against a JVM with" 140 + " a known bug that manifests as an IllegalStateException.", 141 Project.MSG_WARN); 142 } else { 143 throw ex; 144 } 145 } 146 if (results.indexOf(VERIFIED_TEXT) < 0) { 147 throw new BuildException(ERROR_NO_VERIFY + jar); 148 } 149 } 150 151 154 private static class BufferingOutputFilter implements ChainableReader { 155 156 private BufferingOutputFilterReader buffer; 157 158 public Reader chain(Reader rdr) { 159 buffer = new BufferingOutputFilterReader(rdr); 160 return buffer; 161 } 162 163 public String toString() { 164 return buffer.toString(); 165 } 166 167 public void clear() { 168 if (buffer != null) { 169 buffer.clear(); 170 } 171 } 172 } 173 174 177 private static class BufferingOutputFilterReader extends Reader { 178 179 private Reader next; 180 181 private StringBuffer buffer = new StringBuffer (); 182 183 public BufferingOutputFilterReader(Reader next) { 184 this.next = next; 185 } 186 187 public int read(char[] cbuf, int off, int len) throws IOException { 188 int result = next.read(cbuf, off, len); 190 buffer.append(cbuf, off, len); 192 return result; 194 } 195 196 public void close() throws IOException { 197 next.close(); 198 } 199 200 public String toString() { 201 return buffer.toString(); 202 } 203 204 public void clear() { 205 buffer = new StringBuffer (); 206 } 207 } 208 } 209 | Popular Tags |