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