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 jdk.incubator.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  * linux-app-category
  61  *
  62  */
  63 class AddLauncherArguments {
  64 
  65     private final String name;
  66     private final String filename;
  67     private Map<String, String> allArgs;
  68     private Map<String, ? super Object> bundleParams;
  69 
  70     AddLauncherArguments(String name, String filename) {
  71         this.name = name;
  72         this.filename = filename;
  73     }
  74 
  75     private void initLauncherMap() {
  76         if (bundleParams != null) {
  77             return;
  78         }
  79 
  80         allArgs = Arguments.getPropertiesFromFile(filename);
  81         allArgs.put(CLIOptions.NAME.getId(), name);
  82 
  83         bundleParams = new HashMap<>();
  84         String mainJar = getOptionValue(CLIOptions.MAIN_JAR);
  85         String mainClass = getOptionValue(CLIOptions.APPCLASS);
  86         String module = getOptionValue(CLIOptions.MODULE);
  87 
  88         if (module != null && mainClass != null) {
  89             putUnlessNull(bundleParams, CLIOptions.MODULE.getId(),
  90                     module + "/" + mainClass);
  91         } else if (module != null) {
  92             putUnlessNull(bundleParams, CLIOptions.MODULE.getId(),
  93                     module);
  94         } else {
  95             putUnlessNull(bundleParams, CLIOptions.MAIN_JAR.getId(),
  96                     mainJar);
  97             putUnlessNull(bundleParams, CLIOptions.APPCLASS.getId(),
  98                     mainClass);
  99         }
 100 
 101         putUnlessNull(bundleParams, CLIOptions.NAME.getId(),
 102                 getOptionValue(CLIOptions.NAME));
 103 
 104         putUnlessNull(bundleParams, CLIOptions.VERSION.getId(),
 105                 getOptionValue(CLIOptions.VERSION));
 106 
 107         putUnlessNull(bundleParams, CLIOptions.RELEASE.getId(),
 108                 getOptionValue(CLIOptions.RELEASE));
 109 
 110         putUnlessNull(bundleParams, CLIOptions.LINUX_CATEGORY.getId(),
 111                 getOptionValue(CLIOptions.LINUX_CATEGORY));
 112 
 113         putUnlessNull(bundleParams,
 114                 CLIOptions.WIN_CONSOLE_HINT.getId(),
 115                 getOptionValue(CLIOptions.WIN_CONSOLE_HINT));
 116 
 117         String value = getOptionValue(CLIOptions.ICON);
 118         putUnlessNull(bundleParams, CLIOptions.ICON.getId(),
 119                 (value == null) ? null : new File(value));
 120 
 121         // "arguments" and "java-options" even if value is null:
 122         if (allArgs.containsKey(CLIOptions.ARGUMENTS.getId())) {
 123             String argumentStr = getOptionValue(CLIOptions.ARGUMENTS);
 124             bundleParams.put(CLIOptions.ARGUMENTS.getId(),
 125                     Arguments.getArgumentList(argumentStr));
 126         }
 127 
 128         if (allArgs.containsKey(CLIOptions.JAVA_OPTIONS.getId())) {
 129             String jvmargsStr = getOptionValue(CLIOptions.JAVA_OPTIONS);
 130             bundleParams.put(CLIOptions.JAVA_OPTIONS.getId(),
 131                     Arguments.getArgumentList(jvmargsStr));
 132         }
 133     }
 134 
 135     private String getOptionValue(CLIOptions option) {
 136         if (option == null || allArgs == null) {
 137             return null;
 138         }
 139 
 140         String id = option.getId();
 141 
 142         if (allArgs.containsKey(id)) {
 143             return allArgs.get(id);
 144         }
 145 
 146         return null;
 147     }
 148 
 149     Map<String, ? super Object> getLauncherMap() {
 150         initLauncherMap();
 151         return bundleParams;
 152     }
 153 
 154     private void putUnlessNull(Map<String, ? super Object> params,
 155             String param, Object value) {
 156         if (value != null) {
 157             params.put(param, value);
 158         }
 159     }
 160 
 161     static Map<String, ? super Object> merge(
 162             Map<String, ? super Object> original,
 163             Map<String, ? super Object> additional) {
 164         Map<String, ? super Object> tmp = new HashMap<>(original);
 165         if (additional.containsKey(CLIOptions.MODULE.getId())) {
 166             tmp.remove(CLIOptions.MAIN_JAR.getId());
 167             tmp.remove(CLIOptions.APPCLASS.getId());
 168         } else if (additional.containsKey(CLIOptions.MAIN_JAR.getId())) {
 169             tmp.remove(CLIOptions.MODULE.getId());
 170         }
 171         if (additional.containsKey(CLIOptions.ARGUMENTS.getId())) {
 172             // if add launcher properties file contains "arguments", even with
 173             // null value, disregard the "arguments" from command line
 174             tmp.remove(CLIOptions.ARGUMENTS.getId());
 175         }
 176         if (additional.containsKey(CLIOptions.JAVA_OPTIONS.getId())) {
 177             // same thing for java-options
 178             tmp.remove(CLIOptions.JAVA_OPTIONS.getId());
 179         }
 180         tmp.putAll(additional);
 181         return tmp;
 182     }
 183 
 184 }