8210367: invalid --app-image arg causes exception runnning light.exe in windows JDK-8200758-branch
authorherrick
Fri, 21 Sep 2018 09:32:02 -0400
branchJDK-8200758-branch
changeset 56902 6972c0e75e23
parent 56901 d5992bef7aa9
child 56910 410fd33a2c81
8210367: invalid --app-image arg causes exception runnning light.exe in windows Submitten-by: almatvee Reviewed-by: herrick
src/jdk.packager/share/classes/jdk/packager/internal/DeployParams.java
src/jdk.packager/share/classes/jdk/packager/internal/resources/Bundle.properties
src/jdk.packager/windows/classes/jdk/packager/internal/resources/windows/WinAppBundler.properties
src/jdk.packager/windows/classes/jdk/packager/internal/windows/WinAppBundler.java
--- a/src/jdk.packager/share/classes/jdk/packager/internal/DeployParams.java	Fri Sep 21 09:30:03 2018 -0400
+++ b/src/jdk.packager/share/classes/jdk/packager/internal/DeployParams.java	Fri Sep 21 09:32:02 2018 -0400
@@ -88,7 +88,7 @@
     String updateMode = "background";
     boolean isExtension = false;
     boolean isSwingApp = false;
-    
+
     boolean jreInstaller = false;
 
     Boolean needShortcut = null;
@@ -490,6 +490,21 @@
                 throw new PackagerException("ERR_MissingArgument", "--main-jar");
             }
         }
+
+        // Validate app image if set
+        String appImage = (String)bundlerArguments.get(Arguments.CLIOptions.PREDEFINED_APP_IMAGE.getId());
+        if (appImage != null) {
+            File appImageDir = new File(appImage);
+            if (!appImageDir.exists()) {
+                throw new PackagerException("ERR_AppImageNotExist", appImage);
+            }
+
+            File appImageAppDir = new File(appImage + File.separator + "app");
+            File appImageRuntimeDir = new File(appImage + File.separator + "runtime");
+            if (!appImageAppDir.exists() || !appImageRuntimeDir.exists()) {
+                throw new PackagerException("ERR_AppImageInvalid", appImage);
+            }
+        }
     }
 
     public boolean validateForBundle() {
@@ -662,7 +677,7 @@
 
         Map<String, String> unescapedHtmlParams = new TreeMap<>();
         Map<String, String> escapedHtmlParams = new TreeMap<>();
-        
+
         // check for collisions
         TreeSet<String> keys = new TreeSet<>(bundlerArguments.keySet());
         keys.retainAll(bundleParams.getBundleParamsAsMap().keySet());
--- a/src/jdk.packager/share/classes/jdk/packager/internal/resources/Bundle.properties	Fri Sep 21 09:30:03 2018 -0400
+++ b/src/jdk.packager/share/classes/jdk/packager/internal/resources/Bundle.properties	Fri Sep 21 09:32:02 2018 -0400
@@ -24,6 +24,8 @@
 ERR_SignFailed=Error: Signing failed
 ERR_MissingAppResources=Error: No application jars found
 ERR_NoEmbeddedDT=Error: -includedt requires the java deployment toolkit, which is not included in this distribution
+ERR_AppImageNotExist=Error: App image directory "{0}" does not exist
+ERR_AppImageInvalid=Error: App image directory "{0}" is invalid and does not contain "app" and/or "runtime" sub-directories
 
 MSG_UpdatingJar=Updating jar file\: {0}
 MSG_NoJREPackaged=Package is configured to ship without a JRE.
--- a/src/jdk.packager/windows/classes/jdk/packager/internal/resources/windows/WinAppBundler.properties	Fri Sep 21 09:30:03 2018 -0400
+++ b/src/jdk.packager/windows/classes/jdk/packager/internal/resources/windows/WinAppBundler.properties	Fri Sep 21 09:32:02 2018 -0400
@@ -23,6 +23,7 @@
 error.bit-architecture-mismatch.advice=Make sure to use JRE runtime with correct bit architecture.
 error.cannot-create-output-dir=Output directory {0} cannot be created.
 error.cannot-write-to-output-dir=Output directory {0} is not writable.
+error.cannot-find-launcher=Cannot find cfg file in predefined app image directory {0}.
 
 message.creating-app-bundle=Creating app bundle\: {0} in {1}
 message.result-dir=Result application bundle\: {0}
--- a/src/jdk.packager/windows/classes/jdk/packager/internal/windows/WinAppBundler.java	Fri Sep 21 09:30:03 2018 -0400
+++ b/src/jdk.packager/windows/classes/jdk/packager/internal/windows/WinAppBundler.java	Fri Sep 21 09:32:02 2018 -0400
@@ -51,6 +51,7 @@
 
 import static jdk.packager.internal.windows.WindowsBundlerParam.*;
 import jdk.packager.internal.builders.AbstractAppImageBuilder;
+import static jdk.packager.internal.windows.WinMsiBundler.WIN_APP_IMAGE;
 
 public class WinAppBundler extends AbstractImageBundler {
 
@@ -153,18 +154,52 @@
         return new File(outDir, APP_NAME.fetchFrom(p));
     }
 
+    private static boolean usePredefineAppName(Map<String, ? super Object> p) {
+        return (PREDEFINED_APP_IMAGE.fetchFrom(p) != null);
+    }
+
+    private static String appName;
+    private synchronized static String getAppName(Map<String, ? super Object> p) {
+        // If we building from predefined app image, then we should use names
+        // from image and not from CLI.
+        if (usePredefineAppName(p)) {
+            if (appName == null) {
+                // Use WIN_APP_IMAGE here, since we already copy pre-defined image to WIN_APP_IMAGE
+                File appImageDir = new File(WIN_APP_IMAGE.fetchFrom(p).toString() + "\\app");
+                File [] files = appImageDir.listFiles((File dir, String name) -> name.endsWith(".cfg"));
+                if (files == null || files.length != 1) {
+                    throw new RuntimeException(MessageFormat.format(
+                        I18N.getString("error.cannot-find-cfg"),
+                        appImageDir));
+                } else {
+                    appName = files[0].getName();
+                    int index = appName.indexOf(".");
+                    if (index != -1) {
+                        appName = appName.substring(0, index);
+                    }
+                }
+
+                return appName;
+            } else {
+                return appName;
+            }
+        }
+
+        return APP_NAME.fetchFrom(p);
+    }
+
     public static String getLauncherName(Map<String, ? super Object> p) {
-        return APP_NAME.fetchFrom(p) +".exe";
+        return getAppName(p) + ".exe";
     }
 
     public static String getLauncherCfgName(Map<String, ? super Object> p) {
-        return "app\\" + APP_NAME.fetchFrom(p) +".cfg";
+        return "app\\" + getAppName(p) +".cfg";
     }
 
     public boolean bundle(Map<String, ? super Object> p, File outputDirectory) {
         return doBundle(p, outputDirectory, false) != null;
     }
-    
+
     private File createRoot(Map<String, ? super Object> p,
             File outputDirectory, boolean dependentTask) throws IOException {
         if (!outputDirectory.isDirectory() && !outputDirectory.mkdirs()) {
@@ -338,8 +373,8 @@
             Map<String, ? super Object> params, File outputParentDir) {
         return doBundle(params, outputParentDir, false);
     }
-    
-    @Override    
+
+    @Override
     public boolean supported() {
         return (Platform.getPlatform() == Platform.WINDOWS);
     }