1 /*
   2  * Copyright (c) 2013, 2015, 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 hello;
  27 
  28 import com.oracle.tools.packager.Bundler;
  29 import com.oracle.tools.packager.Bundlers;
  30 import com.oracle.tools.packager.ConfigException;
  31 import com.oracle.tools.packager.Log;
  32 import com.oracle.tools.packager.RelativeFileSet;
  33 import com.oracle.tools.packager.StandardBundlerParam;
  34 import com.oracle.tools.packager.UnsupportedPlatformException;
  35 
  36 import java.io.File;
  37 import java.io.IOException;
  38 import java.util.Arrays;
  39 import java.util.HashMap;
  40 import java.util.HashSet;
  41 import java.util.LinkedList;
  42 import java.util.Map;
  43 import java.util.Queue;
  44 import java.util.Set;
  45 import java.util.TreeMap;
  46 import java.util.TreeSet;
  47 
  48 import static com.oracle.tools.packager.StandardBundlerParam.*;
  49 import static com.oracle.tools.packager.StandardBundlerParam.ICON;
  50 import static com.oracle.tools.packager.StandardBundlerParam.VERBOSE;
  51 
  52 public class SimpleBundle {
  53 
  54     public static void main(String... args) throws IOException, ConfigException, UnsupportedPlatformException {
  55         if (args.length == 0) {
  56             System.out.println("Usage: SimpleBundle [-o <outputdir>] [-b <name> <value>]* bundlerID*");
  57             System.out.println("  -o : directory to put output in.  Default is '.'");
  58             System.out.println("  -b : Bundler Argument.  Next value is name, value after that is the value");
  59             System.out.println("  all remaining arguments create the set of bundlerIDs to run, default is to run all.");
  60             return;
  61         }
  62 
  63         File output = new File(".");
  64         Set<String> bundlerIDs = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
  65         Map<String, ? super Object> params = new TreeMap<>();
  66 
  67         Queue<String> argsQ = new LinkedList<>(Arrays.asList(args));
  68         while (!argsQ.isEmpty()) {
  69             String arg = argsQ.remove();
  70             switch (arg) {
  71                 case "-o":
  72                     output = new File(argsQ.remove());
  73                     output.mkdirs();
  74                     break;
  75 
  76                 case "-b":
  77                     params.put(argsQ.remove(), argsQ.remove());
  78                     break;
  79 
  80                 case "-all":
  81                     // JVM args
  82                     params.put(StandardBundlerParam.JVM_OPTIONS.getID(), "-Doption.1=bundlerargs\n-Doption.2=bundlerargs\n-Dcollide=jvmoptions");
  83                     // properties
  84                     params.put(StandardBundlerParam.JVM_PROPERTIES.getID(), "prop.1=bundlerargs\nprop.2=bundlerargs\ncollide=properties");
  85                     // userJVM Args
  86                     params.put(StandardBundlerParam.USER_JVM_OPTIONS.getID(), "-Duser.arg.1\\==bundlerargs\n-Duser.arg.2=\\=bundlerargs\n-Dcollide=\\=userjvmoptions\n-Dcollide\\=jvmoptions=AWESOME");
  87                     // arguments
  88                     params.put(StandardBundlerParam.ARGUMENTS.getID(), "argument1\n" +
  89                             "argument2\n" +
  90                             "argument3=value\n" +
  91                             "arg4=with=embedded\n" +
  92                             "arg5=with=equals=at=end=\n" +
  93                             "one_equal_at_end=\n" +
  94                             "=\n" +
  95                             "\"Prev Arg was just an equals\"\n" +
  96                             "argument1\n" +
  97                             "argument1\n" +
  98                             "argument1");
  99                     break;
 100 
 101                 default:
 102                     if (!arg.isEmpty() && !"all".equals(arg)) {
 103                         bundlerIDs.add(arg);
 104                     }
 105             }
 106         }
 107 
 108         System.out.println("Output directory is : " + output.getCanonicalPath());
 109         System.out.println(" with " + params.size() + " extra bundler arguments");
 110         if (bundlerIDs.isEmpty()) {
 111             System.out.println(" attempting all bundlers");
 112         } else {
 113             for (String b : bundlerIDs) {
 114                 System.out.println(" attempting bundlers with ID or type " + b);
 115             }
 116         }
 117 
 118         Log.setLogger(new Log.Logger(VERBOSE.fetchFrom(params)));
 119 
 120         Bundlers bundlers = Bundlers.createBundlersInstance();
 121 
 122         for (Bundler bundler : bundlers.getBundlers()) {
 123             if (!bundlerIDs.isEmpty()
 124                     && !bundlerIDs.contains(bundler.getBundleType())
 125                     && !bundlerIDs.contains(bundler.getID())) {
 126                 continue;
 127             }
 128 
 129             Map<String, Object> bundleParams = new HashMap<>();
 130 
 131             bundleParams.put(BUILD_ROOT.getID(), output);
 132 
 133             File appResourcesDir = new File(".");
 134             File fakeMainJar = new File(appResourcesDir, "mainApp.jar");
 135             File packagerJar = new File(appResourcesDir, "packager.jar");
 136             Set<File> appResources = new HashSet<>(Arrays.asList(fakeMainJar, packagerJar));
 137 
 138             bundleParams.put(APP_NAME.getID(), "DevTest");
 139             bundleParams.put(MAIN_CLASS.getID(), "hello.TestPackager");
 140             bundleParams.put(MAIN_JAR.getID(),
 141                     new RelativeFileSet(fakeMainJar.getParentFile(),
 142                             new HashSet<>(Arrays.asList(fakeMainJar)))
 143             );
 144             bundleParams.put(CLASSPATH.getID(), fakeMainJar.getName() + " " + packagerJar.getName());
 145             bundleParams.put(APP_RESOURCES.getID(), new RelativeFileSet(appResourcesDir, appResources));
 146             bundleParams.put(VERBOSE.getID(), true);
 147             bundleParams.put(ICON.getID(), "java-logo2.gif");
 148 
 149             bundleParams.putAll(params);
 150             try {
 151                 bundler.validate(bundleParams);
 152             } catch (UnsupportedPlatformException upe) {
 153                 System.out.println("Skipping " + bundler.getID() + " as it is not supported on this platform");
 154                 continue;
 155             } catch (ConfigException ce) {
 156                 System.out.println("Configuration Error : " + ce.getMessage());
 157                 System.out.println("  Advice to fix : " + ce.getAdvice());
 158                 continue;
 159             }
 160 
 161             System.out.println("Running bundler for " + bundler.getID());
 162             System.out.println(" results at " + bundler.execute(bundleParams, output));
 163         }
 164     }
 165 }