1 /* 2 * Copyright (c) 2012, 2017, 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.sun.javafx.tools.packager.bundlers; 27 28 import com.oracle.tools.packager.*; 29 import com.sun.javafx.tools.packager.bundlers.Bundler.BundleType; 30 31 import java.io.File; 32 import java.io.IOException; 33 import java.util.*; 34 import java.util.jar.Attributes; 35 import java.util.jar.JarFile; 36 import java.util.jar.Manifest; 37 38 import static com.oracle.tools.packager.StandardBundlerParam.*; 39 40 import jdk.packager.internal.legacy.JLinkBundlerHelper; 41 42 43 public class BundleParams { 44 45 final protected Map<String, ? super Object> params; 46 47 public static final String PARAM_RUNTIME = "runtime"; // RelativeFileSet 48 public static final String PARAM_APP_RESOURCES = "appResources"; // RelativeFileSet 49 public static final String PARAM_TYPE = "type"; // BundlerType 50 public static final String PARAM_BUNDLE_FORMAT = "bundleFormat"; // String 51 public static final String PARAM_ICON = "icon"; // String 52 53 /* Name of bundle file and native launcher */ 54 public static final String PARAM_NAME = "name"; // String 55 56 /* application vendor, used by most of the bundlers */ 57 public static final String PARAM_VENDOR = "vendor"; // String 58 59 /* email name and email, only used for debian */ 60 public static final String PARAM_EMAIL = "email"; // String 61 62 /* Copyright. Used on Mac */ 63 public static final String PARAM_COPYRIGHT = "copyright"; // String 64 65 /* GUID on windows for MSI, CFBundleIdentifier on Mac 66 If not compatible with requirements then bundler either do not bundle 67 or autogenerate */ 68 public static final String PARAM_IDENTIFIER = "identifier"; // String 69 70 /* shortcut preferences */ 71 public static final String PARAM_SHORTCUT = "shortcutHint"; // boolean 72 public static final String PARAM_MENU = "menuHint"; // boolean 73 74 /* Application version. Format may differ for different bundlers */ 75 public static final String PARAM_VERSION = "appVersion"; // String 76 /* Application category. Used at least on Mac/Linux. Value is platform specific */ 77 public static final String PARAM_CATEGORY = "applicationCategory"; // String 78 79 /* Optional short application */ 80 public static final String PARAM_TITLE = "title"; // String 81 82 /* Optional application description. Used by MSI and on Linux */ 83 public static final String PARAM_DESCRIPTION = "description"; // String 84 85 /* License type. Needed on Linux (rpm) */ 86 public static final String PARAM_LICENSE_TYPE = "licenseType"; // String 87 88 /* File(s) with license. Format is OS/bundler specific */ 89 public static final String PARAM_LICENSE_FILE = "licenseFile"; // List<String> 90 91 /* user or system level install. 92 null means "default" */ 93 public static final String PARAM_SYSTEM_WIDE = "systemWide"; // Boolean 94 95 /* service/daemon install. 96 null means "default" */ 97 public static final String PARAM_SERVICE_HINT = "serviceHint"; // Boolean 98 99 100 /* Main application class. Not used directly but used to derive default values */ 101 public static final String PARAM_APPLICATION_CLASS = "applicationClass"; // String 102 103 /* Adds a dialog to let the user choose a directory where the product will be installed. */ 104 public static final String PARAM_INSTALLDIR_CHOOSER = "installdirChooser"; // Boolean 105 106 /* Prevents from launching multiple instances of application. */ 107 public static final String PARAM_SINGLETON = "singleton"; // Boolean 108 109 /** 110 * create a new bundle with all default values 111 */ 112 public BundleParams() { 113 params = new HashMap<>(); 114 } 115 116 /** 117 * Create a bundle params with a copy of the params 118 * @param params map of initial parameters to be copied in. 119 */ 120 public BundleParams(Map<String, ?> params) { 121 this.params = new HashMap<>(params); 122 } 123 124 public void addAllBundleParams(Map<String, ? super Object> p) { 125 params.putAll(p); 126 } 127 128 public <C> C fetchParam(BundlerParamInfo<C> paramInfo) { 129 return paramInfo.fetchFrom(params); 130 } 131 132 @SuppressWarnings("unchecked") 133 public <C> C fetchParamWithDefault(Class<C> klass, C defaultValue, String... keys) { 134 for (String key : keys) { 135 Object o = params.get(key); 136 if (klass.isInstance(o)) { 137 return (C) o; 138 } 139 else if (params.containsKey(keys) && o == null) { 140 return null; 141 } 142 //else if (o != null) { 143 //TODO log an error. 144 //} 145 } 146 return defaultValue; 147 } 148 149 public <C> C fetchParam(Class<C> klass, String... keys) { 150 return fetchParamWithDefault(klass, null, keys); 151 } 152 153 //NOTE: we do not care about application parameters here 154 // as they will be embeded into jar file manifest and 155 // java launcher will take care of them! 156 157 public Map<String, ? super Object> getBundleParamsAsMap() { 158 return new HashMap<>(params); 159 } 160 161 public void setJvmargs(List<String> jvmargs) { 162 putUnlessNullOrEmpty(JVM_OPTIONS.getID(), jvmargs); 163 } 164 165 public void setJvmUserArgs(Map<String, String> userArgs) { 166 167 putUnlessNullOrEmpty(USER_JVM_OPTIONS.getID(), userArgs); 168 } 169 170 public void setJvmProperties(Map<String, String> jvmProperties) { 171 putUnlessNullOrEmpty(JVM_PROPERTIES.getID(), jvmProperties); 172 } 173 174 public void setArguments(List<String> arguments) { 175 putUnlessNullOrEmpty(ARGUMENTS.getID(), arguments); 176 } 177 178 public void setAddModules(String value) { 179 putUnlessNull(StandardBundlerParam.ADD_MODULES.getID(), value); 180 } 181 182 public void setLimitModules(String value) { 183 putUnlessNull(StandardBundlerParam.LIMIT_MODULES.getID(), value); 184 } 185 186 public void setStripNativeCommands(boolean value) { 187 putUnlessNull(StandardBundlerParam.STRIP_NATIVE_COMMANDS.getID(), value); 188 } 189 190 public void setDetectMods(boolean value) { 191 putUnlessNull(JLinkBundlerHelper.DETECT_MODULES.getID(), value); 192 } 193 194 public void setSrcDir(String value) { 195 putUnlessNull(SOURCE_DIR.getID(), value); 196 } 197 198 public void setModulePath(String value) { 199 putUnlessNull(StandardBundlerParam.MODULE_PATH.getID(), value); 200 } 201 202 public void setMainModule(String value) { 203 putUnlessNull(StandardBundlerParam.MODULE.getID(), value); 204 } 205 206 public void setDebug(String value) { 207 putUnlessNull(JLinkBundlerHelper.DEBUG.getID(), value); 208 } 209 210 public String getApplicationID() { 211 return fetchParam(IDENTIFIER); 212 } 213 214 public String getPreferencesID() { 215 return fetchParam(PREFERENCES_ID); 216 } 217 218 public String getTitle() { 219 return fetchParam(TITLE); 220 } 221 222 public void setTitle(String title) { 223 putUnlessNull(PARAM_TITLE, title); 224 } 225 226 public String getApplicationClass() { 227 return fetchParam(MAIN_CLASS); 228 } 229 230 public void setApplicationClass(String applicationClass) { 231 putUnlessNull(PARAM_APPLICATION_CLASS, applicationClass); 232 } 233 234 public void setPrelaoderClass(String preloaderClass) { 235 putUnlessNull(PRELOADER_CLASS.getID(), preloaderClass); 236 } 237 238 public String getAppVersion() { 239 return fetchParam(VERSION); 240 } 241 242 public void setAppVersion(String version) { 243 putUnlessNull(PARAM_VERSION, version); 244 } 245 246 public String getDescription() { 247 return fetchParam(DESCRIPTION); 248 } 249 250 public void setDescription(String s) { 251 putUnlessNull(PARAM_DESCRIPTION, s); 252 } 253 254 public String getLicenseType() { 255 return fetchParam(LICENSE_TYPE); 256 } 257 258 public void setLicenseType(String version) { 259 putUnlessNull(PARAM_LICENSE_TYPE, version); 260 } 261 262 //path is relative to the application root 263 public void addLicenseFile(String path) { 264 List<String> licenseFiles = fetchParam(LICENSE_FILE); 265 if (licenseFiles == null || licenseFiles.isEmpty()) { 266 licenseFiles = new ArrayList<>(); 267 params.put(PARAM_LICENSE_FILE, licenseFiles); 268 } 269 licenseFiles.add(path); 270 } 271 272 public Boolean getSystemWide() { 273 return fetchParam(SYSTEM_WIDE); 274 } 275 276 public void setSystemWide(Boolean b) { 277 putUnlessNull(PARAM_SYSTEM_WIDE, b); 278 } 279 280 public void setServiceHint(Boolean b) { 281 putUnlessNull(PARAM_SERVICE_HINT, b); 282 } 283 284 public void setInstalldirChooser(Boolean b) { 285 putUnlessNull(PARAM_INSTALLDIR_CHOOSER, b); 286 } 287 288 public void setSingleton(Boolean b) { 289 putUnlessNull(PARAM_SINGLETON, b); 290 } 291 292 public void setSignBundle(Boolean b) { putUnlessNull(SIGN_BUNDLE.getID(), b); } 293 294 public boolean isShortcutHint() { 295 return fetchParam(SHORTCUT_HINT); 296 } 297 298 public void setShortcutHint(Boolean v) { 299 putUnlessNull(PARAM_SHORTCUT, v); 300 } 301 302 public boolean isMenuHint() { 303 return fetchParam(MENU_HINT); 304 } 305 306 public void setMenuHint(Boolean v) { 307 putUnlessNull(PARAM_MENU, v); 308 } 309 310 public String getName() { 311 return fetchParam(APP_NAME); 312 } 313 314 public void setName(String name) { 315 putUnlessNull(PARAM_NAME, name); 316 } 317 318 @SuppressWarnings("deprecation") 319 public BundleType getType() { 320 return fetchParam(BundleType.class, PARAM_TYPE); 321 } 322 323 @SuppressWarnings("deprecation") 324 public void setType(BundleType type) { 325 putUnlessNull(PARAM_TYPE, type); 326 } 327 328 public String getBundleFormat() { 329 return fetchParam(String.class, PARAM_BUNDLE_FORMAT); 330 } 331 332 public void setBundleFormat(String t) { 333 putUnlessNull(PARAM_BUNDLE_FORMAT, t); 334 } 335 336 public boolean getVerbose() { 337 return fetchParam(VERBOSE); 338 } 339 340 public void setVerbose(Boolean verbose) { 341 putUnlessNull(VERBOSE.getID(), verbose); 342 } 343 344 public List<String> getLicenseFile() { 345 return fetchParam(LICENSE_FILE); 346 } 347 348 public List<String> getJvmargs() { 349 return JVM_OPTIONS.fetchFrom(params); 350 } 351 352 public List<String> getArguments() { 353 return ARGUMENTS.fetchFrom(params); 354 } 355 356 //Validation approach: 357 // - JRE marker (rt.jar) 358 // - FX marker (jfxrt.jar) 359 // - JDK marker (tools.jar) 360 private static boolean checkJDKRoot(File jdkRoot) { 361 File rtJar = new File(jdkRoot, "jre/lib/rt.jar"); 362 if (!rtJar.exists()) { 363 Log.verbose("rt.jar is not found at " + rtJar.getAbsolutePath()); 364 return false; 365 } 366 367 File jfxJar = new File(jdkRoot, "jre/lib/ext/jfxrt.jar"); 368 if (!jfxJar.exists()) { 369 //Try again with new location 370 jfxJar = new File(jdkRoot, "jre/lib/jfxrt.jar"); 371 if (!jfxJar.exists()) { 372 Log.verbose("jfxrt.jar is not found at " + jfxJar.getAbsolutePath()); 373 return false; 374 } 375 } 376 377 378 File toolsJar = new File(jdkRoot, "lib/tools.jar"); 379 if (!toolsJar.exists()) { 380 Log.verbose("tools.jar is not found at " + toolsJar.getAbsolutePath()); 381 return false; 382 } 383 384 return true; 385 } 386 387 //Depending on platform and user input we may get different "references" 388 //Should support 389 // - java.home 390 // - reference to JDK install folder 391 // - should NOT support JRE dir 392 //Note: input could be null (then we asked to use system JRE) 393 // or it must be valid directory 394 //Returns null on validation failure. Returns jre root if ok. 395 public static File validateRuntimeLocation(File javaHome) { 396 if (javaHome == null) { 397 return null; 398 } 399 400 File jdkRoot; 401 File rtJar = new File(javaHome, "lib/rt.jar"); 402 403 if (rtJar.exists()) { //must be "java.home" case 404 //i.e. we are in JRE folder 405 jdkRoot = javaHome.getParentFile(); 406 } else { //expect it to be root of JDK installation folder 407 //On Mac it could be jdk/ or jdk/Contents/Home 408 //Norm to jdk/Contents/Home for validation 409 if (Platform.getPlatform() == Platform.MAC) { 410 File f = new File(javaHome, "Contents/Home"); 411 if (f.exists() && f.isDirectory()) { 412 javaHome = f; 413 } 414 } 415 jdkRoot = javaHome; 416 } 417 418 if (!checkJDKRoot(jdkRoot)) { 419 throw new RuntimeException( 420 "Can not find JDK artifacts in specified location: " 421 + javaHome.getAbsolutePath()); 422 } 423 424 return new File(jdkRoot, "jre"); 425 } 426 427 //select subset of given runtime using predefined rules 428 public void setRuntime(File baseDir) { 429 baseDir = validateRuntimeLocation(baseDir); 430 431 //mistake or explicit intent to use system runtime 432 if (baseDir == null) { 433 Log.verbose("No Java runtime to embed. Package will need system Java."); 434 params.put(PARAM_RUNTIME, null); 435 return; 436 } 437 doSetRuntime(baseDir); 438 } 439 440 //input dir "jdk/jre" (i.e. jre folder in the jdk) 441 private void doSetRuntime(File baseDir) { 442 params.put(PARAM_RUNTIME, baseDir.toString()); 443 } 444 445 //Currently unused? 446 // 447 //public void setRuntime(RelativeFileSet fs) { 448 // runtime = fs; 449 //} 450 451 public com.oracle.tools.packager.RelativeFileSet getAppResource() { 452 return fetchParam(APP_RESOURCES); 453 } 454 455 public void setAppResource(com.oracle.tools.packager.RelativeFileSet fs) { 456 putUnlessNull(PARAM_APP_RESOURCES, fs); 457 } 458 459 public void setAppResourcesList(List<com.oracle.tools.packager.RelativeFileSet> rfs) { 460 putUnlessNull(APP_RESOURCES_LIST.getID(), rfs); 461 } 462 463 public File getIcon() { 464 return fetchParam(ICON); 465 } 466 467 public void setIcon(File icon) { 468 putUnlessNull(PARAM_ICON, icon); 469 } 470 471 public String getApplicationCategory() { 472 return fetchParam(CATEGORY); 473 } 474 475 public void setApplicationCategory(String category) { 476 putUnlessNull(PARAM_CATEGORY, category); 477 } 478 479 public String getMainClassName() { 480 String applicationClass = getApplicationClass(); 481 482 if (applicationClass == null) { 483 return null; 484 } 485 486 int idx = applicationClass.lastIndexOf("."); 487 if (idx >= 0) { 488 return applicationClass.substring(idx+1); 489 } 490 return applicationClass; 491 } 492 493 public String getCopyright() { 494 return fetchParam(COPYRIGHT); 495 } 496 497 public void setCopyright(String c) { 498 putUnlessNull(PARAM_COPYRIGHT, c); 499 } 500 501 public String getIdentifier() { 502 return fetchParam(IDENTIFIER); 503 } 504 505 public void setIdentifier(String s) { 506 putUnlessNull(PARAM_IDENTIFIER, s); 507 } 508 509 private String mainJar = null; 510 private String mainJarClassPath = null; 511 private boolean useFXPackaging = true; 512 513 //For regular executable Jars we need to take care of classpath 514 //For JavaFX executable jars we do not need to pay attention to ClassPath entry in manifest 515 public String getAppClassPath() { 516 if (mainJar == null) { 517 //this will find out answer 518 getMainApplicationJar(); 519 } 520 if (useFXPackaging || mainJarClassPath == null) { 521 return ""; 522 } 523 return mainJarClassPath; 524 } 525 526 //assuming that application was packaged according to the rules 527 // we must have application jar, i.e. jar where we embed launcher 528 // and have main application class listed as main class! 529 //If there are more than one, or none - it will be treated as deployment error 530 // 531 //Note we look for both JavaFX executable jars and regular executable jars 532 //As long as main "application" entry point is the same it is main class 533 // (i.e. for FX jar we will use JavaFX manifest entry ...) 534 public String getMainApplicationJar() { 535 if (mainJar != null) { 536 return mainJar; 537 } 538 539 com.oracle.tools.packager.RelativeFileSet appResources = getAppResource(); 540 String applicationClass = getApplicationClass(); 541 542 if (appResources == null || applicationClass == null) { 543 return null; 544 } 545 File srcdir = appResources.getBaseDirectory(); 546 for (String fname : appResources.getIncludedFiles()) { 547 JarFile jf; 548 try { 549 jf = new JarFile(new File(srcdir, fname)); 550 Manifest m = jf.getManifest(); 551 Attributes attrs = (m != null) ? m.getMainAttributes() : null; 552 if (attrs != null) { 553 boolean javaMain = applicationClass.equals( 554 attrs.getValue(Attributes.Name.MAIN_CLASS)); 555 boolean fxMain = applicationClass.equals( 556 attrs.getValue(MANIFEST_JAVAFX_MAIN)); 557 if (javaMain || fxMain) { 558 useFXPackaging = fxMain; 559 mainJar = fname; 560 mainJarClassPath = attrs.getValue(Attributes.Name.CLASS_PATH); 561 return mainJar; 562 } 563 } 564 } catch (IOException ignore) { 565 } 566 } 567 return null; 568 } 569 570 public String getVendor() { 571 return fetchParam(VENDOR); 572 } 573 574 public void setVendor(String vendor) { 575 putUnlessNull(PARAM_VENDOR, vendor); 576 } 577 578 public String getEmail() { 579 return fetchParam(String.class, PARAM_EMAIL); 580 } 581 582 public void setEmail(String email) { 583 putUnlessNull(PARAM_EMAIL, email); 584 } 585 586 public void putUnlessNull(String param, Object value) { 587 if (value != null) { 588 params.put(param, value); 589 } 590 } 591 592 public void putUnlessNullOrEmpty(String param, Collection value) { 593 if (value != null && !value.isEmpty()) { 594 params.put(param, value); 595 } 596 } 597 598 public void putUnlessNullOrEmpty(String param, Map value) { 599 if (value != null && !value.isEmpty()) { 600 params.put(param, value); 601 } 602 } 603 604 }