1 /*
   2  * Copyright (c) 2018, 2019, 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 jdk.jpackage.internal;
  27 
  28 import java.util.Collection;
  29 import java.util.HashMap;
  30 import java.util.Map;
  31 import java.io.File;
  32 import jdk.jpackage.internal.Arguments.CLIOptions;
  33 
  34 /*
  35  * AddLauncherArguments
  36  *
  37  * Processes a add-launcher properties file to create the Map of
  38  * bundle params applicable to the add-launcher:
  39  *
  40  * BundlerParams p = (new AddLauncherArguments(file)).getLauncherMap();
  41  *
  42  * A add-launcher is another executable program generated by either the
  43  * create-app-image mode or the create-installer mode.
  44  * The add-launcher may be the same program with different configuration,
  45  * or a completely different program created from the same files.
  46  *
  47  * There may be multiple add-launchers, each created by using the
  48  * command line arg "--add-launcher <file path>
  49  *
  50  * The add-launcher properties file may have any of:
  51  *
  52  * appVersion
  53  * module
  54  * main-jar
  55  * main-class
  56  * icon
  57  * arguments
  58  * java-options
  59  * win-console
  60  *
  61  */
  62 class AddLauncherArguments {
  63 
  64     private final String name;
  65     private final String filename;
  66     private Map<String, String> allArgs;
  67     private Map<String, ? super Object> bundleParams;
  68 
  69     AddLauncherArguments(String name, String filename) {
  70         this.name = name;
  71         this.filename = filename;
  72     }
  73 
  74     private void initLauncherMap() {
  75         if (bundleParams != null) {
  76             return;
  77         }
  78 
  79         allArgs = Arguments.getPropertiesFromFile(filename);
  80         allArgs.put(CLIOptions.NAME.getId(), name);
  81 
  82         bundleParams = new HashMap<>();
  83         String mainJar = getOptionValue(CLIOptions.MAIN_JAR);
  84         String mainClass = getOptionValue(CLIOptions.APPCLASS);
  85         String module = getOptionValue(CLIOptions.MODULE);
  86 
  87         if (module != null && mainClass != null) {
  88             putUnlessNull(bundleParams, CLIOptions.MODULE.getId(),
  89                     module + "/" + mainClass);
  90         } else if (module != null) {
  91             putUnlessNull(bundleParams, CLIOptions.MODULE.getId(),
  92                     module);
  93         } else {
  94             putUnlessNull(bundleParams, CLIOptions.MAIN_JAR.getId(),
  95                     mainJar);
  96             putUnlessNull(bundleParams, CLIOptions.APPCLASS.getId(),
  97                     mainClass);
  98         }
  99 
 100         putUnlessNull(bundleParams, CLIOptions.NAME.getId(),
 101                 getOptionValue(CLIOptions.NAME));
 102 
 103         putUnlessNull(bundleParams, CLIOptions.VERSION.getId(),
 104                 getOptionValue(CLIOptions.VERSION));
 105 
 106         putUnlessNull(bundleParams, CLIOptions.RELEASE.getId(),
 107                 getOptionValue(CLIOptions.RELEASE));
 108 
 109         putUnlessNull(bundleParams, CLIOptions.LINUX_CATEGORY.getId(),
 110                 getOptionValue(CLIOptions.LINUX_CATEGORY));
 111 
 112         putUnlessNull(bundleParams,
 113                 CLIOptions.WIN_CONSOLE_HINT.getId(),
 114                 getOptionValue(CLIOptions.WIN_CONSOLE_HINT));
 115 
 116         String value = getOptionValue(CLIOptions.ICON);
 117         putUnlessNull(bundleParams, CLIOptions.ICON.getId(),
 118                 (value == null) ? null : new File(value));
 119 
 120         String argumentStr = getOptionValue(CLIOptions.ARGUMENTS);
 121         putUnlessNullOrEmpty(bundleParams,
 122                 CLIOptions.ARGUMENTS.getId(),
 123                 Arguments.getArgumentList(argumentStr));
 124 
 125         String jvmargsStr = getOptionValue(CLIOptions.JAVA_OPTIONS);
 126         putUnlessNullOrEmpty(bundleParams,
 127                 CLIOptions.JAVA_OPTIONS.getId(),
 128                 Arguments.getArgumentList(jvmargsStr));
 129     }
 130 
 131     private String getOptionValue(CLIOptions option) {
 132         if (option == null || allArgs == null) {
 133             return null;
 134         }
 135 
 136         String id = option.getId();
 137 
 138         if (allArgs.containsKey(id)) {
 139             return allArgs.get(id);
 140         }
 141 
 142         return null;
 143     }
 144 
 145     Map<String, ? super Object> getLauncherMap() {
 146         initLauncherMap();
 147         return bundleParams;
 148     }
 149 
 150     private void putUnlessNull(Map<String, ? super Object> params,
 151             String param, Object value) {
 152         if (value != null) {
 153             params.put(param, value);
 154         }
 155     }
 156 
 157     private void putUnlessNullOrEmpty(Map<String, ? super Object> params,
 158             String param, Collection<?> value) {
 159         if (value != null && !value.isEmpty()) {
 160             params.put(param, value);
 161         }
 162     }
 163 
 164     static Map<String, ? super Object> merge(
 165             Map<String, ? super Object> original,
 166             Map<String, ? super Object> additional) {
 167         Map<String, ? super Object> tmp = new HashMap<>(original);
 168         if (additional.containsKey("module")) {
 169             tmp.remove("main-jar");
 170             tmp.remove("main-class");
 171         } else if (additional.containsKey("main-jar")) {
 172             tmp.remove("module");
 173         }
 174         tmp.putAll(additional);
 175         return tmp;
 176     }
 177 
 178 }