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 private static final String JAVABASEJMOD = "java.base.jmod"; 642 643 @SuppressWarnings("unchecked") 644 public static final BundlerParamInfo<List<Path>> MODULE_PATH = 645 new StandardBundlerParam<>( 646 I18N.getString("param.module-path.name"), 647 I18N.getString("param.module-path.description"), 648 "module-path", 649 (Class<List<Path>>) (Object)List.class, 650 p -> { return getDefaultModulePath(); }, 651 (s, p) -> { 652 List<Path> modulePath = Arrays.asList(s.split(File.pathSeparator)).stream() 653 .map(ss -> new File(ss).toPath()) 654 .collect(Collectors.toList()); 655 Path javaBasePath = null; 656 if (modulePath != null) { 657 javaBasePath = JLinkBundlerHelper.findPathOfModule(modulePath, JAVABASEJMOD); 658 } 659 else { 660 modulePath = new ArrayList(); 661 } 662 663 // Add the default JDK module path to the module path. 664 if (javaBasePath == null) { 665 List<Path> jdkModulePath = getDefaultModulePath(); 666 667 if (jdkModulePath != null) { 668 modulePath.addAll(jdkModulePath); 669 javaBasePath = JLinkBundlerHelper.findPathOfModule(modulePath, JAVABASEJMOD); 670 } 671 } 672 673 if (javaBasePath == null || !Files.exists(javaBasePath)) { 674 com.oracle.tools.packager.Log.info( 675 String.format(I18N.getString("warning.no.jdk.modules.found"))); 676 } 677 678 return modulePath; 679 }); 680 681 @SuppressWarnings("unchecked") 682 public static final BundlerParamInfo<String> MODULE = 683 new StandardBundlerParam<>( 684 I18N.getString("param.main.module.name"), 685 I18N.getString("param.main.module.description"), 686 "module", 687 String.class, 688 p -> null, 689 (s, p) -> { 690 return String.valueOf(s); 691 }); 692 693 @SuppressWarnings("unchecked") 694 public static final BundlerParamInfo<Set<String>> ADD_MODULES = 695 new StandardBundlerParam<>( 696 I18N.getString("param.add-modules.name"), 697 I18N.getString("param.add-modules.description"), 698 "add-modules", 699 (Class<Set<String>>) (Object) Set.class, 700 p -> new LinkedHashSet(), 701 (s, p) -> new LinkedHashSet<>(Arrays.asList(s.split(","))) 702 ); 703 704 @SuppressWarnings("unchecked") 705 public static final BundlerParamInfo<Set<String>> LIMIT_MODULES = 706 new StandardBundlerParam<>( 707 I18N.getString("param.limit-modules.name"), 708 I18N.getString("param.limit-modules.description"), 709 "limit-modules", 710 (Class<Set<String>>) (Object) Set.class, 711 p -> new LinkedHashSet(), 712 (s, p) -> new LinkedHashSet<>(Arrays.asList(s.split(","))) 713 ); 714 715 @SuppressWarnings("unchecked") 716 public static final BundlerParamInfo<Boolean> STRIP_NATIVE_COMMANDS = 717 new StandardBundlerParam<>( 718 I18N.getString("param.strip-executables.name"), 719 I18N.getString("param.strip-executables.description"), 720 "strip-native-commands", 721 Boolean.class, 722 p -> Boolean.TRUE, 723 (s, p) -> Boolean.valueOf(s) 724 ); 725 726 public static void extractMainClassInfoFromAppResources(Map<String, ? super Object> params) { 727 boolean hasMainClass = params.containsKey(MAIN_CLASS.getID()); 728 boolean hasMainJar = params.containsKey(MAIN_JAR.getID()); 729 boolean hasMainJarClassPath = params.containsKey(CLASSPATH.getID()); 730 boolean hasPreloader = params.containsKey(PRELOADER_CLASS.getID()); 731 boolean hasModule = params.containsKey(MODULE.getID()); 732 733 if (hasMainClass && hasMainJar && hasMainJarClassPath || hasModule) { 734 return; 735 } 736 737 // it's a pair. The [0] is the srcdir [1] is the file relative to sourcedir 738 List<String[]> filesToCheck = new ArrayList<>(); 739 740 if (hasMainJar) { 741 RelativeFileSet rfs = MAIN_JAR.fetchFrom(params); 742 for (String s : rfs.getIncludedFiles()) { 743 filesToCheck.add(new String[]{rfs.getBaseDirectory().toString(), s}); 744 } 745 } else if (hasMainJarClassPath) { 746 for (String s : CLASSPATH.fetchFrom(params).split("\\s+")) { 747 if (APP_RESOURCES.fetchFrom(params) != null) { 748 filesToCheck.add(new String[] {APP_RESOURCES.fetchFrom(params).getBaseDirectory().toString(), s}); 749 } 750 } 751 } else { 752 List<RelativeFileSet> rfsl = APP_RESOURCES_LIST.fetchFrom(params); 753 if (rfsl == null || rfsl.isEmpty()) { 754 return; 755 } 756 for (RelativeFileSet rfs : rfsl) { 757 if (rfs == null) continue; 758 759 for (String s : rfs.getIncludedFiles()) { 760 filesToCheck.add(new String[]{rfs.getBaseDirectory().toString(), s}); 761 } 762 } 763 } 764 765 String declaredMainClass = (String) params.get(MAIN_CLASS.getID()); 766 767 // presume the set iterates in-order 768 for (String[] fnames : filesToCheck) { 769 try { 770 // only sniff jars 771 if (!fnames[1].toLowerCase().endsWith(".jar")) continue; 772 773 File file = new File(fnames[0], fnames[1]); 774 // that actually exist 775 if (!file.exists()) continue; 776 777 try (JarFile jf = new JarFile(file)) { 778 Manifest m = jf.getManifest(); 779 Attributes attrs = (m != null) ? m.getMainAttributes() : null; 780 781 if (attrs != null) { 782 String mainClass = attrs.getValue(Attributes.Name.MAIN_CLASS); 783 String fxMain = attrs.getValue(MANIFEST_JAVAFX_MAIN); 784 String preloaderClass = attrs.getValue(MANIFEST_PRELOADER); 785 if (hasMainClass) { 786 if (declaredMainClass.equals(fxMain)) { 787 params.put(USE_FX_PACKAGING.getID(), true); 788 } else if (declaredMainClass.equals(mainClass)) { 789 params.put(USE_FX_PACKAGING.getID(), false); 790 } else { 791 if (fxMain != null) { 792 Log.info(MessageFormat.format(I18N.getString("message.fx-app-does-not-match-specified-main"), fnames[1], fxMain, declaredMainClass)); 793 } 794 if (mainClass != null) { 795 Log.info(MessageFormat.format(I18N.getString("message.main-class-does-not-match-specified-main"), fnames[1], mainClass, declaredMainClass)); 796 } 797 continue; 798 } 799 } else { 800 if (fxMain != null) { 801 params.put(USE_FX_PACKAGING.getID(), true); 802 params.put(MAIN_CLASS.getID(), fxMain); 803 } else if (mainClass != null) { 804 params.put(USE_FX_PACKAGING.getID(), false); 805 params.put(MAIN_CLASS.getID(), mainClass); 806 } else { 807 continue; 808 } 809 } 810 if (!hasPreloader && preloaderClass != null) { 811 params.put(PRELOADER_CLASS.getID(), preloaderClass); 812 } 813 if (!hasMainJar) { 814 if (fnames[0] == null) { 815 fnames[0] = file.getParentFile().toString(); 816 } 817 params.put(MAIN_JAR.getID(), new RelativeFileSet(new File(fnames[0]), new LinkedHashSet<>(Collections.singletonList(file)))); 818 } 819 if (!hasMainJarClassPath) { 820 String cp = attrs.getValue(Attributes.Name.CLASS_PATH); 821 params.put(CLASSPATH.getID(), cp == null ? "" : cp); 822 } 823 break; 824 } 825 } 826 } catch (IOException ignore) { 827 ignore.printStackTrace(); 828 } 829 } 830 } 831 832 public static void validateMainClassInfoFromAppResources(Map<String, ? super Object> params) throws ConfigException { 833 boolean hasMainClass = params.containsKey(MAIN_CLASS.getID()); 834 boolean hasMainJar = params.containsKey(MAIN_JAR.getID()); 835 boolean hasMainJarClassPath = params.containsKey(CLASSPATH.getID()); 836 boolean hasModule = params.containsKey(MODULE.getID()); 837 838 if (hasMainClass && hasMainJar && hasMainJarClassPath || hasModule) { 839 return; 840 } 841 842 extractMainClassInfoFromAppResources(params); 843 844 if (!params.containsKey(MAIN_CLASS.getID())) { 845 if (hasMainJar) { 846 throw new ConfigException( 847 MessageFormat.format(I18N.getString("error.no-main-class-with-main-jar"), 848 MAIN_JAR.fetchFrom(params)), 849 MessageFormat.format(I18N.getString("error.no-main-class-with-main-jar.advice"), 850 MAIN_JAR.fetchFrom(params))); 851 } else if (hasMainJarClassPath) { 852 throw new ConfigException( 853 I18N.getString("error.no-main-class-with-classpath"), 854 I18N.getString("error.no-main-class-with-classpath.advice")); 855 } else { 856 throw new ConfigException( 857 I18N.getString("error.no-main-class"), 858 I18N.getString("error.no-main-class.advice")); 859 } 860 } 861 } 862 863 864 private static List<String> splitStringWithEscapes(String s) { 865 List<String> l = new ArrayList<>(); 866 StringBuilder current = new StringBuilder(); 867 boolean quoted = false; 868 boolean escaped = false; 869 for (char c : s.toCharArray()) { 870 if (escaped) { 871 current.append(c); 872 } else if ('"' == c) { 873 quoted = !quoted; 874 } else if (!quoted && Character.isWhitespace(c)) { 875 l.add(current.toString()); 876 current = new StringBuilder(); 877 } else { 878 current.append(c); 879 } 880 } 881 l.add(current.toString()); 882 return l; 883 } 884 885 private static List<RelativeFileSet> createAppResourcesListFromString(String s, Map<String, ? super Object> objectObjectMap) { 886 List<RelativeFileSet> result = new ArrayList<>(); 887 for (String path : s.split("[:;]")) { 888 File f = new File(path); 889 if (f.getName().equals("*") || path.endsWith("/") || path.endsWith("\\")) { 890 if (f.getName().equals("*")) { 891 f = f.getParentFile(); 892 } 893 Set<File> theFiles = new HashSet<>(); 894 try { 895 Files.walk(f.toPath()) 896 .filter(Files::isRegularFile) 897 .forEach(p -> theFiles.add(p.toFile())); 898 } catch (IOException e) { 899 e.printStackTrace(); 900 } 901 result.add(new RelativeFileSet(f, theFiles)); 902 } else { 903 result.add(new RelativeFileSet(f.getParentFile(), Collections.singleton(f))); 904 } 905 } 906 return result; 907 } 908 909 private static RelativeFileSet getMainJar(String moduleName, Map<String, ? super Object> params) { 910 for (RelativeFileSet rfs : APP_RESOURCES_LIST.fetchFrom(params)) { 911 File appResourcesRoot = rfs.getBaseDirectory(); 912 File mainJarFile = new File(appResourcesRoot, moduleName); 913 914 if (mainJarFile.exists()) { 915 return new RelativeFileSet(appResourcesRoot, new LinkedHashSet<>(Collections.singletonList(mainJarFile))); 916 } 917 else { 918 List<Path> modulePath = MODULE_PATH.fetchFrom(params); 919 Path modularJarPath = JLinkBundlerHelper.findPathOfModule(modulePath, moduleName); 920 921 if (modularJarPath != null && Files.exists(modularJarPath)) { 922 return new RelativeFileSet(appResourcesRoot, new LinkedHashSet<>(Collections.singletonList(modularJarPath.toFile()))); 923 } 924 } 925 } 926 927 throw new IllegalArgumentException( 928 new ConfigException( 929 MessageFormat.format(I18N.getString("error.main-jar-does-not-exist"), moduleName), 930 I18N.getString("error.main-jar-does-not-exist.advice"))); 931 } 932 933 public static List<Path> getDefaultModulePath() { 934 List<Path> result = new ArrayList(); 935 Path jdkModulePath = Paths.get(System.getProperty("java.home"), "jmods").toAbsolutePath(); 936 937 if (jdkModulePath != null && Files.exists(jdkModulePath)) { 938 result.add(jdkModulePath); 939 } 940 else { 941 // On a developer build the JDK Home isn't where we expect it 942 // relative to the jmods directory. Do some extra 943 // processing to find it. 944 Map<String, String> env = System.getenv(); 945 946 if (env.containsKey("JDK_HOME")) { 947 jdkModulePath = Paths.get(env.get("JDK_HOME"), ".." + File.separator + "images" + File.separator + "jmods").toAbsolutePath(); 948 949 if (jdkModulePath != null && Files.exists(jdkModulePath)) { 950 result.add(jdkModulePath); 951 } 952 } 953 } 954 955 return result; 956 } 957 }