src/jdk.jpackager/share/classes/jdk/jpackager/internal/BundleParams.java
branchJDK-8200758-branch
changeset 57038 b0f09e7c4680
equal deleted inserted replaced
57032:a42d0a8e0916 57038:b0f09e7c4680
       
     1 /*
       
     2  * Copyright (c) 2012, 2018, 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.jpackager.internal;
       
    27 
       
    28 import jdk.jpackager.internal.*;
       
    29 import jdk.jpackager.internal.BundlerType;
       
    30 import jdk.jpackager.internal.JLinkBundlerHelper;
       
    31 
       
    32 import java.io.File;
       
    33 import java.io.IOException;
       
    34 import java.util.*;
       
    35 import java.util.jar.Attributes;
       
    36 import java.util.jar.JarFile;
       
    37 import java.util.jar.Manifest;
       
    38 
       
    39 import static jdk.jpackager.internal.StandardBundlerParam.*;
       
    40 
       
    41 public class BundleParams {
       
    42 
       
    43     final protected Map<String, ? super Object> params;
       
    44 
       
    45     // RelativeFileSet
       
    46     public static final String PARAM_APP_RESOURCES      = "appResources";
       
    47 
       
    48     // BundlerType
       
    49     public static final String PARAM_TYPE               = "type";
       
    50 
       
    51     // String
       
    52     public static final String PARAM_BUNDLE_FORMAT      = "bundleFormat";
       
    53     // String
       
    54     public static final String PARAM_ICON               = "icon";
       
    55 
       
    56     // String - Name of bundle file and native launcher
       
    57     public static final String PARAM_NAME               = "name";
       
    58 
       
    59     // String - application vendor, used by most of the bundlers
       
    60     public static final String PARAM_VENDOR             = "vendor";
       
    61 
       
    62     // String - email name and email, only used for debian */
       
    63     public static final String PARAM_EMAIL              = "email";
       
    64 
       
    65     /* String - Copyright. Used on Mac */
       
    66     public static final String PARAM_COPYRIGHT          = "copyright";
       
    67 
       
    68     // String - GUID on windows for MSI, CFBundleIdentifier on Mac
       
    69     // If not compatible with requirements then bundler either do not bundle
       
    70     // or autogenerate
       
    71     public static final String PARAM_IDENTIFIER         = "identifier";
       
    72 
       
    73     /* boolean - shortcut preferences */
       
    74     public static final String PARAM_SHORTCUT           = "shortcutHint";
       
    75     // boolean - menu shortcut preference
       
    76     public static final String PARAM_MENU               = "menuHint";
       
    77 
       
    78     // String - Application version. Format may differ for different bundlers
       
    79     public static final String PARAM_VERSION            = "appVersion";
       
    80 
       
    81     // String - Application category. Used at least on Mac/Linux.
       
    82     // Value is platform specific
       
    83     public static final String PARAM_CATEGORY       = "applicationCategory";
       
    84 
       
    85     // String - Optional short application
       
    86     public static final String PARAM_TITLE              = "title";
       
    87 
       
    88     // String - Optional application description. Used by MSI and on Linux
       
    89     public static final String PARAM_DESCRIPTION        = "description";
       
    90 
       
    91     // String - License type. Needed on Linux (rpm)
       
    92     public static final String PARAM_LICENSE_TYPE       = "licenseType";
       
    93 
       
    94     // List<String> - File(s) with license. Format is OS/bundler specific
       
    95     public static final String PARAM_LICENSE_FILE       = "licenseFile";
       
    96 
       
    97     // boolean - service/daemon install.  null means "default"
       
    98     public static final String PARAM_SERVICE_HINT       = "serviceHint";
       
    99 
       
   100 
       
   101     // String Main application class.
       
   102     // Not used directly but used to derive default values
       
   103     public static final String PARAM_APPLICATION_CLASS  = "applicationClass";
       
   104 
       
   105     // boolean - Adds a dialog to let the user choose a directory
       
   106     // where the product will be installed.
       
   107     public static final String PARAM_INSTALLDIR_CHOOSER = "installdirChooser";
       
   108 
       
   109     // boolean - Prevents from launching multiple instances of application.
       
   110     public static final String PARAM_SINGLETON          = "singleton";
       
   111 
       
   112     /**
       
   113      * create a new bundle with all default values
       
   114      */
       
   115     public BundleParams() {
       
   116         params = new HashMap<>();
       
   117     }
       
   118 
       
   119     /**
       
   120      * Create a bundle params with a copy of the params
       
   121      * @param params map of initial parameters to be copied in.
       
   122      */
       
   123     public BundleParams(Map<String, ?> params) {
       
   124         this.params = new HashMap<>(params);
       
   125     }
       
   126 
       
   127     public void addAllBundleParams(Map<String, ? super Object> p) {
       
   128         params.putAll(p);
       
   129     }
       
   130 
       
   131     public <C> C fetchParam(BundlerParamInfo<C> paramInfo) {
       
   132         return paramInfo.fetchFrom(params);
       
   133     }
       
   134 
       
   135     @SuppressWarnings("unchecked")
       
   136     public <C> C fetchParamWithDefault(
       
   137             Class<C> klass, C defaultValue, String... keys) {
       
   138         for (String key : keys) {
       
   139             Object o = params.get(key);
       
   140             if (klass.isInstance(o)) {
       
   141                 return (C) o;
       
   142             } else if (params.containsKey(key) && o == null) {
       
   143                 return null;
       
   144             } else if (o != null) {
       
   145                 Log.debug("Bundle param " + key + " is not type " + klass);
       
   146             }
       
   147         }
       
   148         return defaultValue;
       
   149     }
       
   150 
       
   151     public <C> C fetchParam(Class<C> klass, String... keys) {
       
   152         return fetchParamWithDefault(klass, null, keys);
       
   153     }
       
   154 
       
   155     // NOTE: we do not care about application parameters here
       
   156     // as they will be embeded into jar file manifest and
       
   157     // java launcher will take care of them!
       
   158 
       
   159     public Map<String, ? super Object> getBundleParamsAsMap() {
       
   160         return new HashMap<>(params);
       
   161     }
       
   162 
       
   163     public void setJvmargs(List<String> jvmargs) {
       
   164         putUnlessNullOrEmpty(JVM_OPTIONS.getID(), jvmargs);
       
   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(),
       
   185                 value);
       
   186     }
       
   187 
       
   188     public void setModulePath(String value) {
       
   189         putUnlessNull(StandardBundlerParam.MODULE_PATH.getID(), value);
       
   190     }
       
   191 
       
   192     public void setMainModule(String value) {
       
   193         putUnlessNull(StandardBundlerParam.MODULE.getID(), value);
       
   194     }
       
   195 
       
   196     public void setDebug(String value) {
       
   197         putUnlessNull(JLinkBundlerHelper.DEBUG.getID(), value);
       
   198     }
       
   199 
       
   200     public String getApplicationID() {
       
   201         return fetchParam(IDENTIFIER);
       
   202     }
       
   203 
       
   204     public String getPreferencesID() {
       
   205         return fetchParam(PREFERENCES_ID);
       
   206     }
       
   207 
       
   208     public String getTitle() {
       
   209         return fetchParam(TITLE);
       
   210     }
       
   211 
       
   212     public void setTitle(String title) {
       
   213         putUnlessNull(PARAM_TITLE, title);
       
   214     }
       
   215 
       
   216     public String getApplicationClass() {
       
   217         return fetchParam(MAIN_CLASS);
       
   218     }
       
   219 
       
   220     public void setApplicationClass(String applicationClass) {
       
   221         putUnlessNull(PARAM_APPLICATION_CLASS, applicationClass);
       
   222     }
       
   223 
       
   224     public String getAppVersion() {
       
   225         return fetchParam(VERSION);
       
   226     }
       
   227 
       
   228     public void setAppVersion(String version) {
       
   229         putUnlessNull(PARAM_VERSION, version);
       
   230     }
       
   231 
       
   232     public String getDescription() {
       
   233         return fetchParam(DESCRIPTION);
       
   234     }
       
   235 
       
   236     public void setDescription(String s) {
       
   237         putUnlessNull(PARAM_DESCRIPTION, s);
       
   238     }
       
   239 
       
   240     //path is relative to the application root
       
   241     public void addLicenseFile(String path) {
       
   242         List<String> licenseFiles = fetchParam(LICENSE_FILE);
       
   243         if (licenseFiles == null || licenseFiles.isEmpty()) {
       
   244             licenseFiles = new ArrayList<>();
       
   245             params.put(PARAM_LICENSE_FILE, licenseFiles);
       
   246         }
       
   247         licenseFiles.add(path);
       
   248     }
       
   249 
       
   250     public void setServiceHint(Boolean b) {
       
   251         putUnlessNull(PARAM_SERVICE_HINT, b);
       
   252     }
       
   253 
       
   254     public void setInstalldirChooser(Boolean b) {
       
   255         putUnlessNull(PARAM_INSTALLDIR_CHOOSER, b);
       
   256     }
       
   257 
       
   258     public void setSingleton(Boolean b) {
       
   259         putUnlessNull(PARAM_SINGLETON, b);
       
   260     }
       
   261 
       
   262     public String getName() {
       
   263         return fetchParam(APP_NAME);
       
   264     }
       
   265 
       
   266     public void setName(String name) {
       
   267         putUnlessNull(PARAM_NAME, name);
       
   268     }
       
   269 
       
   270     @SuppressWarnings("deprecation")
       
   271     public BundlerType getType() {
       
   272         return fetchParam(BundlerType.class, PARAM_TYPE);
       
   273     }
       
   274 
       
   275     @SuppressWarnings("deprecation")
       
   276     public void setType(BundlerType type) {
       
   277         putUnlessNull(PARAM_TYPE, type);
       
   278     }
       
   279 
       
   280     public String getBundleFormat() {
       
   281         return fetchParam(String.class, PARAM_BUNDLE_FORMAT);
       
   282     }
       
   283 
       
   284     public void setBundleFormat(String t) {
       
   285         putUnlessNull(PARAM_BUNDLE_FORMAT, t);
       
   286     }
       
   287 
       
   288     public boolean getVerbose() {
       
   289         return fetchParam(VERBOSE);
       
   290     }
       
   291 
       
   292     public List<String> getLicenseFile() {
       
   293         return fetchParam(LICENSE_FILE);
       
   294     }
       
   295 
       
   296     public List<String> getJvmargs() {
       
   297         return JVM_OPTIONS.fetchFrom(params);
       
   298     }
       
   299 
       
   300     public List<String> getArguments() {
       
   301         return ARGUMENTS.fetchFrom(params);
       
   302     }
       
   303 
       
   304     // Validation approach:
       
   305     //  - javac and
       
   306     //
       
   307     //  - /jmods dir
       
   308     // or
       
   309     //  - JRE marker (rt.jar)
       
   310     //  - FX marker (jfxrt.jar)
       
   311     //  - JDK marker (tools.jar)
       
   312     private static boolean checkJDKRoot(File jdkRoot) {
       
   313         String exe = (Platform.getPlatform() == Platform.WINDOWS) ?
       
   314                 ".exe" : "";
       
   315         File javac = new File(jdkRoot, "bin/javac" + exe);
       
   316         if (!javac.exists()) {
       
   317             Log.verbose("javac is not found at " + javac.getAbsolutePath());
       
   318             return false;
       
   319         }
       
   320 
       
   321         File jmods = new File(jdkRoot, "jmods");
       
   322         if (!jmods.exists()) {
       
   323             Log.verbose("jmods is not found in " + jdkRoot.getAbsolutePath());
       
   324             return false;
       
   325         }
       
   326         return true;
       
   327     }
       
   328 
       
   329     public jdk.jpackager.internal.RelativeFileSet getAppResource() {
       
   330         return fetchParam(APP_RESOURCES);
       
   331     }
       
   332 
       
   333     public void setAppResource(jdk.jpackager.internal.RelativeFileSet fs) {
       
   334         putUnlessNull(PARAM_APP_RESOURCES, fs);
       
   335     }
       
   336 
       
   337     public void setAppResourcesList(
       
   338             List<jdk.jpackager.internal.RelativeFileSet> rfs) {
       
   339         putUnlessNull(APP_RESOURCES_LIST.getID(), rfs);
       
   340     }
       
   341 
       
   342     public String getApplicationCategory() {
       
   343         return fetchParam(CATEGORY);
       
   344     }
       
   345 
       
   346     public void setApplicationCategory(String category) {
       
   347         putUnlessNull(PARAM_CATEGORY, category);
       
   348     }
       
   349 
       
   350     public String getMainClassName() {
       
   351         String applicationClass = getApplicationClass();
       
   352 
       
   353         if (applicationClass == null) {
       
   354             return null;
       
   355         }
       
   356 
       
   357         int idx = applicationClass.lastIndexOf(".");
       
   358         if (idx >= 0) {
       
   359             return applicationClass.substring(idx+1);
       
   360         }
       
   361         return applicationClass;
       
   362     }
       
   363 
       
   364     public String getCopyright() {
       
   365         return fetchParam(COPYRIGHT);
       
   366     }
       
   367 
       
   368     public void setCopyright(String c) {
       
   369         putUnlessNull(PARAM_COPYRIGHT, c);
       
   370     }
       
   371 
       
   372     public String getIdentifier() {
       
   373         return fetchParam(IDENTIFIER);
       
   374     }
       
   375 
       
   376     public void setIdentifier(String s) {
       
   377         putUnlessNull(PARAM_IDENTIFIER, s);
       
   378     }
       
   379 
       
   380     private String mainJar = null;
       
   381     private String mainJarClassPath = null;
       
   382     private boolean useFXPackaging = true;
       
   383 
       
   384     // For regular executable Jars we need to take care of classpath
       
   385     // For JavaFX executable jars we do not need to pay attention to
       
   386     // ClassPath entry in manifest
       
   387     public String getAppClassPath() {
       
   388         if (mainJar == null) {
       
   389             // this will find out answer
       
   390             getMainApplicationJar();
       
   391         }
       
   392         if (useFXPackaging || mainJarClassPath == null) {
       
   393             return "";
       
   394         }
       
   395         return mainJarClassPath;
       
   396     }
       
   397 
       
   398     // assuming that application was packaged according to the rules
       
   399     // we must have application jar, i.e. jar where we embed launcher
       
   400     // and have main application class listed as main class!
       
   401     // If there are more than one, or none - it will be treated as
       
   402     // deployment error
       
   403     //
       
   404     // Note we look for both JavaFX executable jars and regular executable jars
       
   405     // As long as main "application" entry point is the same it is main class
       
   406     // (i.e. for FX jar we will use JavaFX manifest entry ...)
       
   407     public String getMainApplicationJar() {
       
   408         jdk.jpackager.internal.RelativeFileSet appResources = getAppResource();
       
   409         if (mainJar != null) {
       
   410             if (getApplicationClass() == null) try {
       
   411                 if (appResources != null) {
       
   412                     File srcdir = appResources.getBaseDirectory();
       
   413                     JarFile jf = new JarFile(new File(srcdir, mainJar));
       
   414                     Manifest m = jf.getManifest();
       
   415                     Attributes attrs = (m != null) ?
       
   416                             m.getMainAttributes() : null;
       
   417                     if (attrs != null) {
       
   418                         setApplicationClass(
       
   419                                 attrs.getValue(Attributes.Name.MAIN_CLASS));
       
   420                     }
       
   421                 }
       
   422             } catch (IOException ignore) {
       
   423             }
       
   424             return mainJar;
       
   425         }
       
   426 
       
   427         String applicationClass = getApplicationClass();
       
   428 
       
   429         if (appResources == null || applicationClass == null) {
       
   430             return null;
       
   431         }
       
   432         File srcdir = appResources.getBaseDirectory();
       
   433         for (String fname : appResources.getIncludedFiles()) {
       
   434             JarFile jf;
       
   435             try {
       
   436                 jf = new JarFile(new File(srcdir, fname));
       
   437                 Manifest m = jf.getManifest();
       
   438                 Attributes attrs = (m != null) ? m.getMainAttributes() : null;
       
   439                 if (attrs != null) {
       
   440                     boolean javaMain = applicationClass.equals(
       
   441                                attrs.getValue(Attributes.Name.MAIN_CLASS));
       
   442 
       
   443                     if (javaMain) {
       
   444                         mainJar = fname;
       
   445                         mainJarClassPath = attrs.getValue(
       
   446                                Attributes.Name.CLASS_PATH);
       
   447                         return mainJar;
       
   448                     }
       
   449                 }
       
   450             } catch (IOException ignore) {
       
   451             }
       
   452         }
       
   453         return null;
       
   454     }
       
   455 
       
   456     public String getVendor() {
       
   457         return fetchParam(VENDOR);
       
   458     }
       
   459 
       
   460     public void setVendor(String vendor) {
       
   461        putUnlessNull(PARAM_VENDOR, vendor);
       
   462     }
       
   463 
       
   464     public String getEmail() {
       
   465         return fetchParam(String.class, PARAM_EMAIL);
       
   466     }
       
   467 
       
   468     public void setEmail(String email) {
       
   469         putUnlessNull(PARAM_EMAIL, email);
       
   470     }
       
   471 
       
   472     public void putUnlessNull(String param, Object value) {
       
   473         if (value != null) {
       
   474             params.put(param, value);
       
   475         }
       
   476     }
       
   477 
       
   478     public void putUnlessNullOrEmpty(String param, Collection<?> value) {
       
   479         if (value != null && !value.isEmpty()) {
       
   480             params.put(param, value);
       
   481         }
       
   482     }
       
   483 
       
   484     public void putUnlessNullOrEmpty(String param, Map<?,?> value) {
       
   485         if (value != null && !value.isEmpty()) {
       
   486             params.put(param, value);
       
   487         }
       
   488     }
       
   489 
       
   490 }