1 18 package org.apache.tools.ant.taskdefs; 19 20 import java.util.Enumeration ; 21 import java.util.Vector ; 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 34 public class GenerateKey extends Task { 35 36 40 public static class DnameParam { 41 private String name; 42 private String value; 43 44 48 public void setName(String name) { 49 this.name = name; 50 } 51 52 56 public String getName() { 57 return name; 58 } 59 60 64 public void setValue(String value) { 65 this.value = value; 66 } 67 68 72 public String getValue() { 73 return value; 74 } 75 } 76 77 80 public static class DistinguishedName { 81 private Vector params = new Vector (); 82 83 87 public Object createParam() { 88 DnameParam param = new DnameParam(); 89 params.addElement(param); 90 91 return param; 92 } 93 94 98 public Enumeration getParams() { 99 return params.elements(); 100 } 101 102 109 public String toString() { 110 final int size = params.size(); 111 final StringBuffer sb = new StringBuffer (); 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 136 public String encode(final String string) { 137 int end = string.indexOf(','); 138 139 if (-1 == end) { 140 return string; 141 } 142 143 final StringBuffer sb = new StringBuffer (); 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 162 165 protected String alias; 166 167 170 protected String keystore; 171 protected String storepass; 172 protected String storetype; 173 protected String keypass; 174 175 protected String sigalg; 176 protected String keyalg; 177 protected String dname; 178 protected DistinguishedName expandedDname; 179 protected int keysize; 180 protected int validity; 181 protected boolean verbose; 182 184 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 209 public void setDname(final String 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 222 public void setAlias(final String alias) { 223 this.alias = alias; 224 } 225 226 231 public void setKeystore(final String keystore) { 232 this.keystore = keystore; 233 } 234 235 240 public void setStorepass(final String storepass) { 241 this.storepass = storepass; 242 } 243 244 249 public void setStoretype(final String storetype) { 250 this.storetype = storetype; 251 } 252 253 258 public void setKeypass(final String keypass) { 259 this.keypass = keypass; 260 } 261 262 267 public void setSigalg(final String sigalg) { 268 this.sigalg = sigalg; 269 } 270 271 275 public void setKeyalg(final String keyalg) { 276 this.keyalg = keyalg; 277 } 278 279 286 public void setKeysize(final String keysize) throws BuildException { 287 try { 288 this.keysize = Integer.parseInt(keysize); 289 } catch (final NumberFormatException nfe) { 290 throw new BuildException("KeySize attribute should be a integer"); 291 } 292 } 293 294 300 public void setValidity(final String validity) throws BuildException { 301 try { 302 this.validity = Integer.parseInt(validity); 303 } catch (final NumberFormatException nfe) { 304 throw new BuildException("Validity attribute should be a integer"); 305 } 306 } 307 308 312 public void setVerbose(final boolean verbose) { 313 this.verbose = verbose; 314 } 315 316 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 sb = new StringBuffer (); 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 |