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.HashMap;
  29 import java.util.HashSet;
  30 import java.util.Map;
  31 import java.util.Set;
  32 import jdk.jpackage.internal.Arguments.CLIOptions;
  33 
  34 /**
  35  * ValidOptions
  36  *
  37  * Two basic methods for validating command line options.
  38  *
  39  * initArgs()
  40  *      Computes the Map of valid options for each mode on this Platform.
  41  *
  42  * checkIfSupported(CLIOptions mode, CLIOptions arg)
  43  *      Determine if the given arg is valid in the given mode.
  44  *
  45  * checkIfOtherSupported(CLIOptions mode, CLIOptions arg)
  46  *      Determine if the given arg is valid in the a different mode.
  47  */
  48 class ValidOptions {
  49 
  50     private ValidOptions() {};
  51 
  52     // multimap that contains pairs of (mode, supported args)
  53     private static final Map<CLIOptions, Set<CLIOptions>> options =
  54             new HashMap<>();
  55 
  56     private static boolean argsInitialized = false;
  57 
  58     // initializing list of mandatory arguments
  59     private static void initArgs() {
  60         if (argsInitialized) {
  61             return;
  62         }
  63 
  64         // add options for CREATE_IMAGE
  65         add(CLIOptions.CREATE_IMAGE, CLIOptions.INPUT);
  66         add(CLIOptions.CREATE_IMAGE, CLIOptions.OUTPUT);
  67         add(CLIOptions.CREATE_IMAGE, CLIOptions.APPCLASS);
  68         add(CLIOptions.CREATE_IMAGE, CLIOptions.NAME);
  69         add(CLIOptions.CREATE_IMAGE, CLIOptions.IDENTIFIER);
  70         add(CLIOptions.CREATE_IMAGE, CLIOptions.VERBOSE);
  71         add(CLIOptions.CREATE_IMAGE, CLIOptions.OVERWRITE);
  72         add(CLIOptions.CREATE_IMAGE, CLIOptions.FILES);
  73         add(CLIOptions.CREATE_IMAGE, CLIOptions.ARGUMENTS);
  74         add(CLIOptions.CREATE_IMAGE, CLIOptions.STRIP_NATIVE_COMMANDS);
  75         add(CLIOptions.CREATE_IMAGE, CLIOptions.ICON);
  76         add(CLIOptions.CREATE_IMAGE, CLIOptions.VERSION);
  77         add(CLIOptions.CREATE_IMAGE, CLIOptions.JVM_ARGS);
  78         add(CLIOptions.CREATE_IMAGE, CLIOptions.SECONDARY_LAUNCHER);
  79         add(CLIOptions.CREATE_IMAGE, CLIOptions.BUILD_ROOT);
  80         add(CLIOptions.CREATE_IMAGE, CLIOptions.PREDEFINED_RUNTIME_IMAGE);
  81         add(CLIOptions.CREATE_IMAGE, CLIOptions.MAIN_JAR);
  82         add(CLIOptions.CREATE_IMAGE, CLIOptions.MODULE);
  83         add(CLIOptions.CREATE_IMAGE, CLIOptions.ADD_MODULES);
  84         add(CLIOptions.CREATE_IMAGE, CLIOptions.MODULE_PATH);
  85         add(CLIOptions.CREATE_IMAGE, CLIOptions.RESOURCE_DIR);
  86 
  87         if (Platform.getPlatform() == Platform.MAC) {
  88             add(CLIOptions.CREATE_IMAGE, CLIOptions.MAC_SIGN);
  89             add(CLIOptions.CREATE_IMAGE, CLIOptions.MAC_BUNDLE_NAME);
  90             add(CLIOptions.CREATE_IMAGE, CLIOptions.MAC_BUNDLE_IDENTIFIER);
  91             add(CLIOptions.CREATE_IMAGE, CLIOptions.MAC_BUNDLE_SIGNING_PREFIX);
  92             add(CLIOptions.CREATE_IMAGE, CLIOptions.MAC_SIGNING_KEY_NAME);
  93             add(CLIOptions.CREATE_IMAGE, CLIOptions.MAC_SIGNING_KEYCHAIN);
  94             add(CLIOptions.CREATE_IMAGE, CLIOptions.CATEGORY);
  95             add(CLIOptions.CREATE_IMAGE, CLIOptions.COPYRIGHT);
  96         }
  97 
  98         if (Platform.getPlatform() == Platform.WINDOWS) {
  99             add(CLIOptions.CREATE_IMAGE, CLIOptions.DESCRIPTION);
 100             add(CLIOptions.CREATE_IMAGE, CLIOptions.VENDOR);
 101             add(CLIOptions.CREATE_IMAGE, CLIOptions.COPYRIGHT);
 102             add(CLIOptions.CREATE_IMAGE, CLIOptions.WIN_CONSOLE_HINT);
 103         }
 104 
 105         // add options for CREATE_INSTALLER
 106         // (start with all options for CREATE_IMAGE)
 107         Set<CLIOptions> imageOptions = options.get(CLIOptions.CREATE_IMAGE);
 108         imageOptions.forEach(o -> add(CLIOptions.CREATE_INSTALLER, o));
 109 
 110         add(CLIOptions.CREATE_INSTALLER, CLIOptions.RUNTIME_INSTALLER);
 111         add(CLIOptions.CREATE_INSTALLER, CLIOptions.INSTALLER_TYPE);
 112         add(CLIOptions.CREATE_INSTALLER, CLIOptions.LICENSE_FILE);
 113         add(CLIOptions.CREATE_INSTALLER, CLIOptions.FILE_ASSOCIATIONS);
 114         add(CLIOptions.CREATE_INSTALLER, CLIOptions.INSTALL_DIR);
 115         add(CLIOptions.CREATE_INSTALLER, CLIOptions.PREDEFINED_APP_IMAGE);
 116         add(CLIOptions.CREATE_INSTALLER, CLIOptions.INSTALLER_TYPE);
 117 
 118         if (Platform.getPlatform() == Platform.MAC) {
 119             add(CLIOptions.CREATE_INSTALLER, CLIOptions.MAC_APP_STORE_CATEGORY);
 120             add(CLIOptions.CREATE_INSTALLER,
 121                     CLIOptions.MAC_APP_STORE_ENTITLEMENTS);
 122         }
 123 
 124         if (Platform.getPlatform() == Platform.LINUX) {
 125             add(CLIOptions.CREATE_INSTALLER, CLIOptions.LINUX_BUNDLE_NAME);
 126             add(CLIOptions.CREATE_INSTALLER, CLIOptions.LINUX_DEB_MAINTAINER);
 127             add(CLIOptions.CREATE_INSTALLER, CLIOptions.LINUX_RPM_LICENSE_TYPE);
 128             add(CLIOptions.CREATE_INSTALLER,
 129                     CLIOptions.LINUX_PACKAGE_DEPENDENCIES);
 130             add(CLIOptions.CREATE_INSTALLER, CLIOptions.DESCRIPTION);
 131             add(CLIOptions.CREATE_INSTALLER, CLIOptions.VENDOR);
 132             add(CLIOptions.CREATE_INSTALLER, CLIOptions.CATEGORY);
 133             add(CLIOptions.CREATE_INSTALLER, CLIOptions.COPYRIGHT);
 134         }
 135 
 136         if (Platform.getPlatform() == Platform.WINDOWS) {
 137             add(CLIOptions.CREATE_INSTALLER, CLIOptions.WIN_MENU_HINT);
 138             add(CLIOptions.CREATE_INSTALLER, CLIOptions.WIN_MENU_GROUP);
 139             add(CLIOptions.CREATE_INSTALLER, CLIOptions.WIN_SHORTCUT_HINT);
 140             add(CLIOptions.CREATE_INSTALLER,
 141                     CLIOptions.WIN_PER_USER_INSTALLATION);
 142             add(CLIOptions.CREATE_INSTALLER, CLIOptions.WIN_DIR_CHOOSER);
 143             add(CLIOptions.CREATE_INSTALLER, CLIOptions.WIN_REGISTRY_NAME);
 144             add(CLIOptions.CREATE_INSTALLER, CLIOptions.WIN_UPGRADE_UUID);
 145             add(CLIOptions.CREATE_INSTALLER, CLIOptions.CATEGORY);
 146             add(CLIOptions.CREATE_INSTALLER, CLIOptions.WIN_CONSOLE_HINT);
 147         }
 148 
 149         argsInitialized = true;
 150     }
 151 
 152     static void add(CLIOptions mode, CLIOptions arg) {
 153         if (mode.equals(arg)) {
 154             return;
 155         }
 156         options.computeIfAbsent(mode,
 157                     k -> new HashSet<>()).add(arg);
 158     }
 159 
 160     static boolean checkIfSupported(CLIOptions mode, CLIOptions arg) {
 161         if (mode.equals(arg)) {
 162             return true;
 163         }
 164 
 165         initArgs();
 166         Set<CLIOptions> set = options.get(mode);
 167         if (set != null) {
 168             return set.contains(arg);
 169         }
 170         return false;
 171     }
 172 
 173     static boolean checkIfOtherSupported(CLIOptions mode, CLIOptions arg) {
 174         for (CLIOptions other : options.keySet()) {
 175             if (!other.equals(mode)) {
 176                 if (checkIfSupported(other, arg)) {
 177                     return true;
 178                 }
 179             }
 180         }
 181         return false;
 182     }
 183 }