8231281: Eliminate the --identifier option JDK-8200758-branch
authorherrick
Mon, 30 Sep 2019 15:59:50 -0400
branchJDK-8200758-branch
changeset 58415 73f8e557549a
parent 58414 a5f66aa04f68
child 58416 f09bf58c1f17
8231281: Eliminate the --identifier option Submitted-by: almatvee Reviewed-by: herrick, asemenyuk
src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/MacAppBundler.java
src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/MacAppImageBuilder.java
src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/MacBaseInstallerBundler.java
src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/MacPkgBundler.java
src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/resources/MacResources.properties
src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/resources/MacResources_ja.properties
src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/resources/MacResources_zh_CN.properties
src/jdk.jpackage/share/classes/jdk/jpackage/internal/Arguments.java
src/jdk.jpackage/share/classes/jdk/jpackage/internal/StandardBundlerParam.java
src/jdk.jpackage/share/classes/jdk/jpackage/internal/ValidOptions.java
src/jdk.jpackage/share/classes/jdk/jpackage/internal/resources/HelpResources.properties
src/jdk.jpackage/share/classes/jdk/jpackage/internal/resources/HelpResources_ja.properties
src/jdk.jpackage/share/classes/jdk/jpackage/internal/resources/HelpResources_zh_CN.properties
test/jdk/tools/jpackage/share/AppImagePackageTest.java
--- a/src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/MacAppBundler.java	Mon Sep 30 15:13:14 2019 -0400
+++ b/src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/MacAppBundler.java	Mon Sep 30 15:59:50 2019 -0400
@@ -109,13 +109,6 @@
                     params -> null,
                     (s, p) -> s);
 
-    public static final BundlerParamInfo<String> MAC_CF_BUNDLE_IDENTIFIER =
-            new StandardBundlerParam<>(
-                    Arguments.CLIOptions.MAC_BUNDLE_IDENTIFIER.getId(),
-                    String.class,
-                    IDENTIFIER::fetchFrom,
-                    (s, p) -> s);
-
     public static final BundlerParamInfo<String> MAC_CF_BUNDLE_VERSION =
             new StandardBundlerParam<>(
                     "mac.CFBundleVersion",
--- a/src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/MacAppImageBuilder.java	Mon Sep 30 15:13:14 2019 -0400
+++ b/src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/MacAppImageBuilder.java	Mon Sep 30 15:59:50 2019 -0400
@@ -54,6 +54,11 @@
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Consumer;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathFactory;
 
 import static jdk.jpackage.internal.StandardBundlerParam.*;
 import static jdk.jpackage.internal.MacBaseInstallerBundler.*;
@@ -112,7 +117,16 @@
             new StandardBundlerParam<>(
                     Arguments.CLIOptions.MAC_BUNDLE_IDENTIFIER.getId(),
                     String.class,
-                    IDENTIFIER::fetchFrom,
+                    params -> {
+                        // Get identifier from app image if user provided
+                        // app image and did not provide the identifier via CLI.
+                        String identifier = extractBundleIdentifier(params);
+                        if (identifier != null) {
+                            return identifier;
+                        }
+
+                        return IDENTIFIER.fetchFrom(params);
+                    },
                     (s, p) -> s);
 
     public static final BundlerParamInfo<String> MAC_CF_BUNDLE_VERSION =
@@ -929,4 +943,39 @@
         return true;
     }
 
+    private static String extractBundleIdentifier(Map<String, Object> params) {
+        if (PREDEFINED_APP_IMAGE.fetchFrom(params) == null) {
+            return null;
+        }
+
+        try {
+            File infoPList = new File(PREDEFINED_APP_IMAGE.fetchFrom(params) +
+                                      File.separator + "Contents" +
+                                      File.separator + "Info.plist");
+
+            DocumentBuilderFactory dbf
+                    = DocumentBuilderFactory.newDefaultInstance();
+            dbf.setFeature("http://apache.org/xml/features/" +
+                           "nonvalidating/load-external-dtd", false);
+            DocumentBuilder b = dbf.newDocumentBuilder();
+            org.w3c.dom.Document doc = b.parse(new FileInputStream(
+                    infoPList.getAbsolutePath()));
+
+            XPath xPath = XPathFactory.newInstance().newXPath();
+            // Query for the value of <string> element preceding <key>
+            // element with value equal to CFBundleIdentifier
+            String v = (String) xPath.evaluate(
+                    "//string[preceding-sibling::key = \"CFBundleIdentifier\"][1]",
+                    doc, XPathConstants.STRING);
+
+            if (v != null && !v.isEmpty()) {
+                return v;
+            }
+        } catch (Exception ex) {
+            Log.verbose(ex);
+        }
+
+        return null;
+    }
+
 }
