FileDocCategorySizeDatePackage
GenerateKey.javaAPI DocApache Ant 1.7011218Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.taskdefs

GenerateKey

public class GenerateKey extends org.apache.tools.ant.Task
Generates a key in a keystore.
since
Ant 1.2
ant.task
name="genkey" category="java"

Fields Summary
protected String
alias
The alias of signer.
protected String
keystore
The name of keystore file.
protected String
storepass
protected String
storetype
protected String
keypass
protected String
sigalg
protected String
keyalg
protected String
dname
protected DistinguishedName
expandedDname
protected int
keysize
protected int
validity
protected boolean
verbose
Constructors Summary
Methods Summary
public org.apache.tools.ant.taskdefs.GenerateKey$DistinguishedNamecreateDname()
Distinguished name list.

return
Distinguished name container.
throws
BuildException If specified more than once or dname attribute is used.

        if (null != expandedDname) {
            throw new BuildException("DName sub-element can only be "
                                     + "specified once.");
        }
        if (null != dname) {
            throw new BuildException("It is not possible to specify dname "
                                    + " both as attribute and element.");
        }
        expandedDname = new DistinguishedName();
        return expandedDname;
    
public voidexecute()
Execute the task.

throws
BuildException on error


        if (null == alias) {
            throw new BuildException("alias attribute must be set");
        }

        if (null == storepass) {
            throw new BuildException("storepass attribute must be set");
        }

        if (null == dname && null == expandedDname) {
            throw new BuildException("dname must be set");
        }

        final StringBuffer sb = new StringBuffer();

        sb.append("-genkey ");

        if (verbose) {
            sb.append("-v ");
        }

        sb.append("-alias \"");
        sb.append(alias);
        sb.append("\" ");

        if (null != dname) {
            sb.append("-dname \"");
            sb.append(dname);
            sb.append("\" ");
        }

        if (null != expandedDname) {
            sb.append("-dname \"");
            sb.append(expandedDname);
            sb.append("\" ");
        }

        if (null != keystore) {
            sb.append("-keystore \"");
            sb.append(keystore);
            sb.append("\" ");
        }

        if (null != storepass) {
            sb.append("-storepass \"");
            sb.append(storepass);
            sb.append("\" ");
        }

        if (null != storetype) {
            sb.append("-storetype \"");
            sb.append(storetype);
            sb.append("\" ");
        }

        sb.append("-keypass \"");
        if (null != keypass) {
            sb.append(keypass);
        } else {
            sb.append(storepass);
        }
        sb.append("\" ");

        if (null != sigalg) {
            sb.append("-sigalg \"");
            sb.append(sigalg);
            sb.append("\" ");
        }

        if (null != keyalg) {
            sb.append("-keyalg \"");
            sb.append(keyalg);
            sb.append("\" ");
        }


        if (0 < keysize) {
            sb.append("-keysize \"");
            sb.append(keysize);
            sb.append("\" ");
        }

        if (0 < validity) {
            sb.append("-validity \"");
            sb.append(validity);
            sb.append("\" ");
        }

        log("Generating Key for " + alias);
        final ExecTask cmd = new ExecTask(this);
        cmd.setExecutable(JavaEnvUtils.getJdkExecutable("keytool"));
        Commandline.Argument arg = cmd.createArg();
        arg.setLine(sb.toString());
        cmd.setFailonerror(true);
        cmd.setTaskName(getTaskName());
        cmd.execute();
    
public voidsetAlias(java.lang.String alias)
The alias to add under.

param
alias alias to add under

        this.alias = alias;
    
public voidsetDname(java.lang.String dname)
The distinguished name for entity.

param
dname distinguished name

        if (null != expandedDname) {
            throw new BuildException("It is not possible to specify dname "
                                    + " both as attribute and element.");
        }
        this.dname = dname;
    
public voidsetKeyalg(java.lang.String keyalg)
The method to use when generating name-value pair.

param
keyalg algorithm

        this.keyalg = keyalg;
    
public voidsetKeypass(java.lang.String keypass)
Password for private key (if different).

param
keypass password

        this.keypass = keypass;
    
public voidsetKeysize(java.lang.String keysize)
Indicates the size of key generated.

param
keysize size of key
throws
BuildException If not an Integer
todo
Could convert this to a plain Integer setter.

        try {
            this.keysize = Integer.parseInt(keysize);
        } catch (final NumberFormatException nfe) {
            throw new BuildException("KeySize attribute should be a integer");
        }
    
public voidsetKeystore(java.lang.String keystore)
Keystore location.

param
keystore location

        this.keystore = keystore;
    
public voidsetSigalg(java.lang.String sigalg)
The algorithm to use in signing.

param
sigalg algorithm

        this.sigalg = sigalg;
    
public voidsetStorepass(java.lang.String storepass)
Password for keystore integrity. Must be at least 6 characters long.

param
storepass password

        this.storepass = storepass;
    
public voidsetStoretype(java.lang.String storetype)
Keystore type.

param
storetype type

        this.storetype = storetype;
    
public voidsetValidity(java.lang.String validity)
Indicates how many days certificate is valid.

param
validity days valid
throws
BuildException If not an Integer

        try {
            this.validity = Integer.parseInt(validity);
        } catch (final NumberFormatException nfe) {
            throw new BuildException("Validity attribute should be a integer");
        }
    
public voidsetVerbose(boolean verbose)
If true, verbose output when signing.

param
verbose verbose or not

        this.verbose = verbose;