1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.javafx.tools.ant;
  27 
  28 import com.sun.javafx.tools.packager.PackagerLib;
  29 import com.sun.javafx.tools.packager.SignJarParams;
  30 import com.oracle.tools.packager.Log;
  31 import java.io.File;
  32 import java.util.ArrayList;
  33 import java.util.List;
  34 import org.apache.tools.ant.BuildException;
  35 import org.apache.tools.ant.Task;
  36 import org.apache.tools.ant.types.FileSet;
  37 
  38 /**
  39  * Sign jar as BLOB (instead of signing every entry separately sign jar
  40  * file as one huge binary object).
  41  * <p>
  42  * This is new signing method in JavaFX. Standard ant signjar task need to
  43  * be used for traditional signing.
  44  * <p>
  45  * Usage examples:
  46  * <pre>
  47  *   &lt;signjar keyStore="${basedir}/sample.jks" destdir="c:/tmp"
  48  *               alias="javafx" storePass="****" keyPass="****"&gt;
  49  *      &lt;fileset dir='${build.dir}/dist/** /*.jar'/&gt;
  50  *   &lt;/signjar&gt;
  51  * </pre>
  52  *
  53  * @ant.task name="signjar" category="javafx"
  54  */
  55 public class FXSignJarTask extends Task {
  56 
  57     private File keyStore;
  58     private String alias;
  59     private String storePass;
  60     private String keyPass;
  61     private String storeType = "jks";
  62     private List<FileSet> fileSets = new ArrayList<FileSet>();
  63     private String deprecatedSourceJar; //deprecated since 2.2
  64     private String sourceJar; //deprecated since 2.2
  65     private String destDir;
  66 
  67     private PackagerLib packager;
  68     private SignJarParams signJarParams;
  69     private boolean verbose = false;
  70 
  71     public FXSignJarTask() {
  72         Log.info("<fx:signjar> is deprecated");
  73         packager = new PackagerLib();
  74         signJarParams = new SignJarParams();
  75     }
  76 
  77     @Override
  78     public void execute() throws BuildException {
  79         signJarParams.setKeyStore(keyStore);
  80         signJarParams.setAlias(alias);
  81         signJarParams.setStorePass(storePass);
  82         signJarParams.setKeyPass(keyPass);
  83         signJarParams.setStoreType(storeType);
  84         signJarParams.setVerbose(verbose);
  85 
  86         if (destDir != null) {
  87             signJarParams.setOutdir(new File(destDir));
  88         }
  89 
  90         if (!fileSets.isEmpty()) {
  91             if (deprecatedSourceJar != null) {
  92                 throw new IllegalArgumentException(
  93                         "Unexpected sourcejar attribute when fileset present");
  94             }
  95             if (sourceJar != null) {
  96                 throw new IllegalArgumentException(
  97                         "Unexpected sourcejar attribute when fileset present");
  98             }
  99         }
 100 
 101         if (sourceJar != null && deprecatedSourceJar != null) {
 102                 throw new IllegalArgumentException(
 103                         "Cannot use both sourcejar and jar attributes. (sourcejar attribute is deprecated).");
 104         }
 105 
 106         for (FileSet fileset: fileSets) {
 107             Utils.addResources(signJarParams, fileset);
 108         }
 109         if (deprecatedSourceJar != null) {
 110             File sourceFile = new File(deprecatedSourceJar);
 111             if (sourceFile.exists()) {
 112                 signJarParams.addResource(new File("."), sourceFile);
 113             }
 114         }
 115         if (sourceJar != null) {
 116             File sourceFile = new File(sourceJar);
 117             if (sourceFile.exists()) {
 118                 //treat as request to copy file to top level folder
 119                 // (i.e. ignore path part)
 120                 //this is consistent with built-in signjar
 121                 signJarParams.addResource(
 122                         sourceFile.getParentFile(), sourceFile.getName());
 123             }
 124         }
 125         try {
 126             signJarParams.validate();
 127             packager.signJar(signJarParams);
 128         } catch (Exception e) {
 129             throw new BuildException(e.getMessage(), e);
 130         }
 131     }
 132 
 133     /**
 134      * The alias to sign under.
 135      *
 136      * @ant.required
 137      */
 138     public void setAlias(String alias) {
 139         this.alias = alias;
 140     }
 141 
 142     public void setVerbose(Boolean verbose) {
 143         this.verbose = verbose;
 144     }
 145 
 146 
 147     /**
 148      * Password for private key.
 149      *
 150      * @ant.required
 151      */
 152     public void setKeyPass(String keyPassword) {
 153         this.keyPass = keyPassword;
 154     }
 155 
 156     /**
 157      * Password for keystore
 158      *
 159      * @ant.required
 160      */
 161     public void setStorePass(String storePassword) {
 162         this.storePass = storePassword;
 163     }
 164 
 165     /**
 166      * Keystore to use.
 167      *
 168      * @ant.required
 169      */
 170     public void setKeyStore(File keyStore) {
 171         this.keyStore = keyStore;
 172     }
 173 
 174     /**
 175      * Keystore type
 176      *
 177      * @ant.not-required Default is "jks".
 178      */
 179     public void setStoreType(String storeType) {
 180         this.storeType = storeType;
 181     }
 182 
 183     public void addFileSet(FileSet fileset) {
 184         fileSets.add(fileset);
 185     }
 186 
 187     /**
 188      * The jar to sign (deprecated since 2.2, use jar attribute)
 189      *
 190      * @ant.not-required Either this or jar attribute or embedded fileset are required.
 191      */
 192     public void setSourceJar(String sourceJar) {
 193         this.deprecatedSourceJar = sourceJar;
 194     }
 195 
 196     /**
 197      * The jar to sign.
 198      *
 199      * @ant.not-required Either this or sourcejar or embedded fileset are required.
 200      */
 201     public void setJar(String sourceJar) {
 202         this.sourceJar = sourceJar;
 203     }
 204 
 205     /**
 206      * Location of output file.
 207      *
 208      * @ant.required
 209      */
 210     public void setDestDir(String destDir) {
 211         this.destDir = destDir;
 212     }
 213 }