source: release-kits/lirk3/resources/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/GenerateKey.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 9.9 KB
Line 
1/*
2 * Copyright 2000,2002,2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17package org.apache.tools.ant.taskdefs;
18
19import java.util.Enumeration;
20import java.util.Vector;
21import org.apache.tools.ant.BuildException;
22import org.apache.tools.ant.Task;
23import org.apache.tools.ant.types.Commandline;
24import org.apache.tools.ant.util.JavaEnvUtils;
25
26/**
27 * Generates a key in a keystore.
28 *
29 *
30 * @since Ant 1.2
31 *
32 * @ant.task name="genkey" category="java"
33 */
34public class GenerateKey extends Task {
35
36 public static class DnameParam {
37 private String name;
38 private String value;
39
40 public void setName(String name) {
41 this.name = name;
42 }
43
44 public String getName() {
45 return name;
46 }
47
48 public void setValue(String value) {
49 this.value = value;
50 }
51
52 public String getValue() {
53 return value;
54 }
55 }
56
57 public static class DistinguishedName {
58 private Vector params = new Vector();
59
60 public Object createParam() {
61 DnameParam param = new DnameParam();
62 params.addElement(param);
63
64 return param;
65 }
66
67 public Enumeration getParams() {
68 return params.elements();
69 }
70
71 public String toString() {
72 final int size = params.size();
73 final StringBuffer sb = new StringBuffer();
74 boolean firstPass = true;
75
76 for (int i = 0; i < size; i++) {
77 if (!firstPass) {
78 sb.append(" ,");
79 }
80 firstPass = false;
81
82 final DnameParam param = (DnameParam) params.elementAt(i);
83 sb.append(encode(param.getName()));
84 sb.append('=');
85 sb.append(encode(param.getValue()));
86 }
87
88 return sb.toString();
89 }
90
91 public String encode(final String string) {
92 int end = string.indexOf(',');
93
94 if (-1 == end) {
95 return string;
96 }
97
98 final StringBuffer sb = new StringBuffer();
99
100 int start = 0;
101
102 while (-1 != end) {
103 sb.append(string.substring(start, end));
104 sb.append("\\,");
105 start = end + 1;
106 end = string.indexOf(',', start);
107 }
108
109 sb.append(string.substring(start));
110
111 return sb.toString();
112 }
113 }
114
115 /**
116 * The alias of signer.
117 */
118 protected String alias;
119
120 /**
121 * The name of keystore file.
122 */
123 protected String keystore;
124 protected String storepass;
125 protected String storetype;
126 protected String keypass;
127
128 protected String sigalg;
129 protected String keyalg;
130 protected String dname;
131 protected DistinguishedName expandedDname;
132 protected int keysize;
133 protected int validity;
134 protected boolean verbose;
135
136 /**
137 * Distinguished name list.
138 *
139 * @return Distinguished name container.
140 * @throws BuildException If specified more than once or dname
141 * attribute is used.
142 */
143 public DistinguishedName createDname() throws BuildException {
144 if (null != expandedDname) {
145 throw new BuildException("DName sub-element can only be "
146 + "specified once.");
147 }
148 if (null != dname) {
149 throw new BuildException("It is not possible to specify dname "
150 + " both as attribute and element.");
151 }
152 expandedDname = new DistinguishedName();
153 return expandedDname;
154 }
155
156 /**
157 * The distinguished name for entity.
158 *
159 * @param dname distinguished name
160 */
161 public void setDname(final String dname) {
162 if (null != expandedDname) {
163 throw new BuildException("It is not possible to specify dname "
164 + " both as attribute and element.");
165 }
166 this.dname = dname;
167 }
168
169 /**
170 * The alias to add under.
171 *
172 * @param alias alias to add under
173 */
174 public void setAlias(final String alias) {
175 this.alias = alias;
176 }
177
178 /**
179 * Keystore location.
180 *
181 * @param keystore location
182 */
183 public void setKeystore(final String keystore) {
184 this.keystore = keystore;
185 }
186
187 /**
188 * Password for keystore integrity.
189 * Must be at least 6 characters long.
190 * @param storepass password
191 */
192 public void setStorepass(final String storepass) {
193 this.storepass = storepass;
194 }
195
196 /**
197 * Keystore type.
198 *
199 * @param storetype type
200 */
201 public void setStoretype(final String storetype) {
202 this.storetype = storetype;
203 }
204
205 /**
206 * Password for private key (if different).
207 *
208 * @param keypass password
209 */
210 public void setKeypass(final String keypass) {
211 this.keypass = keypass;
212 }
213
214 /**
215 * The algorithm to use in signing.
216 *
217 * @param sigalg algorithm
218 */
219 public void setSigalg(final String sigalg) {
220 this.sigalg = sigalg;
221 }
222
223 /**
224 * The method to use when generating name-value pair.
225 * @param keyalg algorithm
226 */
227 public void setKeyalg(final String keyalg) {
228 this.keyalg = keyalg;
229 }
230
231 /**
232 * Indicates the size of key generated.
233 *
234 * @param keysize size of key
235 * @throws BuildException If not an Integer
236 * @todo Could convert this to a plain Integer setter.
237 */
238 public void setKeysize(final String keysize) throws BuildException {
239 try {
240 this.keysize = Integer.parseInt(keysize);
241 } catch (final NumberFormatException nfe) {
242 throw new BuildException("KeySize attribute should be a integer");
243 }
244 }
245
246 /**
247 * Indicates how many days certificate is valid.
248 *
249 * @param validity days valid
250 * @throws BuildException If not an Integer
251 */
252 public void setValidity(final String validity) throws BuildException {
253 try {
254 this.validity = Integer.parseInt(validity);
255 } catch (final NumberFormatException nfe) {
256 throw new BuildException("Validity attribute should be a integer");
257 }
258 }
259
260 /**
261 * If true, verbose output when signing.
262 * @param verbose verbose or not
263 */
264 public void setVerbose(final boolean verbose) {
265 this.verbose = verbose;
266 }
267
268 public void execute() throws BuildException {
269 if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) {
270 throw new BuildException("The genkey task is only available on JDK"
271 + " versions 1.2 or greater");
272 }
273
274 if (null == alias) {
275 throw new BuildException("alias attribute must be set");
276 }
277
278 if (null == storepass) {
279 throw new BuildException("storepass attribute must be set");
280 }
281
282 if (null == dname && null == expandedDname) {
283 throw new BuildException("dname must be set");
284 }
285
286 final StringBuffer sb = new StringBuffer();
287
288 sb.append("-genkey ");
289
290 if (verbose) {
291 sb.append("-v ");
292 }
293
294 sb.append("-alias \"");
295 sb.append(alias);
296 sb.append("\" ");
297
298 if (null != dname) {
299 sb.append("-dname \"");
300 sb.append(dname);
301 sb.append("\" ");
302 }
303
304 if (null != expandedDname) {
305 sb.append("-dname \"");
306 sb.append(expandedDname);
307 sb.append("\" ");
308 }
309
310 if (null != keystore) {
311 sb.append("-keystore \"");
312 sb.append(keystore);
313 sb.append("\" ");
314 }
315
316 if (null != storepass) {
317 sb.append("-storepass \"");
318 sb.append(storepass);
319 sb.append("\" ");
320 }
321
322 if (null != storetype) {
323 sb.append("-storetype \"");
324 sb.append(storetype);
325 sb.append("\" ");
326 }
327
328 sb.append("-keypass \"");
329 if (null != keypass) {
330 sb.append(keypass);
331 } else {
332 sb.append(storepass);
333 }
334 sb.append("\" ");
335
336 if (null != sigalg) {
337 sb.append("-sigalg \"");
338 sb.append(sigalg);
339 sb.append("\" ");
340 }
341
342 if (null != keyalg) {
343 sb.append("-keyalg \"");
344 sb.append(keyalg);
345 sb.append("\" ");
346 }
347
348
349 if (0 < keysize) {
350 sb.append("-keysize \"");
351 sb.append(keysize);
352 sb.append("\" ");
353 }
354
355 if (0 < validity) {
356 sb.append("-validity \"");
357 sb.append(validity);
358 sb.append("\" ");
359 }
360
361 log("Generating Key for " + alias);
362 final ExecTask cmd = (ExecTask) getProject().createTask("exec");
363 cmd.setExecutable(JavaEnvUtils.getJdkExecutable("keytool"));
364 Commandline.Argument arg = cmd.createArg();
365 arg.setLine(sb.toString());
366 cmd.setFailonerror(true);
367 cmd.setTaskName(getTaskName());
368 cmd.execute();
369 }
370}
371
Note: See TracBrowser for help on using the repository browser.