1 /*
   2  * Copyright (c) 2012, 2019, 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.jpackage.internal;
  27 
  28 import java.io.File;
  29 import java.io.IOException;
  30 import java.util.*;
  31 import java.util.jar.Attributes;
  32 import java.util.jar.JarFile;
  33 import java.util.jar.Manifest;
  34 
  35 import static jdk.jpackage.internal.StandardBundlerParam.*;
  36 
  37 public class BundleParams {
  38 
  39     final protected Map<String, ? super Object> params;
  40 
  41     // RelativeFileSet
  42     public static final String PARAM_APP_RESOURCES      = "appResources";
  43 
  44     // BundlerType
  45     public static final String PARAM_TYPE               = "type";
  46 
  47     // String
  48     public static final String PARAM_BUNDLE_FORMAT      = "bundleFormat";
  49     // String
  50     public static final String PARAM_ICON               = "icon";
  51 
  52     // String - Name of bundle file and native launcher
  53     public static final String PARAM_NAME               = "name";
  54 
  55     // String - application vendor, used by most of the bundlers
  56     public static final String PARAM_VENDOR             = "vendor";
  57 
  58     // String - email name and email, only used for debian */
  59     public static final String PARAM_EMAIL              = "email";
  60 
  61     // String - vendor <email>, only used for debian */
  62     public static final String PARAM_MAINTAINER         = "maintainer";
  63 
  64     /* String - Copyright. Used on Mac */
  65     public static final String PARAM_COPYRIGHT          = "copyright";
  66 
  67     // String - GUID on windows for MSI, CFBundleIdentifier on Mac
  68     // If not compatible with requirements then bundler either do not bundle
  69     // or autogenerate
  70     public static final String PARAM_IDENTIFIER         = "identifier";
  71 
  72     /* boolean - shortcut preferences */
  73     public static final String PARAM_SHORTCUT           = "shortcutHint";
  74     // boolean - menu shortcut preference
  75     public static final String PARAM_MENU               = "menuHint";
  76 
  77     // String - Application version. Format may differ for different bundlers
  78     public static final String PARAM_VERSION            = "appVersion";
  79 
  80     // String - Application category. Used at least on Mac/Linux.
  81     // Value is platform specific
  82     public static final String PARAM_CATEGORY       = "applicationCategory";
  83 
  84     // String - Optional application description. Used by MSI and on Linux
  85     public static final String PARAM_DESCRIPTION        = "description";
  86 
  87     // String - License type. Needed on Linux (rpm)
  88     public static final String PARAM_LICENSE_TYPE       = "licenseType";
  89 
  90     // String - File with license. Format is OS/bundler specific
  91     public static final String PARAM_LICENSE_FILE       = "licenseFile";
  92 
  93     // String Main application class.
  94     // Not used directly but used to derive default values
  95     public static final String PARAM_APPLICATION_CLASS  = "applicationClass";
  96 
  97     // boolean - Adds a dialog to let the user choose a directory
  98     // where the product will be installed.
  99     public static final String PARAM_INSTALLDIR_CHOOSER = "installdirChooser";
 100 
 101     /**
 102      * create a new bundle with all default values
 103      */
 104     public BundleParams() {
 105         params = new HashMap<>();
 106     }
 107 
 108     /**
 109      * Create a bundle params with a copy of the params
 110      * @param params map of initial parameters to be copied in.
 111      */
 112     public BundleParams(Map<String, ?> params) {
 113         this.params = new HashMap<>(params);
 114     }
 115 
 116     public void addAllBundleParams(Map<String, ? super Object> p) {
 117         params.putAll(p);
 118     }
 119 
 120     public <C> C fetchParam(BundlerParamInfo<C> paramInfo) {
 121         return paramInfo.fetchFrom(params);
 122     }
 123 
 124     @SuppressWarnings("unchecked")
 125     public <C> C fetchParamWithDefault(
 126             Class<C> klass, C defaultValue, String... keys) {
 127         for (String key : keys) {
 128             Object o = params.get(key);
 129             if (klass.isInstance(o)) {
 130                 return (C) o;
 131             } else if (params.containsKey(key) && o == null) {
 132                 return null;
 133             } else if (o != null) {
 134                 Log.debug("Bundle param " + key + " is not type " + klass);
 135             }
 136         }
 137         return defaultValue;
 138     }
 139 
 140     public <C> C fetchParam(Class<C> klass, String... keys) {
 141         return fetchParamWithDefault(klass, null, keys);
 142     }
 143 
 144     // NOTE: we do not care about application parameters here
 145     // as they will be embeded into jar file manifest and
 146     // java launcher will take care of them!
 147 
 148     public Map<String, ? super Object> getBundleParamsAsMap() {
 149         return new HashMap<>(params);
 150     }
 151 
 152     public void setJvmargs(List<String> jvmargs) {
 153         putUnlessNullOrEmpty(JVM_OPTIONS.getID(), jvmargs);
 154     }
 155 
 156     public void setArguments(List<String> arguments) {
 157         putUnlessNullOrEmpty(ARGUMENTS.getID(), arguments);
 158     }
 159 
 160     public void setAddModules(String value) {
 161         putUnlessNull(StandardBundlerParam.ADD_MODULES.getID(), value);
 162     }
 163 
 164     public void setLimitModules(String value)  {
 165         putUnlessNull(StandardBundlerParam.LIMIT_MODULES.getID(), value);
 166     }
 167 
 168     public void setStripNativeCommands(boolean value) {
 169         putUnlessNull(StandardBundlerParam.STRIP_NATIVE_COMMANDS.getID(),
 170                 value);
 171     }
 172 
 173     public void setModulePath(String value) {
 174         putUnlessNull(StandardBundlerParam.MODULE_PATH.getID(), value);
 175     }
 176 
 177     public void setMainModule(String value) {
 178         putUnlessNull(StandardBundlerParam.MODULE.getID(), value);
 179     }
 180 
 181     public void setDebug(String value) {
 182         putUnlessNull(JLinkBundlerHelper.DEBUG.getID(), value);
 183     }
 184 
 185     public String getApplicationID() {
 186         return fetchParam(IDENTIFIER);
 187     }
 188 
 189     public String getPreferencesID() {
 190         return fetchParam(PREFERENCES_ID);
 191     }
 192 
 193     public String getApplicationClass() {
 194         return fetchParam(MAIN_CLASS);
 195     }
 196 
 197     public void setApplicationClass(String applicationClass) {
 198         putUnlessNull(PARAM_APPLICATION_CLASS, applicationClass);
 199     }
 200 
 201     public String getAppVersion() {
 202         return fetchParam(VERSION);
 203     }
 204 
 205     public void setAppVersion(String version) {
 206         putUnlessNull(PARAM_VERSION, version);
 207     }
 208 
 209     public String getDescription() {
 210         return fetchParam(DESCRIPTION);
 211     }
 212 
 213     public void setDescription(String s) {
 214         putUnlessNull(PARAM_DESCRIPTION, s);
 215     }
 216 
 217     public void setInstalldirChooser(Boolean b) {
 218         putUnlessNull(PARAM_INSTALLDIR_CHOOSER, b);
 219     }
 220 
 221     public String getName() {
 222         return fetchParam(APP_NAME);
 223     }
 224 
 225     public void setName(String name) {
 226         putUnlessNull(PARAM_NAME, name);
 227     }
 228 
 229     @SuppressWarnings("deprecation")
 230     public BundlerType getType() {
 231         return fetchParam(BundlerType.class, PARAM_TYPE);
 232     }
 233 
 234     @SuppressWarnings("deprecation")
 235     public void setType(BundlerType type) {
 236         putUnlessNull(PARAM_TYPE, type);
 237     }
 238 
 239     public String getBundleFormat() {
 240         return fetchParam(String.class, PARAM_BUNDLE_FORMAT);
 241     }
 242 
 243     public void setBundleFormat(String t) {
 244         putUnlessNull(PARAM_BUNDLE_FORMAT, t);
 245     }
 246 
 247     public boolean getVerbose() {
 248         return fetchParam(VERBOSE);
 249     }
 250 
 251     public List<String> getJvmargs() {
 252         return JVM_OPTIONS.fetchFrom(params);
 253     }
 254 
 255     public List<String> getArguments() {
 256         return ARGUMENTS.fetchFrom(params);
 257     }
 258 
 259     public jdk.jpackage.internal.RelativeFileSet getAppResource() {
 260         return fetchParam(APP_RESOURCES);
 261     }
 262 
 263     public void setAppResource(jdk.jpackage.internal.RelativeFileSet fs) {
 264         putUnlessNull(PARAM_APP_RESOURCES, fs);
 265     }
 266 
 267     public void setAppResourcesList(
 268             List<jdk.jpackage.internal.RelativeFileSet> rfs) {
 269         putUnlessNull(APP_RESOURCES_LIST.getID(), rfs);
 270     }
 271 
 272     public String getApplicationCategory() {
 273         return fetchParam(CATEGORY);
 274     }
 275 
 276     public void setApplicationCategory(String category) {
 277         putUnlessNull(PARAM_CATEGORY, category);
 278     }
 279 
 280     public String getMainClassName() {
 281         String applicationClass = getApplicationClass();
 282 
 283         if (applicationClass == null) {
 284             return null;
 285         }
 286 
 287         int idx = applicationClass.lastIndexOf(".");
 288         if (idx >= 0) {
 289             return applicationClass.substring(idx+1);
 290         }
 291         return applicationClass;
 292     }
 293 
 294     public String getCopyright() {
 295         return fetchParam(COPYRIGHT);
 296     }
 297 
 298     public void setCopyright(String c) {
 299         putUnlessNull(PARAM_COPYRIGHT, c);
 300     }
 301 
 302     public String getIdentifier() {
 303         return fetchParam(IDENTIFIER);
 304     }
 305 
 306     public void setIdentifier(String s) {
 307         putUnlessNull(PARAM_IDENTIFIER, s);
 308     }
 309 
 310     private String mainJar = null;
 311 
 312     // assuming that application was packaged according to the rules
 313     // we must have application jar, i.e. jar where we embed launcher
 314     // and have main application class listed as main class!
 315     // If there are more than one, or none - it will be treated as
 316     // deployment error
 317     //
 318     // Note we look for both JavaFX executable jars and regular executable jars
 319     // As long as main "application" entry point is the same it is main class
 320     // (i.e. for FX jar we will use JavaFX manifest entry ...)
 321     public String getMainApplicationJar() {
 322         jdk.jpackage.internal.RelativeFileSet appResources = getAppResource();
 323         if (mainJar != null) {
 324             if (getApplicationClass() == null) try {
 325                 if (appResources != null) {
 326                     File srcdir = appResources.getBaseDirectory();
 327                     JarFile jf = new JarFile(new File(srcdir, mainJar));
 328                     Manifest m = jf.getManifest();
 329                     Attributes attrs = (m != null) ?
 330                             m.getMainAttributes() : null;
 331                     if (attrs != null) {
 332                         setApplicationClass(
 333                                 attrs.getValue(Attributes.Name.MAIN_CLASS));
 334                     }
 335                 }
 336             } catch (IOException ignore) {
 337             }
 338             return mainJar;
 339         }
 340 
 341         String applicationClass = getApplicationClass();
 342 
 343         if (appResources == null || applicationClass == null) {
 344             return null;
 345         }
 346         File srcdir = appResources.getBaseDirectory();
 347         for (String fname : appResources.getIncludedFiles()) {
 348             JarFile jf;
 349             try {
 350                 jf = new JarFile(new File(srcdir, fname));
 351                 Manifest m = jf.getManifest();
 352                 Attributes attrs = (m != null) ? m.getMainAttributes() : null;
 353                 if (attrs != null) {
 354                     boolean javaMain = applicationClass.equals(
 355                                attrs.getValue(Attributes.Name.MAIN_CLASS));
 356 
 357                     if (javaMain) {
 358                         mainJar = fname;
 359                         return mainJar;
 360                     }
 361                 }
 362             } catch (IOException ignore) {
 363             }
 364         }
 365         return null;
 366     }
 367 
 368     public String getVendor() {
 369         return fetchParam(VENDOR);
 370     }
 371 
 372     public void setVendor(String vendor) {
 373        putUnlessNull(PARAM_VENDOR, vendor);
 374     }
 375 
 376     public String getEmail() {
 377         return fetchParam(String.class, PARAM_EMAIL);
 378     }
 379 
 380     public void setEmail(String email) {
 381         putUnlessNull(PARAM_EMAIL, email);
 382     }
 383 
 384     public void putUnlessNull(String param, Object value) {
 385         if (value != null) {
 386             params.put(param, value);
 387         }
 388     }
 389 
 390     public void putUnlessNullOrEmpty(String param, Collection<?> value) {
 391         if (value != null && !value.isEmpty()) {
 392             params.put(param, value);
 393         }
 394     }
 395 
 396     public void putUnlessNullOrEmpty(String param, Map<?,?> value) {
 397         if (value != null && !value.isEmpty()) {
 398             params.put(param, value);
 399         }
 400     }
 401 
 402 }