KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.File JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.Task;
27 import org.apache.tools.ant.types.Environment;
28 import org.apache.tools.ant.types.FileSet;
29 import org.apache.tools.ant.types.Path;
30 import org.apache.tools.ant.types.RedirectorElement;
31 import org.apache.tools.ant.util.JavaEnvUtils;
32
33 /**
34  * This is factored out from {@link SignJar}; a base class that can be used
35  * for both signing and verifying JAR files using jarsigner
36  */

37
38 public abstract class AbstractJarSignerTask extends Task {
39     // CheckStyle:VisibilityModifier OFF - bc
40
/**
41      * The name of the jar file.
42      */

43     protected File JavaDoc jar;
44     /**
45      * The alias of signer.
46      */

47     protected String JavaDoc alias;
48     /**
49      * The url or path of keystore file.
50      */

51     protected String JavaDoc keystore;
52     /**
53      * password for the store
54      */

55     protected String JavaDoc storepass;
56     /**
57      * type of store,-storetype param
58      */

59     protected String JavaDoc storetype;
60     /**
61      * password for the key in the store
62      */

63     protected String JavaDoc keypass;
64     /**
65      * verbose output
66      */

67     protected boolean verbose;
68     /**
69      * The maximum amount of memory to use for Jar signer
70      */

71     protected String JavaDoc maxMemory;
72     /**
73      * the filesets of the jars to sign
74      */

75     protected Vector JavaDoc filesets = new Vector JavaDoc();
76     /**
77      * name of JDK program we are looking for
78      */

79     protected static final String JavaDoc JARSIGNER_COMMAND = "jarsigner";
80
81     // CheckStyle:VisibilityModifier ON
82

83     /**
84      * redirector used to talk to the jarsigner program
85      */

86     private RedirectorElement redirector;
87
88     /**
89      * Java declarations -J-Dname=value
90      */

91     private Environment sysProperties = new Environment();
92
93     /**
94      * error string for unit test verification: {@value}
95      */

96     public static final String JavaDoc ERROR_NO_SOURCE = "jar must be set through jar attribute "
97             + "or nested filesets";
98
99     /**
100      * Path holding all non-filesets of filesystem resources we want to sign.
101      *
102      * @since Ant 1.7
103      */

104     private Path path = null;
105
106     /**
107      * Set the maximum memory to be used by the jarsigner process
108      *
109      * @param max a string indicating the maximum memory according to the JVM
110      * conventions (e.g. 128m is 128 Megabytes)
111      */

112     public void setMaxmemory(String JavaDoc max) {
113         maxMemory = max;
114     }
115
116     /**
117      * the jar file to sign; required
118      *
119      * @param jar the jar file to sign
120      */

121     public void setJar(final File JavaDoc jar) {
122         this.jar = jar;
123     }
124
125     /**
126      * the alias to sign under; required
127      *
128      * @param alias the alias to sign under
129      */

130     public void setAlias(final String JavaDoc alias) {
131         this.alias = alias;
132     }
133
134     /**
135      * keystore location; required
136      *
137      * @param keystore the keystore location
138      */

139     public void setKeystore(final String JavaDoc keystore) {
140         this.keystore = keystore;
141     }
142
143     /**
144      * password for keystore integrity; required
145      *
146      * @param storepass the password for the keystore
147      */

148     public void setStorepass(final String JavaDoc storepass) {
149         this.storepass = storepass;
150     }
151
152     /**
153      * keystore type; optional
154      *
155      * @param storetype the keystore type
156      */

157     public void setStoretype(final String JavaDoc storetype) {
158         this.storetype = storetype;
159     }
160
161     /**
162      * password for private key (if different); optional
163      *
164      * @param keypass the password for the key (if different)
165      */

166     public void setKeypass(final String JavaDoc keypass) {
167         this.keypass = keypass;
168     }
169
170     /**
171      * Enable verbose output when signing ; optional: default false
172      *
173      * @param verbose if true enable verbose output
174      */

175     public void setVerbose(final boolean verbose) {
176         this.verbose = verbose;
177     }
178
179     /**
180      * Adds a set of files to sign
181      *
182      * @param set a set of files to sign
183      * @since Ant 1.4
184      */

185     public void addFileset(final FileSet set) {
186         filesets.addElement(set);
187     }
188
189     /**
190      * Add a system property.
191      *
192      * @param sysp system property.
193      */

194     public void addSysproperty(Environment.Variable sysp) {
195         sysProperties.addVariable(sysp);
196     }
197
198     /**
199      * Adds a path of files to sign.
200      *
201      * @return a path of files to sign.
202      * @since Ant 1.7
203      */

204     public Path createPath() {
205         if (path == null) {
206             path = new Path(getProject());
207         }
208         return path.createPath();
209     }
210
211     /**
212      * init processing logic; this is retained through our execution(s)
213      */

214     protected void beginExecution() {
215
216         redirector = createRedirector();
217     }
218
219     /**
220      * any cleanup logic
221      */

222     protected void endExecution() {
223         redirector = null;
224     }
225
226     /**
227      * Create the redirector to use, if any.
228      *
229      * @return a configured RedirectorElement.
230      */

231     private RedirectorElement createRedirector() {
232         RedirectorElement result = new RedirectorElement();
233         if (storepass != null) {
234             StringBuffer JavaDoc input = new StringBuffer JavaDoc(storepass).append('\n');
235             if (keypass != null) {
236                 input.append(keypass).append('\n');
237             }
238             result.setInputString(input.toString());
239             result.setLogInputString(false);
240         }
241         return result;
242     }
243
244     /**
245      * get the redirector. Non-null between invocations of
246      * {@link #beginExecution()} and {@link #endExecution()}
247      * @return a redirector or null
248      */

249     public RedirectorElement getRedirector() {
250         return redirector;
251     }
252
253     /**
254      * these are options common to signing and verifying
255      * @param cmd command to configure
256      */

257     protected void setCommonOptions(final ExecTask cmd) {
258         if (maxMemory != null) {
259             addValue(cmd, "-J-Xmx" + maxMemory);
260         }
261
262         if (verbose) {
263             addValue(cmd, "-verbose");
264         }
265
266         //now patch in all system properties
267
Vector JavaDoc props = sysProperties.getVariablesVector();
268         Enumeration JavaDoc e = props.elements();
269         while (e.hasMoreElements()) {
270             Environment.Variable variable = (Environment.Variable) e.nextElement();
271             declareSysProperty(cmd, variable);
272         }
273     }
274
275     /**
276      *
277      * @param cmd command to configure
278      * @param property property to set
279      * @throws BuildException if the property is not correctly defined.
280      */

281     protected void declareSysProperty(
282         ExecTask cmd, Environment.Variable property) throws BuildException {
283         addValue(cmd, "-J-D" + property.getContent());
284     }
285
286
287     /**
288      * bind to a keystore if the attributes are there
289      * @param cmd command to configure
290      */

291     protected void bindToKeystore(final ExecTask cmd) {
292         if (null != keystore) {
293             // is the keystore a file
294
addValue(cmd, "-keystore");
295             String JavaDoc loc;
296             File JavaDoc keystoreFile = getProject().resolveFile(keystore);
297             if (keystoreFile.exists()) {
298                 loc = keystoreFile.getPath();
299             } else {
300                 // must be a URL - just pass as is
301
loc = keystore;
302             }
303             addValue(cmd, loc);
304         }
305         if (null != storetype) {
306             addValue(cmd, "-storetype");
307             addValue(cmd, storetype);
308         }
309     }
310
311     /**
312      * create the jarsigner executable task
313      * @return a task set up with the executable of jarsigner, failonerror=true
314      * and bound to our redirector
315      */

316     protected ExecTask createJarSigner() {
317         final ExecTask cmd = new ExecTask(this);
318         cmd.setExecutable(JavaEnvUtils.getJdkExecutable(JARSIGNER_COMMAND));
319         cmd.setTaskType(JARSIGNER_COMMAND);
320         cmd.setFailonerror(true);
321         cmd.addConfiguredRedirector(redirector);
322         return cmd;
323     }
324
325     /**
326      * clone our filesets vector, and patch in the jar attribute as a new
327      * fileset, if is defined
328      * @return a vector of FileSet instances
329      */

330     protected Vector JavaDoc createUnifiedSources() {
331         Vector JavaDoc sources = (Vector JavaDoc) filesets.clone();
332         if (jar != null) {
333             //we create a fileset with the source file.
334
//this lets us combine our logic for handling output directories,
335
//mapping etc.
336
FileSet sourceJar = new FileSet();
337             sourceJar.setProject(getProject());
338             sourceJar.setFile(jar);
339             sourceJar.setDir(jar.getParentFile());
340             sources.add(sourceJar);
341         }
342         return sources;
343     }
344
345     /**
346      * clone our path and add all explicitly specified FileSets as
347      * well, patch in the jar attribute as a new fileset if it is
348      * defined.
349      * @return a path that contains all files to sign
350      * @since Ant 1.7
351      */

352     protected Path createUnifiedSourcePath() {
353         Path p = path == null ? new Path(getProject()) : (Path) path.clone();
354         Vector JavaDoc s = createUnifiedSources();
355         Enumeration JavaDoc e = s.elements();
356         while (e.hasMoreElements()) {
357             p.add((FileSet) e.nextElement());
358         }
359         return p;
360     }
361
362     /**
363      * Has either a path or a fileset been specified?
364      * @return true if a path or fileset has been specified.
365      * @since Ant 1.7
366      */

367     protected boolean hasResources() {
368         return path != null || filesets.size() > 0;
369     }
370
371     /**
372      * add a value argument to a command
373      * @param cmd command to manipulate
374      * @param value value to add
375      */

376     protected void addValue(final ExecTask cmd, String JavaDoc value) {
377         cmd.createArg().setValue(value);
378     }
379 }
380
Popular Tags