1 /* 2 * Copyright (c) 2011, 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.packager.internal; 27 28 import jdk.packager.internal.bundlers.*; 29 import jdk.packager.internal.bundlers.Bundler.BundleType; 30 import java.io.File; 31 import java.util.ArrayList; 32 import java.util.Arrays; 33 import java.util.Collection; 34 import java.util.LinkedHashMap; 35 import java.util.LinkedHashSet; 36 import java.util.LinkedList; 37 import java.util.List; 38 import java.util.Map; 39 import java.util.Set; 40 import java.util.TreeMap; 41 import java.util.TreeSet; 42 43 public class DeployParams extends CommonParams { 44 public enum RunMode { 45 EMBEDDED, STANDALONE, ALL 46 } 47 48 final List<RelativeFileSet> resources = new ArrayList<>(); 49 50 String id; 51 String title; 52 String vendor; 53 String email; 54 String description; 55 String category; 56 String licenseType; 57 String copyright; 58 String version; 59 Boolean systemWide; 60 Boolean serviceHint; 61 Boolean signBundle; 62 Boolean installdirChooser; 63 Boolean singleton; 64 65 String applicationClass; 66 String preloader; 67 68 List<Param> params; 69 List<String> arguments; //unnamed arguments 70 71 // Java 9 modules support 72 String addModules = null; 73 String limitModules = null; 74 Boolean stripNativeCommands = null; 75 Boolean detectmods = null; 76 String modulePath = null; 77 String module = null; 78 String debugPort = null; 79 String srcdir; 80 81 int width; 82 int height; 83 String embeddedWidth = null; 84 String embeddedHeight = null; 85 86 String appName; 87 String codebase; 88 89 @Deprecated final boolean embedCertificates = false; 90 boolean allPermissions = false; 91 String updateMode = "background"; 92 boolean isExtension = false; 93 boolean isSwingApp = false; 94 95 boolean jreInstaller = false; 96 97 Boolean needShortcut = null; 98 Boolean needMenu = null; 99 Boolean needInstall = null; 100 101 String outfile; 102 //if true then we cobundle js and image files needed 103 // for web deployment with the application 104 boolean includeDT; 105 106 String placeholder = "'javafx-app-placeholder'"; 107 String appId = null; 108 109 // didn't have a setter... 110 boolean offlineAllowed = true; 111 112 String jrePlatform = System.getProperty("java.version")+"+"; 113 File javaRuntimeToUse = null; 114 boolean javaRuntimeWasSet = false; 115 116 //list of jvm args (in theory string can contain spaces and need to be escaped 117 List<String> jvmargs = new LinkedList<>(); 118 Map<String, String> jvmUserArgs = new LinkedHashMap<>(); 119 120 //list of jvm properties (can also be passed as VM args 121 // but keeping them separate make it a bit more convinient 122 Map<String, String> properties = new LinkedHashMap<>(); 123 124 // raw arguments to the bundler 125 Map<String, ? super Object> bundlerArguments = new LinkedHashMap<>(); 126 127 String fallbackApp = null; 128 129 public void setJavaRuntimeSource(File src) { 130 javaRuntimeToUse = src; 131 javaRuntimeWasSet = true; 132 } 133 134 public void setCodebase(String codebase) { 135 this.codebase = codebase; 136 } 137 138 public void setId(String id) { 139 this.id = id; 140 } 141 142 public void setCategory(String category) { 143 this.category = category; 144 } 145 146 public void setLicenseType(String licenseType) { 147 this.licenseType = licenseType; 148 } 149 150 public void setCopyright(String copyright) { 151 this.copyright = copyright; 152 } 153 154 public void setVersion(String version) { 155 this.version = version; 156 } 157 158 public void setSystemWide(Boolean systemWide) { 159 this.systemWide = systemWide; 160 } 161 162 public void setServiceHint(Boolean serviceHint) { 163 this.serviceHint = serviceHint; 164 } 165 166 public void setInstalldirChooser(Boolean installdirChooser) { 167 this.installdirChooser = installdirChooser; 168 } 169 170 public void setSingleton(Boolean singleton) { 171 this.singleton = singleton; 172 } 173 174 public void setSignBundle(Boolean signBundle) { 175 this.signBundle = signBundle; 176 } 177 178 public void setJRE(String v) { 179 jrePlatform = v; 180 } 181 182 public void setSwingAppWithEmbeddedJavaFX(boolean v) { 183 isSwingApp = v; 184 } 185 186 public void setNeedInstall(boolean b) { 187 needInstall = b; 188 } 189 190 public void setOfflineAllowed(boolean b) { 191 offlineAllowed = b; 192 } 193 194 public void setNeedShortcut(Boolean b) { 195 needShortcut = b; 196 } 197 198 public void setEmbeddedDimensions(String w, String h) { 199 embeddedWidth = w; 200 embeddedHeight = h; 201 } 202 203 public void setFallback(String v) { 204 if (v == null) { 205 return; 206 } 207 208 if ("none".equals(v) || "null".equals(v)) { 209 fallbackApp = null; 210 } else { 211 fallbackApp = v; 212 } 213 } 214 215 public void addJvmArg(String v) { 216 jvmargs.add(v); 217 } 218 219 public void addJvmUserArg(String n, String v) { 220 jvmUserArgs.put(n, v); 221 } 222 223 public void addJvmProperty(String n, String v) { 224 properties.put(n, v); 225 } 226 227 public void setAllPermissions(boolean allPermissions) { 228 this.allPermissions = allPermissions; 229 } 230 231 public void setAppName(String appName) { 232 this.appName = appName; 233 } 234 235 public void setArguments(List<String> args) { 236 this.arguments = args; 237 } 238 239 public List<String> getArguments() { 240 return this.arguments; 241 } 242 243 public void addArgument(String arg) { 244 this.arguments.add(arg); 245 } 246 247 public void addAddModule(String value) { 248 if (addModules == null) { 249 addModules = value; 250 } 251 else { 252 addModules += "," + value; 253 } 254 } 255 256 public void addLimitModule(String value) { 257 if (limitModules == null) { 258 limitModules = value; 259 } 260 else { 261 limitModules += "," + value; 262 } 263 } 264 265 public String getModulePath() { 266 return this.modulePath; 267 } 268 269 public void setSrcdir(String srcdir) { 270 this.srcdir = srcdir; 271 } 272 273 public void setModulePath(String value) { 274 this.modulePath = value; 275 } 276 277 public void setModule(String value) { 278 this.module = value; 279 } 280 281 public void setDebug(String value) { 282 this.debugPort = value; 283 } 284 285 public void setStripNativeCommands(boolean value) { 286 this.stripNativeCommands = value; 287 } 288 289 public void setDetectModules(boolean value) { 290 this.detectmods = value; 291 } 292 293 public void setDescription(String description) { 294 this.description = description; 295 } 296 297 public void setPlaceholder(String p) { 298 placeholder = p; 299 } 300 301 public void setAppId(String id) { 302 appId = id; 303 } 304 305 public void setHeight(int height) { 306 this.height = height; 307 } 308 309 public void setOutfile(String outfile) { 310 this.outfile = outfile; 311 } 312 313 public void setParams(List<Param> params) { 314 this.params = params; 315 } 316 317 public void setPreloader(String preloader) { 318 this.preloader = preloader; 319 } 320 321 public void setTitle(String title) { 322 this.title = title; 323 } 324 325 public void setUpdateMode(String updateMode) { 326 this.updateMode = updateMode; 327 } 328 329 public void setVendor(String vendor) { 330 this.vendor = vendor; 331 } 332 333 public void setEmail(String email) { 334 this.email = email; 335 } 336 337 public void setWidth(int width) { 338 this.width = width; 339 } 340 341 public void setExtension(boolean isExtension) { 342 this.isExtension = isExtension; 343 } 344 345 public void setApplicationClass(String applicationClass) { 346 this.applicationClass = applicationClass; 347 } 348 349 public void setIncludeDT(boolean doEmbed) { 350 includeDT = doEmbed; 351 } 352 353 public void setJreInstaller(boolean value) { 354 jreInstaller = value; 355 } 356 357 public File getOutdir() { 358 return this.outdir; 359 } 360 361 static class Template { 362 File in; 363 File out; 364 365 Template(File in, File out) { 366 this.in = in; 367 this.out = out; 368 } 369 } 370 371 //we need to expand as in some cases 372 // (most notably javapackager) 373 //we may get "." as filename and assumption is we include 374 // everything in the given folder 375 // (IOUtils.copyfiles() have recursive behavior) 376 List<File> expandFileset(File root) { 377 List<File> files = new LinkedList<>(); 378 if (jdk.packager.internal.IOUtils.isNotSymbolicLink(root)) { 379 if (root.isDirectory()) { 380 File[] children = root.listFiles(); 381 if (children != null) { 382 for (File f : children) { 383 files.addAll(expandFileset(f)); 384 } 385 } 386 } else { 387 files.add(root); 388 } 389 } 390 return files; 391 } 392 393 @Override 394 public void addResource(File baseDir, String path) { 395 File file = new File(baseDir, path); 396 //normalize top level dir 397 // to strip things like "." in the path 398 // or it can confuse symlink detection logic 399 file = file.getAbsoluteFile(); 400 401 if (baseDir == null) { 402 baseDir = file.getParentFile(); 403 } 404 resources.add(new RelativeFileSet(baseDir, new LinkedHashSet<>(expandFileset(file)))); 405 } 406 407 @Override 408 public void addResource(File baseDir, File file) { 409 //normalize initial file 410 // to strip things like "." in the path 411 // or it can confuse symlink detection logic 412 file = file.getAbsoluteFile(); 413 414 if (baseDir == null) { 415 baseDir = file.getParentFile(); 416 } 417 resources.add(new RelativeFileSet(baseDir, new LinkedHashSet<>(expandFileset(file)))); 418 } 419 420 public void addResource(File baseDir, String path, String type) { 421 addResource(baseDir, createFile(baseDir, path), type); 422 } 423 424 public void addResource(File baseDir, File file, String type) { 425 addResource(baseDir, file, "eager", type, null, null); 426 } 427 428 public void addResource(File baseDir, File file, String mode, String type, String os, String arch) { 429 Set<File> singleFile = new LinkedHashSet<>(); 430 singleFile.add(file); 431 if (baseDir == null) { 432 baseDir = file.getParentFile(); 433 } 434 RelativeFileSet rfs = new RelativeFileSet(baseDir, singleFile); 435 rfs.setArch(arch); 436 rfs.setMode(mode); 437 rfs.setOs(os); 438 rfs.setType(parseTypeFromString(type, file)); 439 resources.add(rfs); 440 } 441 442 private RelativeFileSet.Type parseTypeFromString(String type, File file) { 443 if (type == null) { 444 if (file.getName().endsWith(".jar")) { 445 return RelativeFileSet.Type.jar; 446 } else if (file.getName().endsWith(".jnlp")) { 447 return RelativeFileSet.Type.jnlp; 448 } else { 449 return RelativeFileSet.Type.UNKNOWN; 450 } 451 } else { 452 return RelativeFileSet.Type.valueOf(type); 453 } 454 } 455 456 private static File createFile(final File baseDir, final String path) { 457 final File testFile = new File(path); 458 return testFile.isAbsolute() 459 ? testFile 460 : new File(baseDir == null 461 ? null 462 : baseDir.getAbsolutePath(), 463 path); 464 } 465 466 467 @Override 468 public void validate() throws PackagerException { 469 if (outdir == null) { 470 throw new PackagerException("ERR_MissingArgument", "-outdir"); 471 } 472 473 if (bundlerArguments.get(Arguments.CLIOptions.MODULE.getId()) == null && !jreInstaller) { 474 if (resources.isEmpty()) { 475 throw new PackagerException("ERR_MissingAppResources"); 476 } 477 478 if (bundlerArguments.get(Arguments.CLIOptions.APPCLASS.getId()) == null) { 479 throw new PackagerException("ERR_MissingArgument", "-appclass"); 480 } 481 } 482 } 483 484 public boolean validateForBundle() { 485 boolean result = false; 486 487 // Success 488 if (((applicationClass != null && !applicationClass.isEmpty()) || 489 (module != null && !module.isEmpty()))) { 490 result = true; 491 } 492 493 return result; 494 } 495 496 // could be icon or splash 497 // TODO: do we still need this class? 498 static class Icon { 499 final static int UNDEFINED = -1; 500 501 String href; 502 String kind; 503 int width = UNDEFINED; 504 int height = UNDEFINED; 505 int depth = UNDEFINED; 506 RunMode mode = RunMode.ALL; 507 508 Icon(String href, String kind, int w, int h, int d, RunMode m) { 509 mode = m; 510 this.href = href; 511 this.kind = kind; 512 if (w > 0) { 513 width = w; 514 } 515 if (h > 0) { 516 height = h; 517 } 518 if (d > 0) { 519 depth = d; 520 } 521 } 522 } 523 524 List<Icon> icons = new LinkedList<>(); 525 526 public void addIcon(String href, String kind, int w, int h, int d, RunMode m) { 527 icons.add(new Icon(href, kind, w, h, d, m)); 528 } 529 530 BundleType bundleType = BundleType.NONE; 531 String targetFormat = null; //means any 532 533 public void setBundleType(BundleType type) { 534 bundleType = type; 535 } 536 537 public BundleType getBundleType() { 538 return bundleType; 539 } 540 541 public void setTargetFormat(String t) { 542 targetFormat = t; 543 } 544 545 public String getTargetFormat() { 546 return targetFormat; 547 } 548 549 private String getArch() { 550 String arch = System.getProperty("os.arch").toLowerCase(); 551 552 if ("x86".equals(arch) || "i386".equals(arch) || "i486".equals(arch) 553 || "i586".equals(arch) || "i686".equals(arch)) { 554 arch = "x86"; 555 } else if ("x86_64".equals(arch) || "amd64".equals("arch")) { 556 arch = "x86_64"; 557 } 558 559 return arch; 560 } 561 562 static final Set<String> multi_args = new TreeSet<>(Arrays.asList( 563 StandardBundlerParam.JVM_PROPERTIES.getID(), 564 StandardBundlerParam.JVM_OPTIONS.getID(), 565 StandardBundlerParam.USER_JVM_OPTIONS.getID(), 566 StandardBundlerParam.ARGUMENTS.getID(), 567 StandardBundlerParam.MODULE_PATH.getID(), 568 StandardBundlerParam.ADD_MODULES.getID(), 569 StandardBundlerParam.LIMIT_MODULES.getID(), 570 StandardBundlerParam.STRIP_NATIVE_COMMANDS.getID(), 571 JLinkBundlerHelper.DETECT_MODULES.getID() 572 )); 573 574 @SuppressWarnings("unchecked") 575 public void addBundleArgument(String key, Object value) { 576 // special hack for multi-line arguments 577 if (multi_args.contains(key) && value instanceof String) { 578 Object existingValue = bundlerArguments.get(key); 579 if (existingValue instanceof String) { 580 bundlerArguments.put(key, existingValue + "\n\n" + value); 581 } else if (existingValue instanceof List) { 582 ((List)existingValue).add(value); 583 } else if (existingValue instanceof Map && ((String)value).contains("=")) { 584 String[] mapValues = ((String)value).split("=", 2); 585 ((Map)existingValue).put(mapValues[0], mapValues[1]); 586 } else { 587 bundlerArguments.put(key, value); 588 } 589 } else { 590 bundlerArguments.put(key, value); 591 } 592 } 593 594 public BundleParams getBundleParams() { 595 BundleParams bundleParams = new BundleParams(); 596 597 //construct app resources 598 // relative to output folder! 599 String currentOS = System.getProperty("os.name").toLowerCase(); 600 String currentArch = getArch(); 601 602 for (RelativeFileSet rfs : resources) { 603 String os = rfs.getOs(); 604 String arch = rfs.getArch(); 605 //skip resources for other OS 606 // and nativelib jars (we are including raw libraries) 607 if ((os == null || currentOS.contains(os.toLowerCase())) && 608 (arch == null || currentArch.startsWith(arch.toLowerCase())) 609 && rfs.getType() != RelativeFileSet.Type.nativelib) { 610 if (rfs.getType() == RelativeFileSet.Type.license) { 611 for (String s : rfs.getIncludedFiles()) { 612 bundleParams.addLicenseFile(s); 613 } 614 } 615 } 616 } 617 618 bundleParams.setAppResourcesList(resources); 619 620 bundleParams.setIdentifier(id); 621 622 if (javaRuntimeWasSet) { 623 bundleParams.setRuntime(javaRuntimeToUse); 624 } 625 bundleParams.setApplicationClass(applicationClass); 626 bundleParams.setPrelaoderClass(preloader); 627 bundleParams.setName(this.appName); 628 bundleParams.setAppVersion(version); 629 bundleParams.setType(bundleType); 630 bundleParams.setBundleFormat(targetFormat); 631 bundleParams.setVendor(vendor); 632 bundleParams.setEmail(email); 633 bundleParams.setServiceHint(serviceHint); 634 bundleParams.setInstalldirChooser(installdirChooser); 635 bundleParams.setSingleton(singleton); 636 bundleParams.setCopyright(copyright); 637 bundleParams.setApplicationCategory(category); 638 bundleParams.setDescription(description); 639 bundleParams.setTitle(title); 640 if (verbose) bundleParams.setVerbose(true); 641 642 bundleParams.setJvmProperties(properties); 643 bundleParams.setJvmargs(jvmargs); 644 bundleParams.setJvmUserArgs(jvmUserArgs); 645 bundleParams.setArguments(arguments); 646 647 if (addModules != null && !addModules.isEmpty()) { 648 bundleParams.setAddModules(addModules); 649 } 650 651 if (limitModules != null && !limitModules.isEmpty()) { 652 bundleParams.setLimitModules(limitModules); 653 } 654 655 if (stripNativeCommands != null) { 656 bundleParams.setStripNativeCommands(stripNativeCommands); 657 } 658 659 bundleParams.setSrcDir(srcdir); 660 661 if (modulePath != null && !modulePath.isEmpty()) { 662 bundleParams.setModulePath(modulePath); 663 } 664 665 if (module != null && !module.isEmpty()) { 666 bundleParams.setMainModule(module); 667 } 668 669 if (debugPort != null && !debugPort.isEmpty()) { 670 bundleParams.setDebug(debugPort); 671 } 672 673 if (detectmods != null) { 674 bundleParams.setDetectMods(detectmods); 675 } 676 677 File appIcon = null; 678 List<Map<String, ? super Object>> bundlerIcons = new ArrayList<>(); 679 for (Icon ic: icons) { 680 //NB: in theory we should be paying attention to RunMode but 681 // currently everything is marked as webstart internally and runmode 682 // is not publicly documented property 683 if (/* (ic.mode == RunMode.ALL || ic.mode == RunMode.STANDALONE) && */ 684 (ic.kind == null || ic.kind.equals("default"))) 685 { 686 //could be full path or something relative to the output folder 687 appIcon = new File(ic.href); 688 if (!appIcon.exists()) { 689 jdk.packager.internal.Log.debug("Icon [" + ic.href + "] is not valid absolute path. " + 690 "Assume it is relative to the output dir."); 691 appIcon = new File(outdir, ic.href); 692 } 693 } 694 695 Map<String, ? super Object> iconInfo = new TreeMap<>(); 696 /* 697 if (ic.href != null) iconInfo.put(ICONS_HREF.getID(), ic.href); 698 if (ic.kind != null) iconInfo.put(ICONS_KIND.getID(), ic.kind); 699 if (ic.width > 0) iconInfo.put(ICONS_WIDTH.getID(), Integer.toString(ic.width)); 700 if (ic.height > 0) iconInfo.put(ICONS_HEIGHT.getID(), Integer.toString(ic.height)); 701 if (ic.depth > 0) iconInfo.put(ICONS_DEPTH.getID(), Integer.toString(ic.depth)); 702 */ 703 if (!iconInfo.isEmpty()) bundlerIcons.add(iconInfo); 704 } 705 // putUnlessNullOrEmpty(ICONS.getID(), bundlerIcons); 706 707 bundleParams.setIcon(appIcon); 708 709 Map<String, String> paramsMap = new TreeMap<>(); 710 if (params != null) { 711 for (Param p : params) { 712 paramsMap.put(p.name, p.value); 713 } 714 } 715 716 Map<String, String> unescapedHtmlParams = new TreeMap<>(); 717 Map<String, String> escapedHtmlParams = new TreeMap<>(); 718 719 //putUnlessNullOrEmpty(JNLPBundler.APPLET_PARAMS.getID(), unescapedHtmlParams); 720 //putUnlessNullOrEmpty(ESCAPED_APPLET_PARAMS.getID(), escapedHtmlParams); 721 722 /* 723 putUnlessNull(WIDTH.getID(), width); 724 putUnlessNull(HEIGHT.getID(), height); 725 putUnlessNull(EMBEDDED_WIDTH.getID(), embeddedWidth); 726 putUnlessNull(EMBEDDED_HEIGHT.getID(), embeddedHeight); 727 728 putUnlessNull(CODEBASE.getID(), codebase); 729 putUnlessNull(EMBED_JNLP.getID(), embedJNLP); 730 // embedCertificates 731 putUnlessNull(ALL_PERMISSIONS.getID(), allPermissions); 732 putUnlessNull(UPDATE_MODE.getID(), updateMode); 733 putUnlessNull(EXTENSION.getID(), isExtension); 734 putUnlessNull(SWING_APP.getID(), isSwingApp); 735 736 putUnlessNull(OUT_FILE.getID(), outfile); 737 putUnlessNull(INCLUDE_DT.getID(), includeDT); 738 putUnlessNull(PLACEHOLDER.getID(), placeholder); 739 putUnlessNull(OFFLINE_ALLOWED.getID(), offlineAllowed); 740 741 putUnlessNull(TEMPLATES.getID(), templatesMap); 742 743 putUnlessNull(FX_PLATFORM.getID(), fxPlatform); 744 putUnlessNull(JRE_PLATFORM.getID(), jrePlatform); 745 746 putUnlessNull(FALLBACK_APP.getID(), fallbackApp); 747 */ 748 // check for collisions 749 TreeSet<String> keys = new TreeSet<>(bundlerArguments.keySet()); 750 keys.retainAll(bundleParams.getBundleParamsAsMap().keySet()); 751 752 if (!keys.isEmpty()) { 753 throw new RuntimeException("Deploy Params and Bundler Arguments overlap in the following values:" + keys.toString()); 754 } 755 756 bundleParams.addAllBundleParams(bundlerArguments); 757 758 return bundleParams; 759 } 760 761 public Map<String, ? super Object> getBundlerArguments() { 762 return this.bundlerArguments; 763 } 764 765 public void putUnlessNull(String param, Object value) { 766 if (value != null) { 767 bundlerArguments.put(param, value); 768 } 769 } 770 771 public void putUnlessNullOrEmpty(String param, Map<?, ?> value) { 772 if (value != null && !value.isEmpty()) { 773 bundlerArguments.put(param, value); 774 } 775 } 776 777 public void putUnlessNullOrEmpty(String param, Collection<?> value) { 778 if (value != null && !value.isEmpty()) { 779 bundlerArguments.put(param, value); 780 } 781 } 782 }