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