src/jdk.jpackager/macosx/classes/jdk/jpackager/internal/MacAppBundler.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.AbstractImageBundler;
       
    29 import jdk.jpackager.internal.BundlerParamInfo;
       
    30 import jdk.jpackager.internal.ConfigException;
       
    31 import jdk.jpackager.internal.EnumeratedBundlerParam;
       
    32 import jdk.jpackager.internal.IOUtils;
       
    33 import jdk.jpackager.internal.Log;
       
    34 import jdk.jpackager.internal.Platform;
       
    35 import jdk.jpackager.internal.StandardBundlerParam;
       
    36 import jdk.jpackager.internal.Arguments;
       
    37 import jdk.jpackager.internal.UnsupportedPlatformException;
       
    38 import jdk.jpackager.internal.MacAppImageBuilder;
       
    39 import jdk.jpackager.internal.resources.MacResources;
       
    40 import jdk.jpackager.internal.JLinkBundlerHelper;
       
    41 
       
    42 import java.io.File;
       
    43 import java.io.IOException;
       
    44 import java.math.BigInteger;
       
    45 import java.text.MessageFormat;
       
    46 import java.util.Arrays;
       
    47 import java.util.Collection;
       
    48 import java.util.HashMap;
       
    49 import java.util.Map;
       
    50 import java.util.Optional;
       
    51 import java.util.ResourceBundle;
       
    52 
       
    53 import static jdk.jpackager.internal.StandardBundlerParam.*;
       
    54 import static jdk.jpackager.internal.MacBaseInstallerBundler.*;
       
    55 import jdk.jpackager.internal.AbstractAppImageBuilder;
       
    56 
       
    57 public class MacAppBundler extends AbstractImageBundler {
       
    58 
       
    59     private static final ResourceBundle I18N =
       
    60             ResourceBundle.getBundle(
       
    61                     "jdk.jpackager.internal.resources.MacAppBundler");
       
    62 
       
    63     private static final String TEMPLATE_BUNDLE_ICON = "GenericApp.icns";
       
    64 
       
    65     private static Map<String, String> getMacCategories() {
       
    66         Map<String, String> map = new HashMap<>();
       
    67         map.put("Business", "public.app-category.business");
       
    68         map.put("Developer Tools", "public.app-category.developer-tools");
       
    69         map.put("Education", "public.app-category.education");
       
    70         map.put("Entertainment", "public.app-category.entertainment");
       
    71         map.put("Finance", "public.app-category.finance");
       
    72         map.put("Games", "public.app-category.games");
       
    73         map.put("Graphics & Design", "public.app-category.graphics-design");
       
    74         map.put("Healthcare & Fitness",
       
    75                 "public.app-category.healthcare-fitness");
       
    76         map.put("Lifestyle", "public.app-category.lifestyle");
       
    77         map.put("Medical", "public.app-category.medical");
       
    78         map.put("Music", "public.app-category.music");
       
    79         map.put("News", "public.app-category.news");
       
    80         map.put("Photography", "public.app-category.photography");
       
    81         map.put("Productivity", "public.app-category.productivity");
       
    82         map.put("Reference", "public.app-category.reference");
       
    83         map.put("Social Networking", "public.app-category.social-networking");
       
    84         map.put("Sports", "public.app-category.sports");
       
    85         map.put("Travel", "public.app-category.travel");
       
    86         map.put("Utilities", "public.app-category.utilities");
       
    87         map.put("Video", "public.app-category.video");
       
    88         map.put("Weather", "public.app-category.weather");
       
    89 
       
    90         map.put("Action Games", "public.app-category.action-games");
       
    91         map.put("Adventure Games", "public.app-category.adventure-games");
       
    92         map.put("Arcade Games", "public.app-category.arcade-games");
       
    93         map.put("Board Games", "public.app-category.board-games");
       
    94         map.put("Card Games", "public.app-category.card-games");
       
    95         map.put("Casino Games", "public.app-category.casino-games");
       
    96         map.put("Dice Games", "public.app-category.dice-games");
       
    97         map.put("Educational Games", "public.app-category.educational-games");
       
    98         map.put("Family Games", "public.app-category.family-games");
       
    99         map.put("Kids Games", "public.app-category.kids-games");
       
   100         map.put("Music Games", "public.app-category.music-games");
       
   101         map.put("Puzzle Games", "public.app-category.puzzle-games");
       
   102         map.put("Racing Games", "public.app-category.racing-games");
       
   103         map.put("Role Playing Games", "public.app-category.role-playing-games");
       
   104         map.put("Simulation Games", "public.app-category.simulation-games");
       
   105         map.put("Sports Games", "public.app-category.sports-games");
       
   106         map.put("Strategy Games", "public.app-category.strategy-games");
       
   107         map.put("Trivia Games", "public.app-category.trivia-games");
       
   108         map.put("Word Games", "public.app-category.word-games");
       
   109 
       
   110         return map;
       
   111     }
       
   112 
       
   113     public static final EnumeratedBundlerParam<String> MAC_CATEGORY =
       
   114             new EnumeratedBundlerParam<>(
       
   115                     I18N.getString("param.category-name"),
       
   116                     I18N.getString("param.category-name.description"),
       
   117                     Arguments.CLIOptions.MAC_APP_STORE_CATEGORY.getId(),
       
   118                     String.class,
       
   119                     params -> params.containsKey(CATEGORY.getID())
       
   120                             ? CATEGORY.fetchFrom(params)
       
   121                             : "Unknown",
       
   122                     (s, p) -> s,
       
   123                     getMacCategories(),
       
   124                     false //strict - for MacStoreBundler this should be strict
       
   125             );
       
   126 
       
   127     public static final BundlerParamInfo<String> MAC_CF_BUNDLE_NAME =
       
   128             new StandardBundlerParam<>(
       
   129                     I18N.getString("param.cfbundle-name.name"),
       
   130                     I18N.getString("param.cfbundle-name.description"),
       
   131                     Arguments.CLIOptions.MAC_BUNDLE_NAME.getId(),
       
   132                     String.class,
       
   133                     params -> null,
       
   134                     (s, p) -> s);
       
   135 
       
   136     public static final BundlerParamInfo<String> MAC_CF_BUNDLE_IDENTIFIER =
       
   137             new StandardBundlerParam<>(
       
   138                     I18N.getString("param.cfbundle-identifier.name"),
       
   139                     I18N.getString("param.cfbundle-identifier.description"),
       
   140                     Arguments.CLIOptions.MAC_BUNDLE_IDENTIFIER.getId(),
       
   141                     String.class,
       
   142                     IDENTIFIER::fetchFrom,
       
   143                     (s, p) -> s);
       
   144 
       
   145     public static final BundlerParamInfo<String> MAC_CF_BUNDLE_VERSION =
       
   146             new StandardBundlerParam<>(
       
   147                     I18N.getString("param.cfbundle-version.name"),
       
   148                     I18N.getString("param.cfbundle-version.description"),
       
   149                     "mac.CFBundleVersion",
       
   150                     String.class,
       
   151                     p -> {
       
   152                         String s = VERSION.fetchFrom(p);
       
   153                         if (validCFBundleVersion(s)) {
       
   154                             return s;
       
   155                         } else {
       
   156                             return "100";
       
   157                         }
       
   158                     },
       
   159                     (s, p) -> s);
       
   160 
       
   161     public static final BundlerParamInfo<File> CONFIG_ROOT =
       
   162             new StandardBundlerParam<>(
       
   163             I18N.getString("param.config-root.name"),
       
   164             I18N.getString("param.config-root.description"),
       
   165             "configRoot",
       
   166             File.class,
       
   167             params -> {
       
   168                 File configRoot =
       
   169                         new File(BUILD_ROOT.fetchFrom(params), "macosx");
       
   170                 configRoot.mkdirs();
       
   171                 return configRoot;
       
   172             },
       
   173             (s, p) -> new File(s));
       
   174 
       
   175     public static final BundlerParamInfo<String> DEFAULT_ICNS_ICON =
       
   176             new StandardBundlerParam<>(
       
   177             I18N.getString("param.default-icon-icns"),
       
   178             I18N.getString("param.default-icon-icns.description"),
       
   179             ".mac.default.icns",
       
   180             String.class,
       
   181             params -> TEMPLATE_BUNDLE_ICON,
       
   182             (s, p) -> s);
       
   183 
       
   184     public static final BundlerParamInfo<String> DEVELOPER_ID_APP_SIGNING_KEY =
       
   185             new StandardBundlerParam<>(
       
   186             I18N.getString("param.signing-key-developer-id-app.name"),
       
   187             I18N.getString("param.signing-key-developer-id-app.description"),
       
   188             "mac.signing-key-developer-id-app",
       
   189             String.class,
       
   190             params -> {
       
   191                     String result = MacBaseInstallerBundler.findKey(
       
   192                             "Developer ID Application: "
       
   193                             + SIGNING_KEY_USER.fetchFrom(params),
       
   194                             SIGNING_KEYCHAIN.fetchFrom(params),
       
   195                             VERBOSE.fetchFrom(params));
       
   196                     if (result != null) {
       
   197                         MacCertificate certificate = new MacCertificate(result,
       
   198                                 VERBOSE.fetchFrom(params));
       
   199 
       
   200                         if (!certificate.isValid()) {
       
   201                             Log.error(MessageFormat.format(I18N.getString(
       
   202                                     "error.certificate.expired"), result));
       
   203                         }
       
   204                     }
       
   205 
       
   206                     return result;
       
   207                 },
       
   208             (s, p) -> s);
       
   209 
       
   210     public static final BundlerParamInfo<String> BUNDLE_ID_SIGNING_PREFIX =
       
   211             new StandardBundlerParam<>(
       
   212             I18N.getString("param.bundle-id-signing-prefix.name"),
       
   213             I18N.getString("param.bundle-id-signing-prefix.description"),
       
   214             Arguments.CLIOptions.MAC_BUNDLE_SIGNING_PREFIX.getId(),
       
   215             String.class,
       
   216             params -> IDENTIFIER.fetchFrom(params) + ".",
       
   217             (s, p) -> s);
       
   218 
       
   219     public static final BundlerParamInfo<File> ICON_ICNS =
       
   220             new StandardBundlerParam<>(
       
   221             I18N.getString("param.icon-icns.name"),
       
   222             I18N.getString("param.icon-icns.description"),
       
   223             "icon.icns",
       
   224             File.class,
       
   225             params -> {
       
   226                 File f = ICON.fetchFrom(params);
       
   227                 if (f != null && !f.getName().toLowerCase().endsWith(".icns")) {
       
   228                     Log.error(MessageFormat.format(
       
   229                             I18N.getString("message.icon-not-icns"), f));
       
   230                     return null;
       
   231                 }
       
   232                 return f;
       
   233             },
       
   234             (s, p) -> new File(s));
       
   235 
       
   236     public MacAppBundler() {
       
   237         super();
       
   238         baseResourceLoader = MacResources.class;
       
   239     }
       
   240 
       
   241     public static boolean validCFBundleVersion(String v) {
       
   242         // CFBundleVersion (String - iOS, OS X) specifies the build version
       
   243         // number of the bundle, which identifies an iteration (released or
       
   244         // unreleased) of the bundle. The build version number should be a
       
   245         // string comprised of three non-negative, period-separated integers
       
   246         // with the first integer being greater than zero. The string should
       
   247         // only contain numeric (0-9) and period (.) characters. Leading zeros
       
   248         // are truncated from each integer and will be ignored (that is,
       
   249         // 1.02.3 is equivalent to 1.2.3). This key is not localizable.
       
   250 
       
   251         if (v == null) {
       
   252             return false;
       
   253         }
       
   254 
       
   255         String p[] = v.split("\\.");
       
   256         if (p.length > 3 || p.length < 1) {
       
   257             Log.verbose(I18N.getString(
       
   258                     "message.version-string-too-many-components"));
       
   259             return false;
       
   260         }
       
   261 
       
   262         try {
       
   263             BigInteger n = new BigInteger(p[0]);
       
   264             if (BigInteger.ONE.compareTo(n) > 0) {
       
   265                 Log.verbose(I18N.getString(
       
   266                         "message.version-string-first-number-not-zero"));
       
   267                 return false;
       
   268             }
       
   269             if (p.length > 1) {
       
   270                 n = new BigInteger(p[1]);
       
   271                 if (BigInteger.ZERO.compareTo(n) > 0) {
       
   272                     Log.verbose(I18N.getString(
       
   273                             "message.version-string-no-negative-numbers"));
       
   274                     return false;
       
   275                 }
       
   276             }
       
   277             if (p.length > 2) {
       
   278                 n = new BigInteger(p[2]);
       
   279                 if (BigInteger.ZERO.compareTo(n) > 0) {
       
   280                     Log.verbose(I18N.getString(
       
   281                             "message.version-string-no-negative-numbers"));
       
   282                     return false;
       
   283                 }
       
   284             }
       
   285         } catch (NumberFormatException ne) {
       
   286             Log.verbose(I18N.getString("message.version-string-numbers-only"));
       
   287             Log.verbose(ne);
       
   288             return false;
       
   289         }
       
   290 
       
   291         return true;
       
   292     }
       
   293 
       
   294     @Override
       
   295     public boolean validate(Map<String, ? super Object> params)
       
   296             throws UnsupportedPlatformException, ConfigException {
       
   297         try {
       
   298             return doValidate(params);
       
   299         } catch (RuntimeException re) {
       
   300             if (re.getCause() instanceof ConfigException) {
       
   301                 throw (ConfigException) re.getCause();
       
   302             } else {
       
   303                 throw new ConfigException(re);
       
   304             }
       
   305         }
       
   306     }
       
   307 
       
   308     // to be used by chained bundlers, e.g. by EXE bundler to avoid
       
   309     // skipping validation if p.type does not include "image"
       
   310     public boolean doValidate(Map<String, ? super Object> p)
       
   311             throws UnsupportedPlatformException, ConfigException {
       
   312         if (Platform.getPlatform() != Platform.MAC) {
       
   313             throw new UnsupportedPlatformException();
       
   314         }
       
   315 
       
   316         imageBundleValidation(p);
       
   317 
       
   318         if (StandardBundlerParam.getPredefinedAppImage(p) != null) {
       
   319             return true;
       
   320         }
       
   321 
       
   322         // validate short version
       
   323         if (!validCFBundleVersion(MAC_CF_BUNDLE_VERSION.fetchFrom(p))) {
       
   324             throw new ConfigException(
       
   325                     I18N.getString("error.invalid-cfbundle-version"),
       
   326                     I18N.getString("error.invalid-cfbundle-version.advice"));
       
   327         }
       
   328 
       
   329         // reject explicitly set sign to true and no valid signature key
       
   330         if (Optional.ofNullable(MacAppImageBuilder.
       
   331                     SIGN_BUNDLE.fetchFrom(p)).orElse(Boolean.FALSE)) {
       
   332             String signingIdentity = DEVELOPER_ID_APP_SIGNING_KEY.fetchFrom(p);
       
   333             if (signingIdentity == null) {
       
   334                 throw new ConfigException(
       
   335                         I18N.getString("error.explicit-sign-no-cert"),
       
   336                         I18N.getString("error.explicit-sign-no-cert.advice"));
       
   337             }
       
   338         }
       
   339 
       
   340         return true;
       
   341     }
       
   342 
       
   343     private File getConfig_InfoPlist(Map<String, ? super Object> params) {
       
   344         return new File(CONFIG_ROOT.fetchFrom(params), "Info.plist");
       
   345     }
       
   346 
       
   347     private File getConfig_Icon(Map<String, ? super Object> params) {
       
   348         return new File(CONFIG_ROOT.fetchFrom(params),
       
   349                 APP_NAME.fetchFrom(params) + ".icns");
       
   350     }
       
   351 
       
   352     File doBundle(Map<String, ? super Object> p, File outputDirectory,
       
   353             boolean dependentTask) {
       
   354         if (Arguments.CREATE_JRE_INSTALLER.fetchFrom(p)) {
       
   355             return doJreBundle(p, outputDirectory, dependentTask);
       
   356         } else {
       
   357             return doAppBundle(p, outputDirectory, dependentTask);
       
   358         }
       
   359     }
       
   360 
       
   361     File doJreBundle(Map<String, ? super Object> p,
       
   362             File outputDirectory, boolean dependentTask) {
       
   363         try {
       
   364             File rootDirectory = createRoot(p, outputDirectory, dependentTask,
       
   365                     APP_NAME.fetchFrom(p), "macapp-image-builder");
       
   366             AbstractAppImageBuilder appBuilder = new MacAppImageBuilder(p,
       
   367                     APP_NAME.fetchFrom(p), outputDirectory.toPath());
       
   368             File predefined = PREDEFINED_RUNTIME_IMAGE.fetchFrom(p);
       
   369             if (predefined == null ) {
       
   370                 JLinkBundlerHelper.generateServerJre(p, appBuilder);
       
   371             } else {
       
   372                 return predefined;
       
   373             }
       
   374             return rootDirectory;
       
   375         } catch (Exception ex) {
       
   376             Log.error("Exception: "+ex);
       
   377             Log.verbose(ex);
       
   378             return null;
       
   379         }
       
   380     }
       
   381 
       
   382     File doAppBundle(Map<String, ? super Object> p, File outputDirectory,
       
   383             boolean dependentTask) {
       
   384         try {
       
   385             File rootDirectory = createRoot(p, outputDirectory, dependentTask,
       
   386                     APP_NAME.fetchFrom(p) + ".app", "macapp-image-builder");
       
   387             AbstractAppImageBuilder appBuilder =
       
   388                     new MacAppImageBuilder(p, outputDirectory.toPath());
       
   389             if (PREDEFINED_RUNTIME_IMAGE.fetchFrom(p) == null ) {
       
   390                 JLinkBundlerHelper.execute(p, appBuilder);
       
   391             } else {
       
   392                 StandardBundlerParam.copyPredefinedRuntimeImage(p, appBuilder);
       
   393             }
       
   394             return rootDirectory;
       
   395         } catch (Exception ex) {
       
   396             Log.error("Exception: "+ex);
       
   397             Log.verbose(ex);
       
   398             return null;
       
   399         }
       
   400     }
       
   401 
       
   402     public void cleanupConfigFiles(Map<String, ? super Object> params) {
       
   403         if (Log.isDebug() || Log.isVerbose()) {
       
   404             return;
       
   405         }
       
   406 
       
   407         if (CONFIG_ROOT.fetchFrom(params) != null) {
       
   408             getConfig_Icon(params).delete();
       
   409             getConfig_InfoPlist(params).delete();
       
   410         }
       
   411     }
       
   412 
       
   413     /////////////////////////////////////////////////////////////////////////
       
   414     // Implement Bundler
       
   415     /////////////////////////////////////////////////////////////////////////
       
   416 
       
   417     @Override
       
   418     public String getName() {
       
   419         return I18N.getString("bundler.name");
       
   420     }
       
   421 
       
   422     @Override
       
   423     public String getDescription() {
       
   424         return I18N.getString("bundler.description");
       
   425     }
       
   426 
       
   427     @Override
       
   428     public String getID() {
       
   429         return "mac.app";
       
   430     }
       
   431 
       
   432     @Override
       
   433     public String getBundleType() {
       
   434         return "IMAGE";
       
   435     }
       
   436 
       
   437     @Override
       
   438     public Collection<BundlerParamInfo<?>> getBundleParameters() {
       
   439         return getAppBundleParameters();
       
   440     }
       
   441 
       
   442     public static Collection<BundlerParamInfo<?>> getAppBundleParameters() {
       
   443         return Arrays.asList(
       
   444                 APP_NAME,
       
   445                 APP_RESOURCES,
       
   446                 ARGUMENTS,
       
   447                 BUNDLE_ID_SIGNING_PREFIX,
       
   448                 CLASSPATH,
       
   449                 DEVELOPER_ID_APP_SIGNING_KEY,
       
   450                 ICON_ICNS,
       
   451                 JVM_OPTIONS,
       
   452                 MAC_CATEGORY,
       
   453                 MAC_CF_BUNDLE_IDENTIFIER,
       
   454                 MAC_CF_BUNDLE_NAME,
       
   455                 MAC_CF_BUNDLE_VERSION,
       
   456                 MAIN_CLASS,
       
   457                 MAIN_JAR,
       
   458                 PREFERENCES_ID,
       
   459                 SIGNING_KEYCHAIN,
       
   460                 VERSION,
       
   461                 VERBOSE
       
   462         );
       
   463     }
       
   464 
       
   465 
       
   466     @Override
       
   467     public File execute(Map<String, ? super Object> params,
       
   468             File outputParentDir) {
       
   469         return doBundle(params, outputParentDir, false);
       
   470     }
       
   471 
       
   472     @Override
       
   473     public boolean supported() {
       
   474         return Platform.getPlatform() == Platform.MAC;
       
   475     }
       
   476 
       
   477 }