Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 412   Methods: 21
NCLOC: 222   Classes: 3
 
 Source file Conditionals Statements Methods TOTAL
GenerateKey.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000,2002 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 package org.apache.tools.ant.taskdefs;
 55   
 
 56   
 import java.util.Enumeration;
 57   
 import java.util.Vector;
 58   
 import org.apache.tools.ant.BuildException;
 59   
 import org.apache.tools.ant.Task;
 60   
 import org.apache.tools.ant.types.Commandline;
 61   
 import org.apache.tools.ant.util.JavaEnvUtils;
 62   
 
 63   
 /**
 64   
  * Generates a key in a keystore.
 65   
  * 
 66   
  * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
 67   
  *
 68   
  * @since Ant 1.2
 69   
  *
 70   
  * @ant.task name="genkey" category="java"
 71   
  */
 72   
 public class GenerateKey extends Task {
 73   
 
 74   
     public static class DnameParam {
 75   
         private String name;
 76   
         private String value;
 77   
         
 78  0
         public void setName(String name) {
 79  0
             this.name = name;
 80   
         }
 81   
         
 82  0
         public String getName() {
 83  0
             return name;
 84   
         }
 85   
         
 86  0
         public void setValue(String value) {
 87  0
             this.value = value;
 88   
         }
 89   
 
 90  0
         public String getValue() {
 91  0
             return value;
 92   
         }
 93   
     }
 94   
 
 95   
     public static class DistinguishedName {
 96   
         private Vector params = new Vector();
 97   
         
 98  0
         public Object createParam() {
 99  0
             DnameParam param = new DnameParam();
 100  0
             params.addElement(param);
 101   
             
 102  0
             return param;
 103   
         }
 104   
         
 105  0
         public Enumeration getParams() {
 106  0
             return params.elements();
 107   
         }
 108   
 
 109  0
         public String toString() {
 110  0
             final int size = params.size();
 111  0
             final StringBuffer sb = new StringBuffer();
 112  0
             boolean firstPass = true;
 113   
 
 114  0
             for (int i = 0; i < size; i++) {
 115  0
                 if (!firstPass) {
 116  0
                     sb.append(" ,");
 117   
                 }
 118  0
                 firstPass = false;
 119   
 
 120  0
                 final DnameParam param = (DnameParam) params.elementAt(i);
 121  0
                 sb.append(encode(param.getName()));
 122  0
                 sb.append('=');
 123  0
                 sb.append(encode(param.getValue()));
 124   
             }
 125   
                         
 126  0
             return sb.toString();
 127   
         }
 128   
 
 129  0
         public String encode(final String string) {
 130  0
             int end = string.indexOf(',');
 131   
 
 132  0
             if (-1 == end) {
 133  0
               return string;
 134   
             }
 135   
                 
 136  0
             final StringBuffer sb = new StringBuffer();
 137   
                 
 138  0
             int start = 0;
 139   
 
 140  0
             while (-1 != end) {
 141  0
                 sb.append(string.substring(start, end));
 142  0
                 sb.append("\\,");
 143  0
                 start = end + 1;
 144  0
                 end = string.indexOf(',', start);
 145   
             }
 146   
 
 147  0
             sb.append(string.substring(start));
 148   
                 
 149  0
             return sb.toString();                
 150   
         }
 151   
     }
 152   
 
 153   
     /**
 154   
      * The alias of signer.
 155   
      */
 156   
     protected String alias;
 157   
 
 158   
     /**
 159   
      * The name of keystore file.
 160   
      */
 161   
     protected String keystore;
 162   
     protected String storepass;
 163   
     protected String storetype;
 164   
     protected String keypass;
 165   
 
 166   
     protected String sigalg;
 167   
     protected String keyalg;
 168   
     protected String dname;
 169   
     protected DistinguishedName expandedDname;
 170   
     protected int keysize;
 171   
     protected int validity;
 172   
     protected boolean verbose;
 173   
 
 174   
     /**
 175   
      * Distinguished name list.
 176   
      *
 177   
      * @return Distinguished name container.
 178   
      * @throws BuildException If specified more than once or dname
 179   
      *                        attribute is used.
 180   
      */
 181  0
     public DistinguishedName createDname() throws BuildException {
 182  0
         if (null != expandedDname) {
 183  0
             throw new BuildException("DName sub-element can only be "
 184   
                                      + "specified once.");
 185   
         }
 186  0
         if (null != dname) {
 187  0
             throw new BuildException("It is not possible to specify dname " +
 188   
                                      " both " +
 189   
                                      "as attribute and element.");
 190   
         }
 191  0
         expandedDname = new DistinguishedName();
 192  0
         return expandedDname;
 193   
     }
 194   
 
 195   
     /**
 196   
      * The distinguished name for entity.
 197   
      *
 198   
      * @param dname distinguished name
 199   
      */
 200  0
     public void setDname(final String dname) {
 201  0
         if (null != expandedDname) {
 202  0
             throw new BuildException("It is not possible to specify dname " +
 203   
                                      " both " +
 204   
                                      "as attribute and element.");
 205   
         }
 206  0
         this.dname = dname;
 207   
     }
 208   
 
 209   
     /**
 210   
      * The alias to add under.
 211   
      *
 212   
      * @param alias alias to add under
 213   
      */
 214  0
     public void setAlias(final String alias) {
 215  0
         this.alias = alias;
 216   
     }
 217   
 
 218   
     /**
 219   
      * Keystore location.
 220   
      *
 221   
      * @param keystore location
 222   
      */
 223  0
     public void setKeystore(final String keystore) {
 224  0
         this.keystore = keystore;
 225   
     }
 226   
 
 227   
     /**
 228   
      * Password for keystore integrity.
 229   
      * Must be at least 6 characters long.
 230   
      * @param storepass password
 231   
      */
 232  0
     public void setStorepass(final String storepass) {
 233  0
         this.storepass = storepass;
 234   
     }
 235   
 
 236   
     /**
 237   
      * Keystore type.
 238   
      *
 239   
      * @param storetype type
 240   
      */
 241  0
     public void setStoretype(final String storetype) {
 242  0
         this.storetype = storetype;
 243   
     }
 244   
 
 245   
     /**
 246   
      * Password for private key (if different).
 247   
      *
 248   
      * @param keypass password
 249   
      */
 250  0
     public void setKeypass(final String keypass) {
 251  0
         this.keypass = keypass;
 252   
     }
 253   
 
 254   
     /**
 255   
      * The algorithm to use in signing.
 256   
      *
 257   
      * @param sigalg algorithm
 258   
      */
 259  0
     public void setSigalg(final String sigalg) {
 260  0
         this.sigalg = sigalg;
 261   
     }
 262   
 
 263   
     /**
 264   
      * The method to use when generating name-value pair.
 265   
      * @param keyalg algorithm
 266   
      */
 267  0
     public void setKeyalg(final String keyalg) {
 268  0
         this.keyalg = keyalg;
 269   
     }
 270   
 
 271   
     /**
 272   
      * Indicates the size of key generated.
 273   
      *
 274   
      * @param keysize size of key
 275   
      * @throws BuildException If not an Integer
 276   
      * @todo Could convert this to a plain Integer setter.
 277   
      */
 278  0
     public void setKeysize(final String keysize) throws BuildException {
 279  0
         try { 
 280  0
             this.keysize = Integer.parseInt(keysize); 
 281   
         } catch (final NumberFormatException nfe) {
 282  0
             throw new BuildException("KeySize attribute should be a integer");
 283   
         }
 284   
     }
 285   
 
 286   
     /**
 287   
      * Indicates how many days certificate is valid.
 288   
      *
 289   
      * @param validity days valid
 290   
      * @throws BuildException If not an Integer
 291   
      */
 292  0
     public void setValidity(final String validity) throws BuildException {
 293  0
         try { 
 294  0
             this.validity = Integer.parseInt(validity); 
 295   
         } catch (final NumberFormatException nfe) {
 296  0
             throw new BuildException("Validity attribute should be a integer");
 297   
         }
 298   
     }
 299   
 
 300   
     /**
 301   
      * If true, verbose output when signing.
 302   
      * @param verbose verbose or not
 303   
      */
 304  0
     public void setVerbose(final boolean verbose) {
 305  0
         this.verbose = verbose;
 306   
     } 
 307   
 
 308  0
     public void execute() throws BuildException {
 309  0
         if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) {
 310  0
             throw new BuildException("The genkey task is only available on JDK"
 311   
                                      + " versions 1.2 or greater");
 312   
         }
 313   
 
 314  0
         if (null == alias) {
 315  0
             throw new BuildException("alias attribute must be set");
 316   
         } 
 317   
 
 318  0
         if (null == storepass) {
 319  0
             throw new BuildException("storepass attribute must be set");
 320   
         } 
 321   
 
 322  0
         if (null == dname && null == expandedDname) {
 323  0
             throw new BuildException("dname must be set");
 324   
         } 
 325   
 
 326  0
         final StringBuffer sb = new StringBuffer();
 327   
 
 328  0
         sb.append("-genkey ");
 329   
 
 330  0
         if (verbose) {
 331  0
             sb.append("-v ");
 332   
         } 
 333   
 
 334  0
         sb.append("-alias \"");
 335  0
         sb.append(alias);
 336  0
         sb.append("\" ");
 337   
 
 338  0
         if (null != dname) {
 339  0
             sb.append("-dname \"");
 340  0
             sb.append(dname);
 341  0
             sb.append("\" ");
 342   
         } 
 343   
 
 344  0
         if (null != expandedDname) {
 345  0
             sb.append("-dname \"");
 346  0
             sb.append(expandedDname);
 347  0
             sb.append("\" ");
 348   
         } 
 349   
 
 350  0
         if (null != keystore) {
 351  0
             sb.append("-keystore \"");
 352  0
             sb.append(keystore);
 353  0
             sb.append("\" ");
 354   
         }
 355   
 
 356  0
         if (null != storepass) {
 357  0
             sb.append("-storepass \"");
 358  0
             sb.append(storepass);
 359  0
             sb.append("\" ");
 360   
         } 
 361   
 
 362  0
         if (null != storetype) {
 363  0
             sb.append("-storetype \"");
 364  0
             sb.append(storetype);
 365  0
             sb.append("\" ");
 366   
         } 
 367   
 
 368  0
         sb.append("-keypass \"");
 369  0
         if (null != keypass) {
 370  0
             sb.append(keypass);
 371   
         } else {
 372  0
             sb.append(storepass);
 373   
         }
 374  0
         sb.append("\" ");
 375   
 
 376  0
         if (null != sigalg) {
 377  0
             sb.append("-sigalg \"");
 378  0
             sb.append(sigalg);
 379  0
             sb.append("\" ");
 380   
         } 
 381   
 
 382  0
         if (null != keyalg) {
 383  0
             sb.append("-keyalg \"");
 384  0
             sb.append(keyalg);
 385  0
             sb.append("\" ");
 386   
         } 
 387   
 
 388   
 
 389  0
         if (0 < keysize) {
 390  0
             sb.append("-keysize \"");
 391  0
             sb.append(keysize);
 392  0
             sb.append("\" ");
 393   
         } 
 394   
 
 395  0
         if (0 < validity) {
 396  0
             sb.append("-validity \"");
 397  0
             sb.append(validity);
 398  0
             sb.append("\" ");
 399   
         } 
 400   
 
 401  0
         log("Generating Key for " + alias);
 402  0
         final ExecTask cmd = (ExecTask) getProject().createTask("exec");
 403  0
         cmd.setExecutable("keytool");
 404  0
         Commandline.Argument arg = cmd.createArg();
 405  0
         arg.setLine(sb.toString());
 406  0
         cmd.setFailonerror(true);
 407  0
         cmd.setTaskName(getTaskName());
 408  0
         cmd.execute();
 409   
     } 
 410   
 }
 411   
 
 412