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  * SecondaryLauncherArguments
  36  *
  37  * Processes a secondary launcher properties file to create the Map of
  38  * bundle params applicable to the secondary launcher:
  39  *
  40  * BundlerParams p = (new SecondaryLauncherArguments(file)).getLauncherMap();
  41  *
  42  * A secondary launcher is another executable program generated by either the
  43  * create-image mode or the create-installer mode.
  44  * The secondary 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 secondary launchers, each created by using the
  48  * command line arg "--secondary-launcher <file path>
  49  *
  50  * The secondary launcher properties file may have any of:
  51  *
  52  * name (required)
  53  * version
  54  * module
  55  * class
  56  * icon
  57  * arguments
  58  * jvm-args
  59  * win-menu
  60  * win-shortcut
  61  * win-console
  62  *
  63  */
  64 class SecondaryLauncherArguments {
  65 
  66     private final String filename;
  67     private Map<String, String> allArgs;
  68     private Map<String, ? super Object> bundleParams;
  69 
  70     SecondaryLauncherArguments(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 mainClass = getOptionValue(CLIOptions.APPCLASS);
  83         String module = getOptionValue(CLIOptions.MODULE);
  84 
  85         if (module != null && mainClass != null) {
  86             putUnlessNull(bundleParams, Arguments.CLIOptions.MODULE.getId(),
  87                     module + "/" + mainClass);
  88         } else if (module != null) {
  89             putUnlessNull(bundleParams, Arguments.CLIOptions.MODULE.getId(),
  90                     module);
  91         } else if (mainClass != null) {
  92             putUnlessNull(bundleParams, Arguments.CLIOptions.APPCLASS.getId(),
  93                     mainClass);
  94         }
  95 
  96         putUnlessNull(bundleParams, Arguments.CLIOptions.NAME.getId(),
  97                 getOptionValue(CLIOptions.NAME));
  98         putUnlessNull(bundleParams, Arguments.CLIOptions.VERSION.getId(),
  99                 getOptionValue(CLIOptions.VERSION));
 100 
 101         putUnlessNull(bundleParams, Arguments.CLIOptions.WIN_MENU_HINT.getId(),
 102                 getOptionValue(CLIOptions.WIN_MENU_HINT));
 103         putUnlessNull(bundleParams,
 104                 Arguments.CLIOptions.WIN_SHORTCUT_HINT.getId(),
 105                 getOptionValue(CLIOptions.WIN_SHORTCUT_HINT));
 106         putUnlessNull(bundleParams,
 107                 Arguments.CLIOptions.WIN_CONSOLE_HINT.getId(),
 108                 getOptionValue(CLIOptions.WIN_CONSOLE_HINT));
 109 
 110         String value = getOptionValue(CLIOptions.ICON);
 111         putUnlessNull(bundleParams, Arguments.CLIOptions.ICON.getId(),
 112                 (value == null) ? null : new File(value));
 113 
 114         String argumentStr = getOptionValue(CLIOptions.ARGUMENTS);
 115         putUnlessNullOrEmpty(bundleParams,
 116                 CLIOptions.ARGUMENTS.getId(),
 117                 Arguments.getArgumentList(argumentStr));
 118 
 119         String jvmargsStr = getOptionValue(CLIOptions.JVM_ARGS);
 120         putUnlessNullOrEmpty(bundleParams,
 121                 CLIOptions.JVM_ARGS.getId(),
 122                 Arguments.getArgumentList(jvmargsStr));
 123     }
 124 
 125     private String getOptionValue(CLIOptions option) {
 126         if (option == null || allArgs == null) {
 127             return null;
 128         }
 129 
 130         String id = option.getId();
 131 
 132         if (allArgs.containsKey(id)) {
 133             return allArgs.get(id);
 134         }
 135 
 136         return null;
 137     }
 138 
 139     Map<String, ? super Object> getLauncherMap() {
 140         initLauncherMap();
 141         return bundleParams;
 142     }
 143 
 144     private void putUnlessNull(Map<String, ? super Object> params,
 145             String param, Object value) {
 146         if (value != null) {
 147             params.put(param, value);
 148         }
 149     }
 150 
 151     private void putUnlessNullOrEmpty(Map<String, ? super Object> params,
 152             String param, Collection<?> value) {
 153         if (value != null && !value.isEmpty()) {
 154             params.put(param, value);
 155         }
 156     }
 157 
 158     private void putUnlessNullOrEmpty(Map<String, ? super Object> params,
 159             String param, Map<?, ?> value) {
 160         if (value != null && !value.isEmpty()) {
 161             params.put(param, value);
 162         }
 163     }
 164 }