KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 import java.util.Enumeration JavaDoc;
21 import java.util.Vector JavaDoc;
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.Task;
24 import org.apache.tools.ant.types.Commandline;
25 import org.apache.tools.ant.util.JavaEnvUtils;
26
27 /**
28  * Generates a key in a keystore.
29  *
30  * @since Ant 1.2
31  *
32  * @ant.task name="genkey" category="java"
33  */

34 public class GenerateKey extends Task {
35
36     /**
37      * A DistinguishedName parameter.
38      * This is a nested element in a dname nested element.
39      */

40     public static class DnameParam {
41         private String JavaDoc name;
42         private String JavaDoc value;
43
44         /**
45          * Set the name attribute.
46          * @param name a <code>String</code> value
47          */

48         public void setName(String JavaDoc name) {
49             this.name = name;
50         }
51
52         /**
53          * Get the name attribute.
54          * @return the name.
55          */

56         public String JavaDoc getName() {
57             return name;
58         }
59
60         /**
61          * Set the value attribute.
62          * @param value a <code>String</code> value
63          */

64         public void setValue(String JavaDoc value) {
65             this.value = value;
66         }
67
68         /**
69          * Get the value attribute.
70          * @return the value.
71          */

72         public String JavaDoc getValue() {
73             return value;
74         }
75     }
76
77     /**
78      * A class corresponding to the dname nested element.
79      */

80     public static class DistinguishedName {
81         private Vector JavaDoc params = new Vector JavaDoc();
82
83         /**
84          * Create a param nested element.
85          * @return a DnameParam object to be configured.
86          */

87         public Object JavaDoc createParam() {
88             DnameParam param = new DnameParam();
89             params.addElement(param);
90
91             return param;
92         }
93
94         /**
95          * Get the nested parameters.
96          * @return an enumeration of the nested parameters.
97          */

98         public Enumeration JavaDoc getParams() {
99             return params.elements();
100         }
101
102         /**
103          * Generate a string rep of this distinguished name.
104          * The format is each of the parameters (name = value)
105          * separated by ','.
106          * This is used on the command line.
107          * @return a string rep of this name
108          */

109         public String JavaDoc toString() {
110             final int size = params.size();
111             final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
112             boolean firstPass = true;
113
114             for (int i = 0; i < size; i++) {
115                 if (!firstPass) {
116                     sb.append(" ,");
117                 }
118                 firstPass = false;
119
120                 final DnameParam param = (DnameParam) params.elementAt(i);
121                 sb.append(encode(param.getName()));
122                 sb.append('=');
123                 sb.append(encode(param.getValue()));
124             }
125
126             return sb.toString();
127         }
128
129         /**
130          * Encode a name or value.
131          * The encoded result is the same as the input string
132          * except that each ',' is replaced by a '\,'.
133          * @param string the value to be encoded
134          * @return the encoded value.
135          */

136         public String JavaDoc encode(final String JavaDoc string) {
137             int end = string.indexOf(',');
138
139             if (-1 == end) {
140               return string;
141             }
142
143             final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
144
145             int start = 0;
146
147             while (-1 != end) {
148                 sb.append(string.substring(start, end));
149                 sb.append("\\,");
150                 start = end + 1;
151                 end = string.indexOf(',', start);
152             }
153
154             sb.append(string.substring(start));
155
156             return sb.toString();
157         }
158     }
159
160     // CheckStyle:VisibilityModifier OFF - bc
161

162     /**
163      * The alias of signer.
164      */

165     protected String JavaDoc alias;
166
167     /**
168      * The name of keystore file.
169      */

170     protected String JavaDoc keystore;
171     protected String JavaDoc storepass;
172     protected String JavaDoc storetype;
173     protected String JavaDoc keypass;
174
175     protected String JavaDoc sigalg;
176     protected String JavaDoc keyalg;
177     protected String JavaDoc dname;
178     protected DistinguishedName expandedDname;
179     protected int keysize;
180     protected int validity;
181     protected boolean verbose;
182     // CheckStyle:VisibilityModifier ON
183

184     /**
185      * Distinguished name list.
186      *
187      * @return Distinguished name container.
188      * @throws BuildException If specified more than once or dname
189      * attribute is used.
190      */

191     public DistinguishedName createDname() throws BuildException {
192         if (null != expandedDname) {
193             throw new BuildException("DName sub-element can only be "
194                                      + "specified once.");
195         }
196         if (null != dname) {
197             throw new BuildException("It is not possible to specify dname "
198                                     + " both as attribute and element.");
199         }
200         expandedDname = new DistinguishedName();
201         return expandedDname;
202     }
203
204     /**
205      * The distinguished name for entity.
206      *
207      * @param dname distinguished name
208      */

209     public void setDname(final String JavaDoc dname) {
210         if (null != expandedDname) {
211             throw new BuildException("It is not possible to specify dname "
212                                     + " both as attribute and element.");
213         }
214         this.dname = dname;
215     }
216
217     /**
218      * The alias to add under.
219      *
220      * @param alias alias to add under
221      */

222     public void setAlias(final String JavaDoc alias) {
223         this.alias = alias;
224     }
225
226     /**
227      * Keystore location.
228      *
229      * @param keystore location
230      */

231     public void setKeystore(final String JavaDoc keystore) {
232         this.keystore = keystore;
233     }
234
235     /**
236      * Password for keystore integrity.
237      * Must be at least 6 characters long.
238      * @param storepass password
239      */

240     public void setStorepass(final String JavaDoc storepass) {
241         this.storepass = storepass;
242     }
243
244     /**
245      * Keystore type.
246      *
247      * @param storetype type
248      */

249     public void setStoretype(final String JavaDoc storetype) {
250         this.storetype = storetype;
251     }
252
253     /**
254      * Password for private key (if different).
255      *
256      * @param keypass password
257      */

258     public void setKeypass(final String JavaDoc keypass) {
259         this.keypass = keypass;
260     }
261
262     /**
263      * The algorithm to use in signing.
264      *
265      * @param sigalg algorithm
266      */

267     public void setSigalg(final String JavaDoc sigalg) {
268         this.sigalg = sigalg;
269     }
270
271     /**
272      * The method to use when generating name-value pair.
273      * @param keyalg algorithm
274      */

275     public void setKeyalg(final String JavaDoc keyalg) {
276         this.keyalg = keyalg;
277     }
278
279     /**
280      * Indicates the size of key generated.
281      *
282      * @param keysize size of key
283      * @throws BuildException If not an Integer
284      * @todo Could convert this to a plain Integer setter.
285      */

286     public void setKeysize(final String JavaDoc keysize) throws BuildException {
287         try {
288             this.keysize = Integer.parseInt(keysize);
289         } catch (final NumberFormatException JavaDoc nfe) {
290             throw new BuildException("KeySize attribute should be a integer");
291         }
292     }
293
294     /**
295      * Indicates how many days certificate is valid.
296      *
297      * @param validity days valid
298      * @throws BuildException If not an Integer
299      */

300     public void setValidity(final String JavaDoc validity) throws BuildException {
301         try {
302             this.validity = Integer.parseInt(validity);
303         } catch (final NumberFormatException JavaDoc nfe) {
304             throw new BuildException("Validity attribute should be a integer");
305         }
306     }
307
308     /**
309      * If true, verbose output when signing.
310      * @param verbose verbose or not
311      */

312     public void setVerbose(final boolean verbose) {
313         this.verbose = verbose;
314     }
315
316     /**
317      * Execute the task.
318      * @throws BuildException on error
319      */

320     public void execute() throws BuildException {
321
322         if (null == alias) {
323             throw new BuildException("alias attribute must be set");
324         }
325
326         if (null == storepass) {
327             throw new BuildException("storepass attribute must be set");
328         }
329
330         if (null == dname && null == expandedDname) {
331             throw new BuildException("dname must be set");
332         }
333
334         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
335
336         sb.append("-genkey ");
337
338         if (verbose) {
339             sb.append("-v ");
340         }
341
342         sb.append("-alias \"");
343         sb.append(alias);
344         sb.append("\" ");
345
346         if (null != dname) {
347             sb.append("-dname \"");
348             sb.append(dname);
349             sb.append("\" ");
350         }
351
352         if (null != expandedDname) {
353             sb.append("-dname \"");
354             sb.append(expandedDname);
355             sb.append("\" ");
356         }
357
358         if (null != keystore) {
359             sb.append("-keystore \"");
360             sb.append(keystore);
361             sb.append("\" ");
362         }
363
364         if (null != storepass) {
365             sb.append("-storepass \"");
366             sb.append(storepass);
367             sb.append("\" ");
368         }
369
370         if (null != storetype) {
371             sb.append("-storetype \"");
372             sb.append(storetype);
373             sb.append("\" ");
374         }
375
376         sb.append("-keypass \"");
377         if (null != keypass) {
378             sb.append(keypass);
379         } else {
380             sb.append(storepass);
381         }
382         sb.append("\" ");
383
384         if (null != sigalg) {
385             sb.append("-sigalg \"");
386             sb.append(sigalg);
387             sb.append("\" ");
388         }
389
390         if (null != keyalg) {
391             sb.append("-keyalg \"");
392             sb.append(keyalg);
393             sb.append("\" ");
394         }
395
396
397         if (0 < keysize) {
398             sb.append("-keysize \"");
399             sb.append(keysize);
400             sb.append("\" ");
401         }
402
403         if (0 < validity) {
404             sb.append("-validity \"");
405             sb.append(validity);
406             sb.append("\" ");
407         }
408
409         log("Generating Key for " + alias);
410         final ExecTask cmd = new ExecTask(this);
411         cmd.setExecutable(JavaEnvUtils.getJdkExecutable("keytool"));
412         Commandline.Argument arg = cmd.createArg();
413         arg.setLine(sb.toString());
414         cmd.setFailonerror(true);
415         cmd.setTaskName(getTaskName());
416         cmd.execute();
417     }
418 }
419
420
Popular Tags