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