1 /*
   2  * Copyright (c) 2018, 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.jpackager.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.jpackager.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 public class ValidOptions {
  46 
  47     private ValidOptions() {};
  48 
  49     // multimap that contains pairs of (mode, supported args)
  50     private static final Map<CLIOptions, Set<CLIOptions>> options =
  51             new HashMap<>();
  52 
  53     private static boolean argsInitialized = false;
  54 
  55     // initializing list of mandatory arguments
  56     private static void initArgs() {
  57         if (argsInitialized) {
  58             return;
  59         }
  60 
  61         // add options for CREATE_IMAGE
  62         add(CLIOptions.CREATE_IMAGE, CLIOptions.INPUT);
  63         add(CLIOptions.CREATE_IMAGE, CLIOptions.OUTPUT);
  64         add(CLIOptions.CREATE_IMAGE, CLIOptions.APPCLASS);
  65         add(CLIOptions.CREATE_IMAGE, CLIOptions.SINGLETON);
  66         add(CLIOptions.CREATE_IMAGE, CLIOptions.NAME);
  67         add(CLIOptions.CREATE_IMAGE, CLIOptions.IDENTIFIER);
  68         add(CLIOptions.CREATE_IMAGE, CLIOptions.VERBOSE);
  69         add(CLIOptions.CREATE_IMAGE, CLIOptions.FORCE);
  70         add(CLIOptions.CREATE_IMAGE, CLIOptions.FILES);
  71         add(CLIOptions.CREATE_IMAGE, CLIOptions.ARGUMENTS);
  72         add(CLIOptions.CREATE_IMAGE, CLIOptions.STRIP_NATIVE_COMMANDS);
  73         add(CLIOptions.CREATE_IMAGE, CLIOptions.ICON);
  74         add(CLIOptions.CREATE_IMAGE, CLIOptions.VERSION);
  75         add(CLIOptions.CREATE_IMAGE, CLIOptions.JVM_ARGS);
  76         add(CLIOptions.CREATE_IMAGE, CLIOptions.SECONDARY_LAUNCHER);
  77         add(CLIOptions.CREATE_IMAGE, CLIOptions.BUILD_ROOT);
  78         add(CLIOptions.CREATE_IMAGE, CLIOptions.PREDEFINED_RUNTIME_IMAGE);
  79         add(CLIOptions.CREATE_IMAGE, CLIOptions.MAIN_JAR);
  80         add(CLIOptions.CREATE_IMAGE, CLIOptions.MODULE);
  81         add(CLIOptions.CREATE_IMAGE, CLIOptions.ADD_MODULES);
  82         add(CLIOptions.CREATE_IMAGE, CLIOptions.MODULE_PATH);
  83         add(CLIOptions.CREATE_IMAGE, CLIOptions.LIMIT_MODULES);
  84 
  85         if (Platform.getPlatform() == Platform.MAC) {
  86             add(CLIOptions.CREATE_IMAGE, CLIOptions.MAC_SIGN);
  87             add(CLIOptions.CREATE_IMAGE, CLIOptions.MAC_BUNDLE_NAME);
  88             add(CLIOptions.CREATE_IMAGE, CLIOptions.MAC_BUNDLE_IDENTIFIER);
  89             add(CLIOptions.CREATE_IMAGE, CLIOptions.MAC_BUNDLE_SIGNING_PREFIX);
  90             add(CLIOptions.CREATE_IMAGE, CLIOptions.MAC_SIGNING_KEY_NAME);
  91             add(CLIOptions.CREATE_IMAGE, CLIOptions.MAC_SIGNING_KEYCHAIN);
  92             add(CLIOptions.CREATE_IMAGE, CLIOptions.CATEGORY);
  93             add(CLIOptions.CREATE_IMAGE, CLIOptions.COPYRIGHT);
  94         }
  95 
  96         if (Platform.getPlatform() == Platform.WINDOWS) {
  97             add(CLIOptions.CREATE_IMAGE, CLIOptions.DESCRIPTION);
  98             add(CLIOptions.CREATE_IMAGE, CLIOptions.VENDOR);
  99             add(CLIOptions.CREATE_IMAGE, CLIOptions.COPYRIGHT);
 100             add(CLIOptions.CREATE_IMAGE, CLIOptions.WIN_CONSOLE_HINT);
 101         }
 102 
 103         // add options for CREATE_INSTALLER
 104 
 105         // add all CREATE_IMAGE options for CREATE_JRE_INSTALLER
 106         Set<CLIOptions> imageOptions = options.get(CLIOptions.CREATE_IMAGE);
 107         imageOptions.forEach(o -> add(CLIOptions.CREATE_INSTALLER, o));
 108 
 109         add(CLIOptions.CREATE_INSTALLER, CLIOptions.LICENSE_FILE);
 110         add(CLIOptions.CREATE_INSTALLER, CLIOptions.FILE_ASSOCIATIONS);
 111         add(CLIOptions.CREATE_INSTALLER, CLIOptions.INSTALL_DIR);
 112         add(CLIOptions.CREATE_INSTALLER, CLIOptions.PREDEFINED_APP_IMAGE);
 113 
 114         if (Platform.getPlatform() == Platform.MAC) {
 115             add(CLIOptions.CREATE_INSTALLER, CLIOptions.MAC_APP_STORE_CATEGORY);
 116             add(CLIOptions.CREATE_INSTALLER,
 117                     CLIOptions.MAC_APP_STORE_ENTITLEMENTS);
 118         }
 119 
 120         if (Platform.getPlatform() == Platform.LINUX) {
 121             add(CLIOptions.CREATE_INSTALLER, CLIOptions.LINUX_BUNDLE_NAME);
 122             add(CLIOptions.CREATE_INSTALLER, CLIOptions.LINUX_DEB_MAINTAINER);
 123             add(CLIOptions.CREATE_INSTALLER, CLIOptions.LINUX_RPM_LICENSE_TYPE);
 124             add(CLIOptions.CREATE_INSTALLER,
 125                     CLIOptions.LINUX_PACKAGE_DEPENDENCIES);
 126             add(CLIOptions.CREATE_INSTALLER, CLIOptions.DESCRIPTION);
 127             add(CLIOptions.CREATE_INSTALLER, CLIOptions.VENDOR);
 128             add(CLIOptions.CREATE_INSTALLER, CLIOptions.CATEGORY);
 129             add(CLIOptions.CREATE_INSTALLER, CLIOptions.COPYRIGHT);
 130         }
 131 
 132         if (Platform.getPlatform() == Platform.WINDOWS) {
 133             add(CLIOptions.CREATE_INSTALLER, CLIOptions.WIN_MENU_HINT);
 134             add(CLIOptions.CREATE_INSTALLER, CLIOptions.WIN_MENU_GROUP);
 135             add(CLIOptions.CREATE_INSTALLER, CLIOptions.WIN_SHORTCUT_HINT);
 136             add(CLIOptions.CREATE_INSTALLER,
 137                     CLIOptions.WIN_PER_USER_INSTALLATION);
 138             add(CLIOptions.CREATE_INSTALLER, CLIOptions.WIN_DIR_CHOOSER);
 139             add(CLIOptions.CREATE_INSTALLER, CLIOptions.WIN_REGISTRY_NAME);
 140             add(CLIOptions.CREATE_INSTALLER, CLIOptions.WIN_MSI_UPGRADE_UUID);
 141             add(CLIOptions.CREATE_INSTALLER, CLIOptions.CATEGORY);
 142             add(CLIOptions.CREATE_INSTALLER, CLIOptions.WIN_CONSOLE_HINT);
 143         }
 144 
 145         // add options for CREATE_JRE_INSTALLER
 146 
 147         add(CLIOptions.CREATE_JRE_INSTALLER, CLIOptions.INPUT);
 148         add(CLIOptions.CREATE_JRE_INSTALLER, CLIOptions.OUTPUT);
 149         add(CLIOptions.CREATE_JRE_INSTALLER, CLIOptions.NAME);
 150         add(CLIOptions.CREATE_JRE_INSTALLER, CLIOptions.VERBOSE);
 151         add(CLIOptions.CREATE_JRE_INSTALLER, CLIOptions.FILES);
 152         add(CLIOptions.CREATE_JRE_INSTALLER, CLIOptions.STRIP_NATIVE_COMMANDS);
 153         add(CLIOptions.CREATE_JRE_INSTALLER, CLIOptions.LICENSE_FILE);
 154         add(CLIOptions.CREATE_JRE_INSTALLER, CLIOptions.VERSION);
 155         add(CLIOptions.CREATE_JRE_INSTALLER, CLIOptions.BUILD_ROOT);
 156         add(CLIOptions.CREATE_JRE_INSTALLER, CLIOptions.INSTALL_DIR);
 157         add(CLIOptions.CREATE_JRE_INSTALLER,
 158                     CLIOptions.PREDEFINED_RUNTIME_IMAGE);
 159         add(CLIOptions.CREATE_JRE_INSTALLER, CLIOptions.ADD_MODULES);
 160         add(CLIOptions.CREATE_JRE_INSTALLER, CLIOptions.MODULE_PATH);
 161         add(CLIOptions.CREATE_JRE_INSTALLER, CLIOptions.LIMIT_MODULES);
 162 
 163         if (Platform.getPlatform() == Platform.MAC) {
 164             add(CLIOptions.CREATE_JRE_INSTALLER, CLIOptions.MAC_SIGN);
 165             add(CLIOptions.CREATE_JRE_INSTALLER, CLIOptions.MAC_BUNDLE_NAME);
 166             add(CLIOptions.CREATE_JRE_INSTALLER,
 167                     CLIOptions.MAC_BUNDLE_IDENTIFIER);
 168             add(CLIOptions.CREATE_JRE_INSTALLER,
 169                     CLIOptions.MAC_BUNDLE_SIGNING_PREFIX);
 170             add(CLIOptions.CREATE_JRE_INSTALLER,
 171                     CLIOptions.MAC_SIGNING_KEY_NAME);
 172             add(CLIOptions.CREATE_JRE_INSTALLER,
 173                     CLIOptions.MAC_SIGNING_KEYCHAIN);
 174         }
 175 
 176         if (Platform.getPlatform() == Platform.WINDOWS) {
 177             add(CLIOptions.CREATE_JRE_INSTALLER,
 178                     CLIOptions.WIN_PER_USER_INSTALLATION);
 179             add(CLIOptions.CREATE_JRE_INSTALLER, CLIOptions.WIN_DIR_CHOOSER);
 180             add(CLIOptions.CREATE_JRE_INSTALLER,
 181                     CLIOptions.WIN_MSI_UPGRADE_UUID);
 182             add(CLIOptions.CREATE_JRE_INSTALLER, CLIOptions.DESCRIPTION);
 183             add(CLIOptions.CREATE_JRE_INSTALLER, CLIOptions.VENDOR);
 184         }
 185 
 186         if (Platform.getPlatform() == Platform.LINUX) {
 187             add(CLIOptions.CREATE_JRE_INSTALLER, CLIOptions.LINUX_BUNDLE_NAME);
 188             add(CLIOptions.CREATE_JRE_INSTALLER,
 189                     CLIOptions.LINUX_DEB_MAINTAINER);
 190             add(CLIOptions.CREATE_JRE_INSTALLER,
 191                     CLIOptions.LINUX_PACKAGE_DEPENDENCIES);
 192             add(CLIOptions.CREATE_JRE_INSTALLER, CLIOptions.DESCRIPTION);
 193             add(CLIOptions.CREATE_JRE_INSTALLER, CLIOptions.VENDOR);
 194             add(CLIOptions.CREATE_JRE_INSTALLER,
 195                     CLIOptions.LINUX_RPM_LICENSE_TYPE);
 196         }
 197 
 198         argsInitialized = true;
 199     }
 200 
 201     public static void add(CLIOptions mode, CLIOptions arg) {
 202         if (mode.equals(arg)) {
 203             return;
 204         }
 205         options.computeIfAbsent(mode,
 206                     k -> new HashSet<>()).add(arg);
 207     }
 208 
 209     public static boolean checkIfSupported(CLIOptions mode, CLIOptions arg) {
 210         if (mode.equals(arg)) {
 211             return true;
 212         }
 213 
 214         initArgs();
 215         Set<CLIOptions> set = options.get(mode);
 216         if (set != null) {
 217             return set.contains(arg);
 218         }
 219         return false;
 220     }
 221 }