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