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