KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > VerifyJar


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
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 JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.Reader JavaDoc;
32 import java.io.IOException JavaDoc;
33
34 /**
35  * JAR verification task.
36  * For every JAR passed in, we fork jarsigner to verify
37  * that it is correctly signed. This is more rigorous than just checking for
38  * the existence of a signature; the entire certification chain is tested
39  * @since Ant 1.7
40  */

41
42 public class VerifyJar extends AbstractJarSignerTask {
43     /**
44      * no file message {@value}
45      */

46     public static final String JavaDoc ERROR_NO_FILE = "Not found :";
47
48     /**
49      * The string we look for in the text to indicate direct verification
50      */

51     private static final String JavaDoc VERIFIED_TEXT = "jar verified.";
52
53     /**
54      * certification flag
55      */

56     private boolean certificates = false;
57     private BufferingOutputFilter outputCache = new BufferingOutputFilter();
58     /** Error output if there is a failure to verify the jar. */
59     public static final String JavaDoc ERROR_NO_VERIFY = "Failed to verify ";
60
61     /**
62      * Ask for certificate information to be printed
63      * @param certificates if true print certificates.
64      */

65     public void setCertificates(boolean certificates) {
66         this.certificates = certificates;
67     }
68
69     /**
70      * verify our jar files
71      * @throws BuildException on error.
72      */

73     public void execute() throws BuildException {
74         //validation logic
75
final boolean hasJar = jar != null;
76
77         if (!hasJar && !hasResources()) {
78             throw new BuildException(ERROR_NO_SOURCE);
79         }
80
81         beginExecution();
82
83         //patch the redirector to save output to a file
84
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 JavaDoc 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     /**
104      * verify a JAR.
105      * @param jar the jar to verify.
106      * @throws BuildException if the file could not be verified
107      */

108     private void verifyOneJar(File JavaDoc 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         //verify special operations
118
addValue(cmd, "-verify");
119
120         if (certificates) {
121             addValue(cmd, "-certs");
122         }
123
124         //JAR is required
125
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 JavaDoc results = outputCache.toString();
136         //deal with jdk1.4.2 bug:
137
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     /**
152      * we are not thread safe here. Do not use on multiple threads at the same time.
153      */

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 JavaDoc toString() {
164             return buffer.toString();
165         }
166
167         public void clear() {
168             if (buffer != null) {
169                 buffer.clear();
170             }
171         }
172     }
173
174     /**
175      * catch the output of the buffer
176      */

177     private static class BufferingOutputFilterReader extends Reader {
178
179         private Reader next;
180
181         private StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
182
183         public BufferingOutputFilterReader(Reader next) {
184             this.next = next;
185         }
186
187         public int read(char[] cbuf, int off, int len) throws IOException JavaDoc {
188             //hand down
189
int result = next.read(cbuf, off, len);
190             //cache
191
buffer.append(cbuf, off, len);
192             //return
193
return result;
194         }
195
196         public void close() throws IOException JavaDoc {
197             next.close();
198         }
199
200         public String JavaDoc toString() {
201             return buffer.toString();
202         }
203
204         public void clear() {
205             buffer = new StringBuffer JavaDoc();
206         }
207     }
208 }
209
Popular Tags