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-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  * name (required)
  53  * appVersion
  54  * module
  55  * add-modules
  56  * main-jar
  57  * main-class
  58  * icon
  59  * arguments
  60  * jvm-args
  61  * win-console
  62  *
  63  */
  64 class AddLauncherArguments {
  65 
  66     private final String filename;
  67     private Map<String, String> allArgs;
  68     private Map<String, ? super Object> bundleParams;
  69 
  70     AddLauncherArguments(String filename) {
  71         this.filename = filename;
  72     }
  73 
  74     private void initLauncherMap() {
  75         if (bundleParams != null) {
  76             return;
  77         }
  78 
  79         allArgs = Arguments.getPropertiesFromFile(filename);
  80 
  81         bundleParams = new HashMap<>();
  82         String mainJar = getOptionValue(CLIOptions.MAIN_JAR);
  83         String mainClass = getOptionValue(CLIOptions.APPCLASS);
  84         String module = getOptionValue(CLIOptions.MODULE);
  85 
  86         if (module != null && mainClass != null) {
  87             putUnlessNull(bundleParams, Arguments.CLIOptions.MODULE.getId(),
  88                     module + "/" + mainClass);
  89         } else if (module != null) {
  90             putUnlessNull(bundleParams, Arguments.CLIOptions.MODULE.getId(),
  91                     module);
  92         } else {
  93             putUnlessNull(bundleParams, Arguments.CLIOptions.MAIN_JAR.getId(),
  94                     mainJar);
  95             putUnlessNull(bundleParams, Arguments.CLIOptions.APPCLASS.getId(),
  96                     mainClass);
  97         }
  98 
  99         putUnlessNull(bundleParams, Arguments.CLIOptions.NAME.getId(),
 100                 getOptionValue(CLIOptions.NAME));
 101 
 102         putUnlessNull(bundleParams, Arguments.CLIOptions.VERSION.getId(),
 103                 getOptionValue(CLIOptions.VERSION));
 104 
 105         putUnlessNull(bundleParams,
 106                 Arguments.CLIOptions.ADD_MODULES.getId(),
 107                 getOptionValue(CLIOptions.ADD_MODULES));
 108 
 109         putUnlessNull(bundleParams,
 110                 Arguments.CLIOptions.WIN_CONSOLE_HINT.getId(),
 111                 getOptionValue(CLIOptions.WIN_CONSOLE_HINT));
 112 
 113         String value = getOptionValue(CLIOptions.ICON);
 114         putUnlessNull(bundleParams, Arguments.CLIOptions.ICON.getId(),
 115                 (value == null) ? null : new File(value));
 116 
 117         String argumentStr = getOptionValue(CLIOptions.ARGUMENTS);
 118         putUnlessNullOrEmpty(bundleParams,
 119                 CLIOptions.ARGUMENTS.getId(),
 120                 Arguments.getArgumentList(argumentStr));
 121 
 122         String jvmargsStr = getOptionValue(CLIOptions.JVM_ARGS);
 123         putUnlessNullOrEmpty(bundleParams,
 124                 CLIOptions.JVM_ARGS.getId(),
 125                 Arguments.getArgumentList(jvmargsStr));
 126     }
 127 
 128     private String getOptionValue(CLIOptions option) {
 129         if (option == null || allArgs == null) {
 130             return null;
 131         }
 132 
 133         String id = option.getId();
 134 
 135         if (allArgs.containsKey(id)) {
 136             return allArgs.get(id);
 137         }
 138 
 139         return null;
 140     }
 141 
 142     Map<String, ? super Object> getLauncherMap() {
 143         initLauncherMap();
 144         return bundleParams;
 145     }
 146 
 147     private void putUnlessNull(Map<String, ? super Object> params,
 148             String param, Object value) {
 149         if (value != null) {
 150             params.put(param, value);
 151         }
 152     }
 153 
 154     private void putUnlessNullOrEmpty(Map<String, ? super Object> params,
 155             String param, Collection<?> value) {
 156         if (value != null && !value.isEmpty()) {
 157             params.put(param, value);
 158         }
 159     }
 160 
 161     private void putUnlessNullOrEmpty(Map<String, ? super Object> params,
 162             String param, Map<?, ?> value) {
 163         if (value != null && !value.isEmpty()) {
 164             params.put(param, value);
 165         }
 166     }
 167 }