--- a/src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/MacBaseInstallerBundler.java	Mon Sep 30 15:13:14 2019 -0400
+++ b/src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/MacBaseInstallerBundler.java	Mon Sep 30 15:59:50 2019 -0400
@@ -32,9 +32,6 @@
 import java.nio.file.Files;
 import java.text.MessageFormat;
 import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.ResourceBundle;
@@ -123,12 +120,6 @@
                         I18N.getString(
                             "message.app-image-requires-app-name.advice"));
             }
-            if (IDENTIFIER.fetchFrom(params) == null) {
-                throw new ConfigException(
-                        I18N.getString("message.app-image-requires-identifier"),
-                        I18N.getString(
-                            "message.app-image-requires-identifier.advice"));
-            }
         } else {
             APP_BUNDLER.fetchFrom(params).validate(params);
         }
--- a/src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/MacPkgBundler.java	Mon Sep 30 15:13:14 2019 -0400
+++ b/src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/MacPkgBundler.java	Mon Sep 30 15:59:50 2019 -0400
@@ -25,9 +25,7 @@
 
 package jdk.jpackage.internal;
 
-import java.io.BufferedWriter;
 import java.io.File;
-import java.io.FileWriter;
 import java.io.IOException;
 import java.io.PrintStream;
 import java.io.PrintWriter;
@@ -37,10 +35,7 @@
 import java.nio.file.Path;
 import java.text.MessageFormat;
 import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
 import java.util.HashMap;
-import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
@@ -49,6 +44,7 @@
 import static jdk.jpackage.internal.StandardBundlerParam.*;
 import static jdk.jpackage.internal.MacBaseInstallerBundler.SIGNING_KEYCHAIN;
 import static jdk.jpackage.internal.MacBaseInstallerBundler.SIGNING_KEY_USER;
