1 /*
   2  * Copyright (c) 2014, 2016, 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 com.oracle.tools.packager;
  27 
  28 import jdk.packager.internal.JLinkBundlerHelper;
  29 
  30 import com.sun.javafx.tools.packager.bundlers.BundleParams;
  31 
  32 import java.io.File;
  33 import java.io.IOException;
  34 import java.io.StringReader;
  35 import java.nio.file.Files;
  36 import java.nio.file.Path;
  37 import java.nio.file.Paths;
  38 import java.text.MessageFormat;
  39 import java.util.ArrayList;
  40 import java.util.Arrays;
  41 import java.util.Collections;
  42 import java.util.Date;
  43 import java.util.HashMap;
  44 import java.util.HashSet;
  45 import java.util.LinkedHashSet;
  46 import java.util.List;
  47 import java.util.Map;
  48 import java.util.Optional;
  49 import java.util.Properties;
  50 import java.util.ResourceBundle;
  51 import java.util.Set;
  52 import java.util.function.BiFunction;
  53 import java.util.function.Function;
  54 import java.util.jar.Attributes;
  55 import java.util.jar.JarFile;
  56 import java.util.jar.Manifest;
  57 import java.util.regex.Pattern;
  58 import java.util.stream.Collectors;
  59 import static jdk.packager.internal.JLinkBundlerHelper.findPathOfModule;
  60 import static jdk.packager.internal.JLinkBundlerHelper.listOfPathToString;
  61 
  62 public class StandardBundlerParam<T> extends BundlerParamInfo<T> {
  63 
  64     public static final String MANIFEST_JAVAFX_MAIN ="JavaFX-Application-Class";
  65     public static final String MANIFEST_PRELOADER = "JavaFX-Preloader-Class";
  66 
  67     private static final ResourceBundle I18N =
  68             ResourceBundle.getBundle(StandardBundlerParam.class.getName());
  69 
  70     public StandardBundlerParam(String name, String description, String id,
  71                                 Class<T> valueType,
  72                                 Function<Map<String, ? super Object>, T> defaultValueFunction,
  73                                 BiFunction<String, Map<String, ? super Object>, T> stringConverter) {
  74         this.name = name;
  75         this.description = description;
  76         this.id = id;
  77         this.valueType = valueType;
  78         this.defaultValueFunction = defaultValueFunction;
  79         this.stringConverter = stringConverter;
  80     }
  81 
  82     public static final StandardBundlerParam<RelativeFileSet> APP_RESOURCES =
  83             new StandardBundlerParam<>(
  84                     I18N.getString("param.app-resources.name"),
  85                     I18N.getString("param.app-resource.description"),
  86                     BundleParams.PARAM_APP_RESOURCES,
  87                     RelativeFileSet.class,
  88                     null, // no default.  Required parameter
  89                     null // no string translation, tool must provide complex type
  90             );
  91 
  92     @SuppressWarnings("unchecked")
  93     public static final StandardBundlerParam<List<RelativeFileSet>> APP_RESOURCES_LIST =
  94             new StandardBundlerParam<>(
  95                     I18N.getString("param.app-resources-list.name"),
  96                     I18N.getString("param.app-resource-list.description"),
  97                     BundleParams.PARAM_APP_RESOURCES + "List",
  98                     (Class<List<RelativeFileSet>>) (Object) List.class,
  99                     p -> new ArrayList<>(Collections.singletonList(APP_RESOURCES.fetchFrom(p))), // Default is appResources, as a single item list
 100                     StandardBundlerParam::createAppResourcesListFromString
 101             );
 102 
 103     @SuppressWarnings("unchecked")
 104     public static final StandardBundlerParam<String> SOURCE_DIR =
 105             new StandardBundlerParam<>(
 106                     I18N.getString("param.source-dir.name"),
 107                     I18N.getString("param.source-dir.description"),
 108                     "srcdir",
 109                     String.class,
 110                     p -> null,
 111                     (s, p) -> {
 112                         String value = String.valueOf(s);
 113                         if (value.charAt(value.length() - 1) == File.separatorChar) {
 114                             return value.substring(0, value.length() - 1);
 115                         }
 116                         else {
 117                             return value;
 118                         }
 119                     }
 120             );
 121 
 122     // note that each bundler is likely to replace this one with their own converter
 123     public static final StandardBundlerParam<RelativeFileSet> MAIN_JAR =
 124             new StandardBundlerParam<>(
 125                     I18N.getString("param.main-jar.name"),
 126                     I18N.getString("param.main-jar.description"),
 127                     "mainJar",
 128                     RelativeFileSet.class,
 129                     params -> {
 130                         extractMainClassInfoFromAppResources(params);
 131                         return (RelativeFileSet) params.get("mainJar");
 132                     },
 133                     (s, p) -> getMainJar(s, p)
 134             );
 135 
 136     public static final StandardBundlerParam<String> CLASSPATH =
 137             new StandardBundlerParam<>(
 138                     I18N.getString("param.classpath.name"),
 139                     I18N.getString("param.classpath.description"),
 140                     "classpath",
 141                     String.class,
 142                     params -> {
 143                         extractMainClassInfoFromAppResources(params);
 144                         String cp = (String) params.get("classpath");
 145                         return cp == null ? "" : cp;
 146                     },
 147                     (s, p) -> s.replace(File.pathSeparator, " ")
 148             );
 149 
 150     public static final StandardBundlerParam<String> MAIN_CLASS =
 151             new StandardBundlerParam<>(
 152                     I18N.getString("param.main-class.name"),
 153                     I18N.getString("param.main-class.description"),
 154                     BundleParams.PARAM_APPLICATION_CLASS,
 155                     String.class,
 156                     params -> {
 157                         //FIXME sniff modules
 158                         extractMainClassInfoFromAppResources(params);
 159                         String s = (String) params.get(BundleParams.PARAM_APPLICATION_CLASS);
 160                         if (s == null) {
 161                             s = JLinkBundlerHelper.getMainClass(params);
 162                         }
 163                         return s;
 164                     },
 165                     (s, p) -> s
 166             );
 167 
 168     public static final StandardBundlerParam<String> APP_NAME =
 169             new StandardBundlerParam<>(
 170                     I18N.getString("param.app-name.name"),
 171                     I18N.getString("param.app-name.description"),
 172                     BundleParams.PARAM_NAME,
 173                     String.class,
 174                     params -> {
 175                         String s = MAIN_CLASS.fetchFrom(params);
 176                         if (s == null) return null;
 177 
 178                         int idx = s.lastIndexOf(".");
 179                         if (idx >= 0) {
 180                             return s.substring(idx+1);
 181                         }
 182                         return s;
 183                     },
 184                     (s, p) -> s
 185             );
 186 
 187     private static Pattern TO_FS_NAME = Pattern.compile("\\s|[\\\\/?:*<>|]"); // keep out invalid/undesireable filename characters
 188 
 189     public static final StandardBundlerParam<String> APP_FS_NAME =
 190             new StandardBundlerParam<>(
 191                     I18N.getString("param.app-fs-name.name"),
 192                     I18N.getString("param.app-fs-name.description"),
 193                     "name.fs",
 194                     String.class,
 195                     params -> TO_FS_NAME.matcher(APP_NAME.fetchFrom(params)).replaceAll(""),
 196                     (s, p) -> s
 197             );
 198 
 199     public static final StandardBundlerParam<File> ICON =
 200             new StandardBundlerParam<>(
 201                     I18N.getString("param.icon-file.name"),
 202                     I18N.getString("param.icon-file.description"),
 203                     BundleParams.PARAM_ICON,
 204                     File.class,
 205                     params -> null,
 206                     (s, p) -> new File(s)
 207             );
 208 
 209     public static final StandardBundlerParam<String> VENDOR =
 210             new StandardBundlerParam<>(
 211                     I18N.getString("param.vendor.name"),
 212                     I18N.getString("param.vendor.description"),
 213                     BundleParams.PARAM_VENDOR,
 214                     String.class,
 215                     params -> I18N.getString("param.vendor.default"),
 216                     (s, p) -> s
 217             );
 218 
 219     public static final StandardBundlerParam<String> CATEGORY =
 220             new StandardBundlerParam<>(
 221                     I18N.getString("param.category.name"),
 222                     I18N.getString("param.category.description"),
 223                     BundleParams.PARAM_CATEGORY,
 224                     String.class,
 225                     params -> I18N.getString("param.category.default"),
 226                     (s, p) -> s
 227             );
 228 
 229     public static final StandardBundlerParam<String> DESCRIPTION =
 230             new StandardBundlerParam<>(
 231                     I18N.getString("param.description.name"),
 232                     I18N.getString("param.description.description"),
 233                     BundleParams.PARAM_DESCRIPTION,
 234                     String.class,
 235                     params -> params.containsKey(APP_NAME.getID())
 236                             ? APP_NAME.fetchFrom(params)
 237                             : I18N.getString("param.description.default"),
 238                     (s, p) -> s
 239             );
 240 
 241     public static final StandardBundlerParam<String> COPYRIGHT =
 242             new StandardBundlerParam<>(
 243                     I18N.getString("param.copyright.name"),
 244                     I18N.getString("param.copyright.description"),
 245                     BundleParams.PARAM_COPYRIGHT,
 246                     String.class,
 247                     params -> MessageFormat.format(I18N.getString("param.copyright.default"), new Date()),
 248                     (s, p) -> s
 249             );
 250 
 251     public static final StandardBundlerParam<Boolean> USE_FX_PACKAGING =
 252             new StandardBundlerParam<>(
 253                     I18N.getString("param.use-javafx-packaging.name"),
 254                     I18N.getString("param.use-javafx-packaging.description"),
 255                     "fxPackaging",
 256                     Boolean.class,
 257                     params -> {
 258                         extractMainClassInfoFromAppResources(params);
 259                         Boolean result = (Boolean) params.get("fxPackaging");
 260                         return (result == null) ? Boolean.FALSE : result;
 261                     },
 262                     (s, p) -> Boolean.valueOf(s)
 263             );
 264 
 265     @SuppressWarnings("unchecked")
 266     public static final StandardBundlerParam<List<String>> ARGUMENTS =
 267             new StandardBundlerParam<>(
 268                     I18N.getString("param.arguments.name"),
 269                     I18N.getString("param.arguments.description"),
 270                     "arguments",
 271                     (Class<List<String>>) (Object) List.class,
 272                     params -> Collections.emptyList(),
 273                     (s, p) -> splitStringWithEscapes(s)
 274             );
 275 
 276     @SuppressWarnings("unchecked")
 277     public static final StandardBundlerParam<List<String>> JVM_OPTIONS =
 278             new StandardBundlerParam<>(
 279                     I18N.getString("param.jvm-options.name"),
 280                     I18N.getString("param.jvm-options.description"),
 281                     "jvmOptions",
 282                     (Class<List<String>>) (Object) List.class,
 283                     params -> Collections.emptyList(),
 284                     (s, p) -> Arrays.asList(s.split("\\s+"))
 285             );
 286 
 287     @SuppressWarnings("unchecked")
 288     public static final StandardBundlerParam<Map<String, String>> JVM_PROPERTIES =
 289             new StandardBundlerParam<>(
 290                     I18N.getString("param.jvm-system-properties.name"),
 291                     I18N.getString("param.jvm-system-properties.description"),
 292                     "jvmProperties",
 293                     (Class<Map<String, String>>) (Object) Map.class,
 294                     params -> Collections.emptyMap(),
 295                     (s, params) -> {
 296                         Map<String, String> map = new HashMap<>();
 297                         try {
 298                             Properties p = new Properties();
 299                             p.load(new StringReader(s));
 300                             for (Map.Entry<Object, Object> entry : p.entrySet()) {
 301                                 map.put((String)entry.getKey(), (String)entry.getValue());
 302                             }
 303                         } catch (IOException e) {
 304                             e.printStackTrace();
 305                         }
 306                         return map;
 307                     }
 308             );
 309 
 310     @SuppressWarnings("unchecked")
 311     public static final StandardBundlerParam<Map<String, String>> USER_JVM_OPTIONS =
 312             new StandardBundlerParam<>(
 313                     I18N.getString("param.user-jvm-options.name"),
 314                     I18N.getString("param.user-jvm-options.description"),
 315                     "userJvmOptions",
 316                     (Class<Map<String, String>>) (Object) Map.class,
 317                     params -> Collections.emptyMap(),
 318                     (s, params) -> {
 319                         Map<String, String> map = new HashMap<>();
 320                         try {
 321                             Properties p = new Properties();
 322                             p.load(new StringReader(s));
 323                             for (Map.Entry<Object, Object> entry : p.entrySet()) {
 324                                 map.put((String)entry.getKey(), (String)entry.getValue());
 325                             }
 326                         } catch (IOException e) {
 327                             e.printStackTrace();
 328                         }
 329                         return map;
 330                     }
 331             );
 332 
 333     public static final StandardBundlerParam<String> TITLE =
 334             new StandardBundlerParam<>(
 335                     I18N.getString("param.title.name"),
 336                     I18N.getString("param.title.description"), //?? but what does it do?
 337                     BundleParams.PARAM_TITLE,
 338                     String.class,
 339                     APP_NAME::fetchFrom,
 340                     (s, p) -> s
 341             );
 342 
 343     // note that each bundler is likely to replace this one with their own converter
 344     public static final StandardBundlerParam<String> VERSION =
 345             new StandardBundlerParam<>(
 346                     I18N.getString("param.version.name"),
 347                     I18N.getString("param.version.description"),
 348                     BundleParams.PARAM_VERSION,
 349                     String.class,
 350                     params -> I18N.getString("param.version.default"),
 351                     (s, p) -> s
 352             );
 353 
 354     public static final StandardBundlerParam<Boolean> SYSTEM_WIDE =
 355             new StandardBundlerParam<>(
 356                     I18N.getString("param.system-wide.name"),
 357                     I18N.getString("param.system-wide.description"),
 358                     BundleParams.PARAM_SYSTEM_WIDE,
 359                     Boolean.class,
 360                     params -> null,
 361                     // valueOf(null) is false, and we actually do want null in some cases
 362                     (s, p) -> (s == null || "null".equalsIgnoreCase(s))? null : Boolean.valueOf(s)
 363             );
 364 
 365     public static final StandardBundlerParam<Boolean> SERVICE_HINT  =
 366             new StandardBundlerParam<>(
 367                     I18N.getString("param.service-hint.name"),
 368                     I18N.getString("param.service-hint.description"),
 369                     BundleParams.PARAM_SERVICE_HINT,
 370                     Boolean.class,
 371                     params -> false,
 372                     (s, p) -> (s == null || "null".equalsIgnoreCase(s))? false : Boolean.valueOf(s)
 373             );
 374 
 375     public static final StandardBundlerParam<Boolean> START_ON_INSTALL  =
 376             new StandardBundlerParam<>(
 377                     I18N.getString("param.start-on-install.name"),
 378                     I18N.getString("param.start-on-install.description"),
 379                     "startOnInstall",
 380                     Boolean.class,
 381                     params -> false,
 382                     (s, p) -> (s == null || "null".equalsIgnoreCase(s))? false : Boolean.valueOf(s)
 383             );
 384 
 385     public static final StandardBundlerParam<Boolean> STOP_ON_UNINSTALL  =
 386             new StandardBundlerParam<>(
 387                     I18N.getString("param.stop-on-uninstall.name"),
 388                     I18N.getString("param.stop-on-uninstall.description"),
 389                     "stopOnUninstall",
 390                     Boolean.class,
 391                     params -> true,
 392                     (s, p) -> (s == null || "null".equalsIgnoreCase(s))? true : Boolean.valueOf(s)
 393             );
 394 
 395     public static final StandardBundlerParam<Boolean> RUN_AT_STARTUP  =
 396             new StandardBundlerParam<>(
 397                     I18N.getString("param.run-at-startup.name"),
 398                     I18N.getString("param.run-at-startup.description"),
 399                     "runAtStartup",
 400                     Boolean.class,
 401                     params -> false,
 402                     (s, p) -> (s == null || "null".equalsIgnoreCase(s))? false : Boolean.valueOf(s)
 403             );
 404 
 405     public static final StandardBundlerParam<Boolean> SIGN_BUNDLE  =
 406             new StandardBundlerParam<>(
 407                     I18N.getString("param.sign-bundle.name"),
 408                     I18N.getString("param.sign-bundle.description"),
 409                     "signBundle",
 410                     Boolean.class,
 411                     params -> null,
 412                     // valueOf(null) is false, and we actually do want null in some cases
 413                     (s, p) -> (s == null || "null".equalsIgnoreCase(s))? null : Boolean.valueOf(s)
 414             );
 415 
 416     public static final StandardBundlerParam<Boolean> SHORTCUT_HINT =
 417             new StandardBundlerParam<>(
 418                     I18N.getString("param.desktop-shortcut-hint.name"),
 419                     I18N.getString("param.desktop-shortcut-hint.description"),
 420                     BundleParams.PARAM_SHORTCUT,
 421                     Boolean.class,
 422                     params -> false,
 423                     // valueOf(null) is false, and we actually do want null in some cases
 424                     (s, p) -> (s == null || "null".equalsIgnoreCase(s))? false : Boolean.valueOf(s)
 425             );
 426 
 427     public static final StandardBundlerParam<Boolean> MENU_HINT =
 428             new StandardBundlerParam<>(
 429                     I18N.getString("param.menu-shortcut-hint.name"),
 430                     I18N.getString("param.menu-shortcut-hint.description"),
 431                     BundleParams.PARAM_MENU,
 432                     Boolean.class,
 433                     params -> false,
 434                     // valueOf(null) is false, and we actually do want null in some cases
 435                     (s, p) -> (s == null || "null".equalsIgnoreCase(s))? true : Boolean.valueOf(s)
 436             );
 437 
 438     @SuppressWarnings("unchecked")
 439     public static final StandardBundlerParam<List<String>> LICENSE_FILE =
 440             new StandardBundlerParam<>(
 441                     I18N.getString("param.license-file.name"),
 442                     I18N.getString("param.license-file.description"),
 443                     BundleParams.PARAM_LICENSE_FILE,
 444                     (Class<List<String>>)(Object)List.class,
 445                     params -> Collections.<String>emptyList(),
 446                     (s, p) -> Arrays.asList(s.split(","))
 447             );
 448 
 449     public static final BundlerParamInfo<String> LICENSE_TYPE =
 450             new StandardBundlerParam<>(
 451                     I18N.getString("param.license-type.name"),
 452                     I18N.getString("param.license-type.description"),
 453                     BundleParams.PARAM_LICENSE_TYPE,
 454                     String.class,
 455                     params -> I18N.getString("param.license-type.default"),
 456                     (s, p) -> s
 457             );
 458 
 459     public static final StandardBundlerParam<File> BUILD_ROOT =
 460             new StandardBundlerParam<>(
 461                     I18N.getString("param.build-root.name"),
 462                     I18N.getString("param.build-root.description"),
 463                     "buildRoot",
 464                     File.class,
 465                     params -> {
 466                         try {
 467                             return Files.createTempDirectory("fxbundler").toFile();
 468                         } catch (IOException ioe) {
 469                             return null;
 470                         }
 471                     },
 472                     (s, p) -> new File(s)
 473             );
 474 
 475     public static final StandardBundlerParam<String> IDENTIFIER =
 476             new StandardBundlerParam<>(
 477                     I18N.getString("param.identifier.name"),
 478                     I18N.getString("param.identifier.description"),
 479                     BundleParams.PARAM_IDENTIFIER,
 480                     String.class,
 481                     params -> {
 482                         String s = MAIN_CLASS.fetchFrom(params);
 483                         if (s == null) return null;
 484 
 485                         int idx = s.lastIndexOf(".");
 486                         if (idx >= 1) {
 487                             return s.substring(0, idx);
 488                         }
 489                         return s;
 490                     },
 491                     (s, p) -> s
 492             );
 493 
 494     public static final StandardBundlerParam<String> PREFERENCES_ID =
 495             new StandardBundlerParam<>(
 496                     I18N.getString("param.preferences-id.name"),
 497                     I18N.getString("param.preferences-id.description"),
 498                     "preferencesID",
 499                     String.class,
 500                     p -> Optional.ofNullable(IDENTIFIER.fetchFrom(p)).orElse("").replace('.', '/'),
 501                     (s, p) -> s
 502             );
 503 
 504     public static final StandardBundlerParam<String> PRELOADER_CLASS =
 505             new StandardBundlerParam<>(
 506                     I18N.getString("param.preloader.name"),
 507                     I18N.getString("param.preloader.description"),
 508                     "preloader",
 509                     String.class,
 510                     p -> null,
 511                     null
 512             );
 513 
 514     public static final StandardBundlerParam<Boolean> VERBOSE  =
 515             new StandardBundlerParam<>(
 516                     I18N.getString("param.verbose.name"),
 517                     I18N.getString("param.verbose.description"),
 518                     "verbose",
 519                     Boolean.class,
 520                     params -> false,
 521                     // valueOf(null) is false, and we actually do want null in some cases
 522                     (s, p) -> (s == null || "null".equalsIgnoreCase(s))? true : Boolean.valueOf(s)
 523             );
 524 
 525     public static final StandardBundlerParam<File> DROP_IN_RESOURCES_ROOT =
 526             new StandardBundlerParam<>(
 527                     I18N.getString("param.drop-in-resources-root.name"),
 528                     I18N.getString("param.drop-in-resources-root.description"),
 529                     "dropinResourcesRoot",
 530                     File.class,
 531                     params -> null,
 532                     (s, p) -> new File(s)
 533             );
 534 
 535     @SuppressWarnings("unchecked")
 536     public static final StandardBundlerParam<List<Map<String, ? super Object>>> SECONDARY_LAUNCHERS =
 537             new StandardBundlerParam<>(
 538                     I18N.getString("param.secondary-launchers.name"),
 539                     I18N.getString("param.secondary-launchers.description"),
 540                     "secondaryLaunchers",
 541                     (Class<List<Map<String, ? super Object>>>) (Object) List.class,
 542                     params -> new ArrayList<>(1),
 543                     // valueOf(null) is false, and we actually do want null in some cases
 544                     (s, p) -> null
 545             );
 546 
 547     @SuppressWarnings("unchecked")
 548     public static final StandardBundlerParam<List<Map<String, ? super Object>>> FILE_ASSOCIATIONS =
 549             new StandardBundlerParam<>(
 550                     I18N.getString("param.file-associations.name"),
 551                     I18N.getString("param.file-associations.description"),
 552                     "fileAssociations",
 553                     (Class<List<Map<String, ? super Object>>>) (Object) List.class,
 554                     params -> new ArrayList<>(1),
 555                     // valueOf(null) is false, and we actually do want null in some cases
 556                     (s, p) -> null
 557             );
 558 
 559     @SuppressWarnings("unchecked")
 560     public static final StandardBundlerParam<List<String>> FA_EXTENSIONS =
 561             new StandardBundlerParam<>(
 562                     I18N.getString("param.fa-extension.name"),
 563                     I18N.getString("param.fa-extension.description"),
 564                     "fileAssociation.extension",
 565                     (Class<List<String>>) (Object) List.class,
 566                     params -> null, // null means not matched to an extension
 567                     (s, p) -> Arrays.asList(s.split("(,|\\s)+"))
 568             );
 569 
 570     @SuppressWarnings("unchecked")
 571     public static final StandardBundlerParam<List<String>> FA_CONTENT_TYPE =
 572             new StandardBundlerParam<>(
 573                     I18N.getString("param.fa-content-type.name"),
 574                     I18N.getString("param.fa-content-type.description"),
 575                     "fileAssociation.contentType",
 576                     (Class<List<String>>) (Object) List.class,
 577                     params -> null, // null means not matched to a content/mime type
 578                     (s, p) -> Arrays.asList(s.split("(,|\\s)+"))
 579             );
 580 
 581     public static final StandardBundlerParam<String> FA_DESCRIPTION =
 582             new StandardBundlerParam<>(
 583                     I18N.getString("param.fa-description.name"),
 584                     I18N.getString("param.fa-description.description"),
 585                     "fileAssociation.description",
 586                     String.class,
 587                     params -> APP_NAME.fetchFrom(params) + " File",
 588                     null
 589             );
 590 
 591     public static final StandardBundlerParam<File> FA_ICON =
 592             new StandardBundlerParam<>(
 593                     I18N.getString("param.fa-icon.name"),
 594                     I18N.getString("param.fa-icon.description"),
 595                     "fileAssociation.icon",
 596                     File.class,
 597                     ICON::fetchFrom,
 598                     (s, p) -> new File(s)
 599             );
 600 
 601     public static final StandardBundlerParam<Boolean> UNLOCK_COMMERCIAL_FEATURES =
 602             new StandardBundlerParam<>(
 603                     I18N.getString("param.commercial-features.name"),
 604                     I18N.getString("param.commercial-features.description"),
 605                     "commercialFeatures",
 606                     Boolean.class,
 607                     p -> false,
 608                     (s, p) -> Boolean.parseBoolean(s)
 609             );
 610 
 611     public static final StandardBundlerParam<Boolean> ENABLE_APP_CDS =
 612             new StandardBundlerParam<>(
 613                     I18N.getString("param.com-app-cds.name"),
 614                     I18N.getString("param.com-app-cds.description"),
 615                     "commercial.AppCDS",
 616                     Boolean.class,
 617                     p -> false,
 618                     (s, p) -> Boolean.parseBoolean(s)
 619             );
 620 
 621     public static final StandardBundlerParam<String> APP_CDS_CACHE_MODE =
 622             new StandardBundlerParam<>(
 623                     I18N.getString("param.com-app-cds-cache-mode.name"),
 624                     I18N.getString("param.com-app-cds-cache-mode.description"),
 625                     "commercial.AppCDS.cache",
 626                     String.class,
 627                     p -> "auto",
 628                     (s, p) -> s
 629             );
 630 
 631     @SuppressWarnings("unchecked")
 632     public static final StandardBundlerParam<List<String>> APP_CDS_CLASS_ROOTS =
 633             new StandardBundlerParam<>(
 634                     I18N.getString("param.com-app-cds-root.name"),
 635                     I18N.getString("param.com-app-cds-root.description"),
 636                     "commercial.AppCDS.classRoots",
 637                     (Class<List<String>>)((Object)List.class),
 638                     p -> Collections.singletonList(MAIN_CLASS.fetchFrom(p)),
 639                     (s, p) -> Arrays.asList(s.split("[ ,:]"))
 640             );
 641 
 642     @SuppressWarnings("unchecked")
 643     public static final BundlerParamInfo<List<Path>> MODULE_PATH =
 644             new StandardBundlerParam<>(
 645                     I18N.getString("param.module-path.name"),
 646                     I18N.getString("param.module-path.description"),
 647                     "module-path",
 648                     (Class<List<Path>>) (Object)List.class,
 649                     p -> { return getDefaultModulePath(); },
 650                     (s, p) -> {
 651                         List<Path> modulePath = Arrays.asList(s.split("[;:]")).stream()
 652                                                       .map(ss -> new File(ss).toPath())
 653                                                       .collect(Collectors.toList());
 654                         Path userDefinedJdkModulePath = null;
 655                         if (modulePath != null) {
 656                             userDefinedJdkModulePath = JLinkBundlerHelper.findPathOfModule(modulePath, "java.base.jmod");
 657                         }
 658                         else {
 659                             modulePath = new ArrayList();
 660                         }
 661 
 662                         // Add the default JDK module path to the module path.
 663                         if (userDefinedJdkModulePath == null) {
 664                             List<Path> jdkModulePath = getDefaultModulePath();
 665 
 666                             if (jdkModulePath != null) {
 667                                 modulePath.addAll(jdkModulePath);
 668                             }
 669                         }
 670 
 671                         Path javaBasePath = findPathOfModule(modulePath, "java.base.jmod");
 672 
 673                         if (javaBasePath == null || !javaBasePath.toFile().exists()) {
 674                             com.oracle.tools.packager.Log.info(String.format(I18N.getString("warning.no.jdk.modules.found")));
 675                         }
 676 
 677                         return modulePath;
 678                     });
 679 
 680     @SuppressWarnings("unchecked")
 681     public static final BundlerParamInfo<String> MODULE =
 682             new StandardBundlerParam<>(
 683                     I18N.getString("param.main.module.name"),
 684                     I18N.getString("param.main.module.description"),
 685                     "module",
 686                     String.class,
 687                     p -> null,
 688                     (s, p) -> {
 689                         return String.valueOf(s);
 690                     });
 691 
 692     @SuppressWarnings("unchecked")
 693     public static final BundlerParamInfo<Set<String>> ADD_MODULES =
 694             new StandardBundlerParam<>(
 695                     I18N.getString("param.add-modules.name"),
 696                     I18N.getString("param.add-modules.description"),
 697                     "add-modules",
 698                     (Class<Set<String>>) (Object) Set.class,
 699                     p -> new LinkedHashSet(),
 700                     (s, p) -> new LinkedHashSet<>(Arrays.asList(s.split("[,;: ]+")))
 701             );
 702 
 703     @SuppressWarnings("unchecked")
 704     public static final BundlerParamInfo<Set<String>> LIMIT_MODULES =
 705             new StandardBundlerParam<>(
 706                     I18N.getString("param.limit-modules.name"),
 707                     I18N.getString("param.limit-modules.description"),
 708                     "limit-modules",
 709                     (Class<Set<String>>) (Object) Set.class,
 710                     p -> new LinkedHashSet(),
 711                     (s, p) -> new LinkedHashSet<>(Arrays.asList(s.split("[,;: ]+")))
 712             );
 713 
 714     @SuppressWarnings("unchecked")
 715     public static final BundlerParamInfo<Boolean> STRIP_NATIVE_COMMANDS =
 716             new StandardBundlerParam<>(
 717                     I18N.getString("param.strip-executables.name"),
 718                     I18N.getString("param.strip-executables.description"),
 719                     "strip-native-commands",
 720                     Boolean.class,
 721                     p -> Boolean.TRUE,
 722                     (s, p) -> Boolean.valueOf(s)
 723             );
 724 
 725     public static void extractMainClassInfoFromAppResources(Map<String, ? super Object> params) {
 726         boolean hasMainClass = params.containsKey(MAIN_CLASS.getID());
 727         boolean hasMainJar = params.containsKey(MAIN_JAR.getID());
 728         boolean hasMainJarClassPath = params.containsKey(CLASSPATH.getID());
 729         boolean hasPreloader = params.containsKey(PRELOADER_CLASS.getID());
 730         boolean hasModule = params.containsKey(MODULE.getID());
 731 
 732         if (hasMainClass && hasMainJar && hasMainJarClassPath || hasModule) {
 733             return;
 734         }
 735 
 736         // it's a pair.  The [0] is the srcdir [1] is the file relative to sourcedir
 737         List<String[]> filesToCheck = new ArrayList<>();
 738 
 739         if (hasMainJar) {
 740             RelativeFileSet rfs = MAIN_JAR.fetchFrom(params);
 741             for (String s : rfs.getIncludedFiles()) {
 742                 filesToCheck.add(new String[]{rfs.getBaseDirectory().toString(), s});
 743             }
 744         } else if (hasMainJarClassPath) {
 745             for (String s : CLASSPATH.fetchFrom(params).split("\\s+")) {
 746                 if (APP_RESOURCES.fetchFrom(params) != null) {
 747                     filesToCheck.add(new String[] {APP_RESOURCES.fetchFrom(params).getBaseDirectory().toString(), s});
 748                 }
 749             }
 750         } else {
 751             List<RelativeFileSet> rfsl = APP_RESOURCES_LIST.fetchFrom(params);
 752             if (rfsl == null || rfsl.isEmpty()) {
 753                 return;
 754             }
 755             for (RelativeFileSet rfs : rfsl) {
 756                 if (rfs == null) continue;
 757 
 758                 for (String s : rfs.getIncludedFiles()) {
 759                     filesToCheck.add(new String[]{rfs.getBaseDirectory().toString(), s});
 760                 }
 761             }
 762         }
 763 
 764         String declaredMainClass = (String) params.get(MAIN_CLASS.getID());
 765 
 766         // presume the set iterates in-order
 767         for (String[] fnames : filesToCheck) {
 768             try {
 769                 // only sniff jars
 770                 if (!fnames[1].toLowerCase().endsWith(".jar")) continue;
 771 
 772                 File file = new File(fnames[0], fnames[1]);
 773                 // that actually exist
 774                 if (!file.exists()) continue;
 775 
 776                 try (JarFile jf = new JarFile(file)) {
 777                     Manifest m = jf.getManifest();
 778                     Attributes attrs = (m != null) ? m.getMainAttributes() : null;
 779 
 780                     if (attrs != null) {
 781                         String mainClass = attrs.getValue(Attributes.Name.MAIN_CLASS);
 782                         String fxMain = attrs.getValue(MANIFEST_JAVAFX_MAIN);
 783                         String preloaderClass = attrs.getValue(MANIFEST_PRELOADER);
 784                         if (hasMainClass) {
 785                             if (declaredMainClass.equals(fxMain)) {
 786                                 params.put(USE_FX_PACKAGING.getID(), true);
 787                             } else if (declaredMainClass.equals(mainClass)) {
 788                                 params.put(USE_FX_PACKAGING.getID(), false);
 789                             } else {
 790                                 if (fxMain != null) {
 791                                     Log.info(MessageFormat.format(I18N.getString("message.fx-app-does-not-match-specified-main"), fnames[1], fxMain, declaredMainClass));
 792                                 }
 793                                 if (mainClass != null) {
 794                                     Log.info(MessageFormat.format(I18N.getString("message.main-class-does-not-match-specified-main"), fnames[1], mainClass, declaredMainClass));
 795                                 }
 796                                 continue;
 797                             }
 798                         } else {
 799                             if (fxMain != null) {
 800                                 params.put(USE_FX_PACKAGING.getID(), true);
 801                                 params.put(MAIN_CLASS.getID(), fxMain);
 802                             } else if (mainClass != null) {
 803                                 params.put(USE_FX_PACKAGING.getID(), false);
 804                                 params.put(MAIN_CLASS.getID(), mainClass);
 805                             } else {
 806                                 continue;
 807                             }
 808                         }
 809                         if (!hasPreloader && preloaderClass != null) {
 810                             params.put(PRELOADER_CLASS.getID(), preloaderClass);
 811                         }
 812                         if (!hasMainJar) {
 813                             if (fnames[0] == null) {
 814                                 fnames[0] = file.getParentFile().toString();
 815                             }
 816                             params.put(MAIN_JAR.getID(), new RelativeFileSet(new File(fnames[0]), new LinkedHashSet<>(Collections.singletonList(file))));
 817                         }
 818                         if (!hasMainJarClassPath) {
 819                             String cp = attrs.getValue(Attributes.Name.CLASS_PATH);
 820                             params.put(CLASSPATH.getID(), cp == null ? "" : cp);
 821                         }
 822                         break;
 823                     }
 824                 }
 825             } catch (IOException ignore) {
 826                 ignore.printStackTrace();
 827             }
 828         }
 829     }
 830 
 831     public static void validateMainClassInfoFromAppResources(Map<String, ? super Object> params) throws ConfigException {
 832         boolean hasMainClass = params.containsKey(MAIN_CLASS.getID());
 833         boolean hasMainJar = params.containsKey(MAIN_JAR.getID());
 834         boolean hasMainJarClassPath = params.containsKey(CLASSPATH.getID());
 835         boolean hasModule = params.containsKey(MODULE.getID());
 836 
 837         if (hasMainClass && hasMainJar && hasMainJarClassPath || hasModule) {
 838             return;
 839         }
 840 
 841         extractMainClassInfoFromAppResources(params);
 842 
 843         if (!params.containsKey(MAIN_CLASS.getID())) {
 844             if (hasMainJar) {
 845                 throw new ConfigException(
 846                         MessageFormat.format(I18N.getString("error.no-main-class-with-main-jar"),
 847                                 MAIN_JAR.fetchFrom(params)),
 848                         MessageFormat.format(I18N.getString("error.no-main-class-with-main-jar.advice"),
 849                                 MAIN_JAR.fetchFrom(params)));
 850             } else if (hasMainJarClassPath) {
 851                 throw new ConfigException(
 852                         I18N.getString("error.no-main-class-with-classpath"),
 853                         I18N.getString("error.no-main-class-with-classpath.advice"));
 854             } else {
 855                 throw new ConfigException(
 856                         I18N.getString("error.no-main-class"),
 857                         I18N.getString("error.no-main-class.advice"));
 858             }
 859         }
 860     }
 861 
 862 
 863     private static List<String> splitStringWithEscapes(String s) {
 864         List<String> l = new ArrayList<>();
 865         StringBuilder current = new StringBuilder();
 866         boolean quoted = false;
 867         boolean escaped = false;
 868         for (char c : s.toCharArray()) {
 869             if (escaped) {
 870                 current.append(c);
 871             } else if ('"' == c) {
 872                 quoted = !quoted;
 873             } else if (!quoted && Character.isWhitespace(c)) {
 874                 l.add(current.toString());
 875                 current = new StringBuilder();
 876             } else {
 877                 current.append(c);
 878             }
 879         }
 880         l.add(current.toString());
 881         return l;
 882     }
 883 
 884     private static List<RelativeFileSet> createAppResourcesListFromString(String s, Map<String, ? super Object> objectObjectMap) {
 885         List<RelativeFileSet> result = new ArrayList<>();
 886         for (String path : s.split("[:;]")) {
 887             File f = new File(path);
 888             if (f.getName().equals("*") || path.endsWith("/") || path.endsWith("\\")) {
 889                 if (f.getName().equals("*")) {
 890                     f = f.getParentFile();
 891                 }
 892                 Set<File> theFiles = new HashSet<>();
 893                 try {
 894                     Files.walk(f.toPath())
 895                             .filter(Files::isRegularFile)
 896                             .forEach(p -> theFiles.add(p.toFile()));
 897                 } catch (IOException e) {
 898                     e.printStackTrace();
 899                 }
 900                 result.add(new RelativeFileSet(f, theFiles));
 901             } else {
 902                 result.add(new RelativeFileSet(f.getParentFile(), Collections.singleton(f)));
 903             }
 904         }
 905         return result;
 906     }
 907 
 908     private static RelativeFileSet getMainJar(String moduleName, Map<String, ? super Object> params) {
 909         for (RelativeFileSet rfs : APP_RESOURCES_LIST.fetchFrom(params)) {
 910             File appResourcesRoot = rfs.getBaseDirectory();
 911             File mainJarFile = new File(appResourcesRoot, moduleName);
 912 
 913             if (mainJarFile.exists()) {
 914                 return new RelativeFileSet(appResourcesRoot, new LinkedHashSet<>(Collections.singletonList(mainJarFile)));
 915             }
 916             else {
 917                 List<Path> modulePath = MODULE_PATH.fetchFrom(params);
 918                 Path modularJarPath = JLinkBundlerHelper.findPathOfModule(modulePath, moduleName);
 919 
 920                 if (modularJarPath != null && Files.exists(modularJarPath)) {
 921                     return new RelativeFileSet(appResourcesRoot, new LinkedHashSet<>(Collections.singletonList(modularJarPath.toFile())));
 922                 }
 923             }
 924         }
 925 
 926         throw new IllegalArgumentException(
 927                 new ConfigException(
 928                         MessageFormat.format(I18N.getString("error.main-jar-does-not-exist"), moduleName),
 929                         I18N.getString("error.main-jar-does-not-exist.advice")));
 930     }
 931 
 932     public static List<Path> getDefaultModulePath() {
 933         List<Path> result = new ArrayList();
 934         Path jdkModulePath = Paths.get(System.getProperty("java.home"), "jmods").toAbsolutePath();
 935 
 936         if (jdkModulePath != null && Files.exists(jdkModulePath)) {
 937             result.add(jdkModulePath);
 938         }
 939         else {
 940             jdkModulePath = Paths.get(System.getProperty("java.home"), "../images/jmods").toAbsolutePath();
 941 
 942             if (jdkModulePath != null && Files.exists(jdkModulePath)) {
 943                 result.add(jdkModulePath);
 944             }
 945         }
 946 
 947         return result;
 948     }
 949 }