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 - Copyright. Used on Mac */
  62     public static final String PARAM_COPYRIGHT          = "copyright";
  63 
  64     // String - GUID on windows for MSI, CFBundleIdentifier on Mac
  65     // If not compatible with requirements then bundler either do not bundle
  66     // or autogenerate
  67     public static final String PARAM_IDENTIFIER         = "identifier";
  68 
  69     /* boolean - shortcut preferences */
  70     public static final String PARAM_SHORTCUT           = "shortcutHint";
  71     // boolean - menu shortcut preference
  72     public static final String PARAM_MENU               = "menuHint";
  73 
  74     // String - Application version. Format may differ for different bundlers
  75     public static final String PARAM_VERSION            = "appVersion";
  76 
  77     // String - Application category. Used at least on Mac/Linux.
  78     // Value is platform specific
  79     public static final String PARAM_CATEGORY       = "applicationCategory";
  80 
  81     // String - Optional short application
  82     public static final String PARAM_TITLE              = "title";
  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 setModulePath(String value) {
 169         putUnlessNull(StandardBundlerParam.MODULE_PATH.getID(), value);
 170     }
 171 
 172     public void setMainModule(String value) {
 173         putUnlessNull(StandardBundlerParam.MODULE.getID(), value);
 174     }
 175 
 176     public void setDebug(String value) {
 177         putUnlessNull(JLinkBundlerHelper.DEBUG.getID(), value);
 178     }
 179 
 180     public String getApplicationID() {
 181         return fetchParam(IDENTIFIER);
 182     }
 183 
 184     public String getPreferencesID() {
 185         return fetchParam(PREFERENCES_ID);
 186     }
 187 
 188     public String getTitle() {
 189         return fetchParam(TITLE);
 190     }
 191 
 192     public void setTitle(String title) {
 193         putUnlessNull(PARAM_TITLE, title);
 194     }
 195 
 196     public String getApplicationClass() {
 197         return fetchParam(MAIN_CLASS);
 198     }
 199 
 200     public void setApplicationClass(String applicationClass) {
 201         putUnlessNull(PARAM_APPLICATION_CLASS, applicationClass);
 202     }
 203 
 204     public String getAppVersion() {
 205         return fetchParam(VERSION);
 206     }
 207 
 208     public void setAppVersion(String version) {
 209         putUnlessNull(PARAM_VERSION, version);
 210     }
 211 
 212     public String getDescription() {
 213         return fetchParam(DESCRIPTION);
 214     }
 215 
 216     public void setDescription(String s) {
 217         putUnlessNull(PARAM_DESCRIPTION, s);
 218     }
 219 
 220     public void setInstalldirChooser(Boolean b) {
 221         putUnlessNull(PARAM_INSTALLDIR_CHOOSER, b);
 222     }
 223 
 224     public String getName() {
 225         return fetchParam(APP_NAME);
 226     }
 227 
 228     public void setName(String name) {
 229         putUnlessNull(PARAM_NAME, name);
 230     }
 231 
 232     @SuppressWarnings("deprecation")
 233     public BundlerType getType() {
 234         return fetchParam(BundlerType.class, PARAM_TYPE);
 235     }
 236 
 237     @SuppressWarnings("deprecation")
 238     public void setType(BundlerType type) {
 239         putUnlessNull(PARAM_TYPE, type);
 240     }
 241 
 242     public String getBundleFormat() {
 243         return fetchParam(String.class, PARAM_BUNDLE_FORMAT);
 244     }
 245 
 246     public void setBundleFormat(String t) {
 247         putUnlessNull(PARAM_BUNDLE_FORMAT, t);
 248     }
 249 
 250     public boolean getVerbose() {
 251         return fetchParam(VERBOSE);
 252     }
 253 
 254     public List<String> getJvmargs() {
 255         return JVM_OPTIONS.fetchFrom(params);
 256     }
 257 
 258     public List<String> getArguments() {
 259         return ARGUMENTS.fetchFrom(params);
 260     }
 261 
 262     public jdk.jpackage.internal.RelativeFileSet getAppResource() {
 263         return fetchParam(APP_RESOURCES);
 264     }
 265 
 266     public void setAppResource(jdk.jpackage.internal.RelativeFileSet fs) {
 267         putUnlessNull(PARAM_APP_RESOURCES, fs);
 268     }
 269 
 270     public void setAppResourcesList(
 271             List<jdk.jpackage.internal.RelativeFileSet> rfs) {
 272         putUnlessNull(APP_RESOURCES_LIST.getID(), rfs);
 273     }
 274 
 275     public String getApplicationCategory() {
 276         return fetchParam(CATEGORY);
 277     }
 278 
 279     public void setApplicationCategory(String category) {
 280         putUnlessNull(PARAM_CATEGORY, category);
 281     }
 282 
 283     public String getMainClassName() {
 284         String applicationClass = getApplicationClass();
 285 
 286         if (applicationClass == null) {
 287             return null;
 288         }
 289 
 290         int idx = applicationClass.lastIndexOf(".");
 291         if (idx >= 0) {
 292             return applicationClass.substring(idx+1);
 293         }
 294         return applicationClass;
 295     }
 296 
 297     public String getCopyright() {
 298         return fetchParam(COPYRIGHT);
 299     }
 300 
 301     public void setCopyright(String c) {
 302         putUnlessNull(PARAM_COPYRIGHT, c);
 303     }
 304 
 305     public String getIdentifier() {
 306         return fetchParam(IDENTIFIER);
 307     }
 308 
 309     public void setIdentifier(String s) {
 310         putUnlessNull(PARAM_IDENTIFIER, s);
 311     }
 312 
 313     private String mainJar = null;
 314 
 315     // assuming that application was packaged according to the rules
 316     // we must have application jar, i.e. jar where we embed launcher
 317     // and have main application class listed as main class!
 318     // If there are more than one, or none - it will be treated as
 319     // deployment error
 320     //
 321     // Note we look for both JavaFX executable jars and regular executable jars
 322     // As long as main "application" entry point is the same it is main class
 323     // (i.e. for FX jar we will use JavaFX manifest entry ...)
 324     public String getMainApplicationJar() {
 325         jdk.jpackage.internal.RelativeFileSet appResources = getAppResource();
 326         if (mainJar != null) {
 327             if (getApplicationClass() == null) try {
 328                 if (appResources != null) {
 329                     File srcdir = appResources.getBaseDirectory();
 330                     JarFile jf = new JarFile(new File(srcdir, mainJar));
 331                     Manifest m = jf.getManifest();
 332                     Attributes attrs = (m != null) ?
 333                             m.getMainAttributes() : null;
 334                     if (attrs != null) {
 335                         setApplicationClass(
 336                                 attrs.getValue(Attributes.Name.MAIN_CLASS));
 337                     }
 338                 }
 339             } catch (IOException ignore) {
 340             }
 341             return mainJar;
 342         }
 343 
 344         String applicationClass = getApplicationClass();
 345 
 346         if (appResources == null || applicationClass == null) {
 347             return null;
 348         }
 349         File srcdir = appResources.getBaseDirectory();
 350         for (String fname : appResources.getIncludedFiles()) {
 351             JarFile jf;
 352             try {
 353                 jf = new JarFile(new File(srcdir, fname));
 354                 Manifest m = jf.getManifest();
 355                 Attributes attrs = (m != null) ? m.getMainAttributes() : null;
 356                 if (attrs != null) {
 357                     boolean javaMain = applicationClass.equals(
 358                                attrs.getValue(Attributes.Name.MAIN_CLASS));
 359 
 360                     if (javaMain) {
 361                         mainJar = fname;
 362                         return mainJar;
 363                     }
 364                 }
 365             } catch (IOException ignore) {
 366             }
 367         }
 368         return null;
 369     }
 370 
 371     public String getVendor() {
 372         return fetchParam(VENDOR);
 373     }
 374 
 375     public void setVendor(String vendor) {
 376        putUnlessNull(PARAM_VENDOR, vendor);
 377     }
 378 
 379     public String getEmail() {
 380         return fetchParam(String.class, PARAM_EMAIL);
 381     }
 382 
 383     public void setEmail(String email) {
 384         putUnlessNull(PARAM_EMAIL, email);
 385     }
 386 
 387     public void putUnlessNull(String param, Object value) {
 388         if (value != null) {
 389             params.put(param, value);
 390         }
 391     }
 392 
 393     public void putUnlessNullOrEmpty(String param, Collection<?> value) {
 394         if (value != null && !value.isEmpty()) {
 395             params.put(param, value);
 396         }
 397     }
 398 
 399     public void putUnlessNullOrEmpty(String param, Map<?,?> value) {
 400         if (value != null && !value.isEmpty()) {
 401             params.put(param, value);
 402         }
 403     }
 404 
 405 }