+import static jdk.jpackage.internal.MacAppImageBuilder.MAC_CF_BUNDLE_IDENTIFIER;
 
 public class MacPkgBundler extends MacBaseInstallerBundler {
 
@@ -495,6 +491,13 @@
             // we are not interested in return code, only possible exception
             validateAppImageAndBundeler(params);
 
+            if (MAC_CF_BUNDLE_IDENTIFIER.fetchFrom(params) == null) {
+                throw new ConfigException(
+                        I18N.getString("message.app-image-requires-identifier"),
+                        I18N.getString(
+                            "message.app-image-requires-identifier.advice"));
+            }
+
             // reject explicitly set sign to true and no valid signature key
             if (Optional.ofNullable(MacAppImageBuilder.
                     SIGN_BUNDLE.fetchFrom(params)).orElse(Boolean.FALSE)) {
--- a/src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/resources/MacResources.properties	Mon Sep 30 15:13:14 2019 -0400
+++ b/src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/resources/MacResources.properties	Mon Sep 30 15:59:50 2019 -0400
@@ -76,8 +76,8 @@
 message.app-image-dir-does-not-exist.advice=Confirm that the value for {0} exists.
 message.app-image-requires-app-name=When using an external app image you must specify the app name.
 message.app-image-requires-app-name.advice=Set the app name via the -name CLI flag, the fx:application/@name ANT attribute, or via the 'appName' bundler argument.
-message.app-image-requires-identifier=When using an external app image you must specify the identifier.
-message.app-image-requires-identifier.advice=Set the identifier via the -appId CLI flag, the fx:application/@id ANT attribute, or via the 'identifier' bundler argument.
+message.app-image-requires-identifier=Unable to extract identifier from app image.
+message.app-image-requires-identifier.advice=Use "--verbose" for extended error message or specify it via "--mac-package-identifier".
 message.building-dmg=Building DMG package for {0}.
 message.running-script=Running shell script on application image [{0}].
 message.preparing-dmg-setup=Preparing dmg setup: {0}.
--- a/src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/resources/MacResources_ja.properties	Mon Sep 30 15:13:14 2019 -0400
+++ b/src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/resources/MacResources_ja.properties	Mon Sep 30 15:59:50 2019 -0400
@@ -76,8 +76,8 @@
 message.app-image-dir-does-not-exist.advice=Confirm that the value for {0} exists.
 message.app-image-requires-app-name=When using an external app image you must specify the app name.
 message.app-image-requires-app-name.advice=Set the app name via the -name CLI flag, the fx:application/@name ANT attribute, or via the 'appName' bundler argument.
-message.app-image-requires-identifier=When using an external app image you must specify the identifier.
-message.app-image-requires-identifier.advice=Set the identifier via the -appId CLI flag, the fx:application/@id ANT attribute, or via the 'identifier' bundler argument.
+message.app-image-requires-identifier=Unable to extract identifier from app image.
+message.app-image-requires-identifier.advice=Use "--verbose" for extended error message or specify it via "--mac-package-identifier".
 message.building-dmg=Building DMG package for {0}.
 message.running-script=Running shell script on application image [{0}].
 message.preparing-dmg-setup=Preparing dmg setup: {0}.
--- a/src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/resources/MacResources_zh_CN.properties	Mon Sep 30 15:13:14 2019 -0400
+++ b/src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/resources/MacResources_zh_CN.properties	Mon Sep 30 15:59:50 2019 -0400
@@ -76,8 +76,8 @@
 message.app-image-dir-does-not-exist.advice=Confirm that the value for {0} exists.
 message.app-image-requires-app-name=When using an external app image you must specify the app name.
 message.app-image-requires-app-name.advice=Set the app name via the -name CLI flag, the fx:application/@name ANT attribute, or via the 'appName' bundler argument.
-message.app-image-requires-identifier=When using an external app image you must specify the identifier.
-message.app-image-requires-identifier.advice=Set the identifier via the -appId CLI flag, the fx:application/@id ANT attribute, or via the 'identifier' bundler argument.
+message.app-image-requires-identifier=Unable to extract identifier from app image.
+message.app-image-requires-identifier.advice=Use "--verbose" for extended error message or specify it via "--mac-package-identifier".
 message.building-dmg=Building DMG package for {0}.
 message.running-script=Running shell script on application image [{0}].
 message.preparing-dmg-setup=Preparing dmg setup: {0}.
--- a/src/jdk.jpackage/share/classes/jdk/jpackage/internal/Arguments.java	Mon Sep 30 15:13:14 2019 -0400
+++ b/src/jdk.jpackage/share/classes/jdk/jpackage/internal/Arguments.java	Mon Sep 30 15:59:50 2019 -0400
@@ -154,8 +154,6 @@
 
         NAME ("name", "n", OptionCategories.PROPERTY),
 
-        IDENTIFIER ("identifier", OptionCategories.PROPERTY),
-
         VERBOSE ("verbose", OptionCategories.PROPERTY, () -> {
             setOptionValue("verbose", true);
             Log.setVerbose();
--- a/src/jdk.jpackage/share/classes/jdk/jpackage/internal/StandardBundlerParam.java	Mon Sep 30 15:13:14 2019 -0400
+++ b/src/jdk.jpackage/share/classes/jdk/jpackage/internal/StandardBundlerParam.java	Mon Sep 30 15:59:50 2019 -0400
@@ -306,7 +306,7 @@
 
     static final StandardBundlerParam<String> IDENTIFIER =
             new StandardBundlerParam<>(
-                    Arguments.CLIOptions.IDENTIFIER.getId(),
+                    "identifier.default",
                     String.class,
                     params -> {
                         String s = MAIN_CLASS.fetchFrom(params);
--- a/src/jdk.jpackage/share/classes/jdk/jpackage/internal/ValidOptions.java	Mon Sep 30 15:13:14 2019 -0400
+++ b/src/jdk.jpackage/share/classes/jdk/jpackage/internal/ValidOptions.java	Mon Sep 30 15:59:50 2019 -0400
@@ -69,7 +69,6 @@
         options.put(CLIOptions.VERBOSE.getId(), USE.ALL);
         options.put(CLIOptions.PREDEFINED_RUNTIME_IMAGE.getId(), USE.ALL);
         options.put(CLIOptions.RESOURCE_DIR.getId(), USE.ALL);
-        options.put(CLIOptions.IDENTIFIER.getId(), USE.ALL);
         options.put(CLIOptions.DESCRIPTION.getId(), USE.ALL);
         options.put(CLIOptions.VENDOR.getId(), USE.ALL);
         options.put(CLIOptions.COPYRIGHT.getId(), USE.ALL);
--- a/src/jdk.jpackage/share/classes/jdk/jpackage/internal/resources/HelpResources.properties	Mon Sep 30 15:13:14 2019 -0400
+++ b/src/jdk.jpackage/share/classes/jdk/jpackage/internal/resources/HelpResources.properties	Mon Sep 30 15:59:50 2019 -0400
@@ -169,10 +169,6 @@
 \          The keys "extension", "mime-type", "icon", and "description"\n\
 \          can be used to describe the association.\n\
 \          This option can be used multiple times.\n\
-\  --identifier <id string>\n\
-\          An identifier that uniquely identifies the application\n\
-\          Defaults to the main class name.\n\
-\          The value should be a valid DNS name.\n\
 \  --install-dir <file path>\n\
 \          {4}\
 \  --license-file <file path>\n\
@@ -219,7 +215,7 @@
 MSG_Help_mac_launcher=\
 \  --mac-package-identifier <ID string>\n\
 \          An identifier that uniquely identifies the application for macOS\n\
-\          Defaults to the value of --identifier option.\n\
+\          Defaults to the main class name.\n\
 \          May only use alphanumeric (A-Z,a-z,0-9), hyphen (-),\n\
 \          and period (.) characters.\n\
 \  --mac-package-name <name string>\n\
--- a/src/jdk.jpackage/share/classes/jdk/jpackage/internal/resources/HelpResources_ja.properties	Mon Sep 30 15:13:14 2019 -0400
+++ b/src/jdk.jpackage/share/classes/jdk/jpackage/internal/resources/HelpResources_ja.properties	Mon Sep 30 15:59:50 2019 -0400
@@ -169,10 +169,6 @@
 \          The keys "extension", "mime-type", "icon", and "description"\n\
 \          can be used to describe the association.\n\
 \          This option can be used multiple times.\n\
-\  --identifier <id string>\n\
-\          An identifier that uniquely identifies the application\n\
-\          Defaults to the main class name.\n\
-\          The value should be a valid DNS name.\n\
 \  --install-dir <file path>\n\
 \          {4}\
 \  --license-file <file path>\n\
@@ -219,7 +215,7 @@
 MSG_Help_mac_launcher=\
 \  --mac-package-identifier <ID string>\n\
 \          An identifier that uniquely identifies the application for macOS\n\
-\          Defaults to the value of --identifier option.\n\
+\          Defaults to the main class name.\n\
 \          May only use alphanumeric (A-Z,a-z,0-9), hyphen (-),\n\
 \          and period (.) characters.\n\
 \  --mac-package-name <name string>\n\
--- a/src/jdk.jpackage/share/classes/jdk/jpackage/internal/resources/HelpResources_zh_CN.properties	Mon Sep 30 15:13:14 2019 -0400
+++ b/src/jdk.jpackage/share/classes/jdk/jpackage/internal/resources/HelpResources_zh_CN.properties	Mon Sep 30 15:59:50 2019 -0400
@@ -169,10 +169,6 @@
 \          The keys "extension", "mime-type", "icon", and "description"\n\
 \          can be used to describe the association.\n\
 \          This option can be used multiple times.\n\
-\  --identifier <id string>\n\
-\          An identifier that uniquely identifies the application\n\
-\          Defaults to the main class name.\n\
-\          The value should be a valid DNS name.\n\
 \  --install-dir <file path>\n\
 \          {4}\
 \  --license-file <file path>\n\
@@ -219,7 +215,7 @@
 MSG_Help_mac_launcher=\
 \  --mac-package-identifier <ID string>\n\
 \          An identifier that uniquely identifies the application for macOS\n\
-\          Defaults to the value of --identifier option.\n\
+\          Defaults to the main class name.\n\
 \          May only use alphanumeric (A-Z,a-z,0-9), hyphen (-),\n\
 \          and period (.) characters.\n\
 \  --mac-package-name <name string>\n\
--- a/test/jdk/tools/jpackage/share/AppImagePackageTest.java	Mon Sep 30 15:13:14 2019 -0400
+++ b/test/jdk/tools/jpackage/share/AppImagePackageTest.java	Mon Sep 30 15:59:50 2019 -0400
@@ -61,7 +61,6 @@
                 if (PackageType.MAC.contains(cmd.packageType())) {
                     // Why so complicated on macOS?
                     appimageInput = Path.of(appimageInput.toString() + ".app");
-                    cmd.addArguments("--identifier", appImageCmd.name());
                 }
 
                 cmd.addArguments("--app-image", appimageInput);