changeset 10732:ca3b3494dccb

8191515: Add ToolProvider support for Java Packager Reviewed-by: vdrozdov, mchung, kcr
author almatvee
date Thu, 07 Dec 2017 13:13:58 -0800
parents f7287eee7a64
children 31c7f9661f09
files modules/jdk.packager/src/main/java/com/oracle/tools/packager/Log.java modules/jdk.packager/src/main/java/com/oracle/tools/packager/jnlp/JNLPBundler.java modules/jdk.packager/src/main/java/com/sun/javafx/tools/packager/DeployParams.java modules/jdk.packager/src/main/java/com/sun/javafx/tools/packager/Main.java modules/jdk.packager/src/main/java/com/sun/javafx/tools/packager/PackagerLib.java modules/jdk.packager/src/main/java/jdk/packager/internal/JavaPackagerToolProvider.java modules/jdk.packager/src/main/java/module-info.java modules/jdk.packager/src/test/java/hello/HelloToolProvider.java
diffstat 8 files changed, 234 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/modules/jdk.packager/src/main/java/com/oracle/tools/packager/Log.java	Thu Dec 07 10:50:25 2017 -0800
+++ b/modules/jdk.packager/src/main/java/com/oracle/tools/packager/Log.java	Thu Dec 07 13:13:58 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -28,33 +28,81 @@
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.PrintStream;
+import java.io.PrintWriter;
 
 public class Log {
     public static class Logger {
         private boolean verbose = false;
+        private PrintWriter out = null;
+        private PrintWriter err = null;
 
         public Logger(boolean v) {
             verbose = v;
         }
 
+        public void setVerbose(boolean v) {
+            verbose = v;
+        }
+
+        public void setPrintWriter(PrintWriter out, PrintWriter err) {
+            this.out = out;
+            this.err = err;
+        }
+
+        public void flush() {
+            if (out != null) {
+                out.flush();
+            }
+
+            if (err != null) {
+                err.flush();
+            }
+        }
+
         public void info(String msg) {
-            System.out.println(msg);
+            if (out != null) {
+                out.println(msg);
+            } else {
+                System.out.println(msg);
+            }
+        }
+
+        public void infof(String format, Object... args) {
+            if (out != null) {
+                out.printf(format, args);
+            } else {
+                System.out.printf(format, args);
+            }
+        }
+
+        public void error(String msg) {
+            if (err != null) {
+                err.println(msg);
+            } else {
+                System.err.println(msg);
+            }
         }
 
         public void verbose(Throwable t) {
-            if (Log.debug || verbose) {
+            if (out != null && (Log.debug || verbose)) {
+                t.printStackTrace(out);
+            } else if (Log.debug || verbose) {
                 t.printStackTrace(System.out);
             }
         }
 
         public void verbose(String msg) {
-            if (Log.debug || verbose) {
+            if (out != null && (Log.debug || verbose)) {
+                out.println(msg);
+            } else if (Log.debug || verbose) {
                 System.out.println(msg);
             }
         }
 
         public void debug(String msg) {
-            if (Log.debug) {
+            if (out != null && Log.debug) {
+                out.println(msg);
+            } else if (Log.debug) {
                 System.out.println(msg);
             }
         }
@@ -71,6 +119,15 @@
         }
     }
 
+    public static Logger getLogger() {
+        return delegate;
+    }
+
+    public static void flush() {
+        if (delegate != null) {
+            delegate.flush();
+        }
+    }
 
     public static void info(String msg) {
         if (delegate != null) {
@@ -78,6 +135,18 @@
         }
     }
 
+    public static void infof(String format, Object... args) {
+        if (delegate != null) {
+           delegate.infof(format, args);
+        }
+    }
+
+    public static void error(String msg) {
+        if (delegate != null) {
+            delegate.error(msg);
+        }
+    }
+
     public static void verbose(String msg) {
         if (delegate != null) {
            delegate.verbose(msg);
--- a/modules/jdk.packager/src/main/java/com/oracle/tools/packager/jnlp/JNLPBundler.java	Thu Dec 07 10:50:25 2017 -0800
+++ b/modules/jdk.packager/src/main/java/com/oracle/tools/packager/jnlp/JNLPBundler.java	Thu Dec 07 13:13:58 2017 -0800
@@ -602,7 +602,7 @@
                     for (Map.Entry<File, File> t: TEMPLATES.fetchFrom(params).entrySet()) {
                         File out = t.getValue();
                         if (out == null) {
-                            System.out.println(
+                            Log.info(
                                     "Perform inplace substitution for " +
                                             t.getKey().getAbsolutePath());
                             out = t.getKey();
@@ -988,7 +988,6 @@
     }
 
     private String xmlPrettyPrint(String s) throws TransformerException {
-//        System.out.println(s);
         TransformerFactory factory = TransformerFactory.newInstance();
 
         Transformer transformer = factory.newTransformer();
@@ -1350,7 +1349,7 @@
         for (String s: lst) {
             InputStream is = PackagerResource.class.getResourceAsStream(prefixWebFiles+s);
             if (is == null) {
-                System.err.println("Internal error. Missing resources [" +
+                Log.error("Internal error. Missing resources [" +
                         (prefixWebFiles+s) + "]");
                 return false;
             } else {
--- a/modules/jdk.packager/src/main/java/com/sun/javafx/tools/packager/DeployParams.java	Thu Dec 07 10:50:25 2017 -0800
+++ b/modules/jdk.packager/src/main/java/com/sun/javafx/tools/packager/DeployParams.java	Thu Dec 07 13:13:58 2017 -0800
@@ -307,7 +307,7 @@
     @Deprecated
     public void setEmbedCertifcates(boolean v) {
         if (v) {
-            System.out.println("JavaFX Packager no longer supports embedding certificates in JNLP files.  Setting will be ignored.");
+            Log.info("JavaFX Packager no longer supports embedding certificates in JNLP files.  Setting will be ignored.");
         }
     }
 
--- a/modules/jdk.packager/src/main/java/com/sun/javafx/tools/packager/Main.java	Thu Dec 07 10:50:25 2017 -0800
+++ b/modules/jdk.packager/src/main/java/com/sun/javafx/tools/packager/Main.java	Thu Dec 07 13:13:58 2017 -0800
@@ -34,6 +34,7 @@
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.PrintWriter;
 import java.text.MessageFormat;
 import java.util.*;
 import java.util.stream.Stream;
@@ -182,15 +183,34 @@
         return list;
     }
 
+    public static void main(String... args) throws Exception {
+        // Create logger with default system.out and system.err
+        Log.Logger logger = new Log.Logger(false);
+        Log.setLogger(logger);
+
+        int status = run(args);
+        System.exit(status);
+    }
+
+    public static int run(PrintWriter out, PrintWriter err, String... args) throws Exception {
+        // Create logger with provided streams
+        Log.Logger logger = new Log.Logger(false);
+        logger.setPrintWriter(out, err);
+        Log.setLogger(logger);
+
+        int status = run(args);
+        Log.flush();
+        return status;
+    }
 
     @SuppressWarnings("deprecation")
-    public static void main(String... args) throws Exception {
+    public static int run(String... args) throws Exception {
         BundleType bundleType = BundleType.NONE;
 
         if (args.length == 0 || args.length == 1 && args[0].equals("-help")) {
-            System.out.println(help);
+            Log.info(help);
         } else if (args.length == 1 && args[0].equals("-version")) {
-            System.out.println(version);
+            Log.info(version);
         } else {
             PackagerLib packager = new PackagerLib();
             CreateJarParams createJarParams = new CreateJarParams();
@@ -215,12 +235,12 @@
                         } else if (arg.equalsIgnoreCase("-manifestAttrs")) {
                             createJarParams.setManifestAttrs(createAttrMap(nextArg(args, i++)));
                         } else if (arg.equalsIgnoreCase("-noembedlauncher")) {
-                            System.err.println("-noembedlauncher is deprecated");
+                            Log.error("-noembedlauncher is deprecated");
                         } else if (arg.equalsIgnoreCase("-nocss2bin")) {
                             createJarParams.setCss2bin(false);
                         } else if (arg.equalsIgnoreCase("-runtimeVersion")) {
                             createJarParams.setFxVersion(nextArg(args, i++));
-                            System.err.println("-runtimeVersion is deprecated");
+                            Log.error("-runtimeVersion is deprecated");
                         } else if (arg.equalsIgnoreCase("-verbose") || arg.equalsIgnoreCase("-v")) {
                             createJarParams.setVerbose(true);
                             verbose = true;
@@ -316,7 +336,7 @@
                         } else if (arg.equalsIgnoreCase("-embedJNLP")) {
                             deployParams.setEmbedJNLP(true);
                         } else if (arg.equalsIgnoreCase("-embedCertificates")) {
-                            System.err.println("-embedCertificates is deprecated");
+                            Log.error("-embedCertificates is deprecated");
                         } else if (arg.equalsIgnoreCase("-allpermissions")) {
                             deployParams.setAllPermissions(true);
                         } else if (arg.equalsIgnoreCase("-updatemode")) {
@@ -443,7 +463,7 @@
                     addResources(signJarParams, srcdir, srcfiles);
                     signJar = true;
                 } else if (args[0].equalsIgnoreCase("-makeall")) {
-                    System.err.println("-makeall is deprecated");
+                    Log.error("-makeall is deprecated");
                     for (int i = 1; i < args.length; i++) {
                         String arg = args[i];
                         if (arg.equalsIgnoreCase("-appclass")) {
@@ -468,17 +488,18 @@
                 } else if (args[0].equalsIgnoreCase("-help")) {
                     showBundlerHelp(args[1], args.length > 2 && "-verbose".equals(args[2]));
                 } else {
-                    System.err.println(MessageFormat.format(
+                    Log.error(MessageFormat.format(
                                         bundle.getString("ERR_UnknownCommand"),
                                         args[0]));
-                    System.exit(-1);
+                    return -1;
                 }
 
-                //set default logger
+                // Enable verbose if needed
                 if (verbose) {
-                    com.oracle.tools.packager.Log.setLogger(new com.oracle.tools.packager.Log.Logger(true));
-                } else {
-                    com.oracle.tools.packager.Log.setLogger(new com.oracle.tools.packager.Log.Logger(false));
+                    Log.Logger logger = Log.getLogger();
+                    if (logger != null) {
+                        logger.setVerbose(true);
+                    }
                 }
 
                 if (css2Bin) {
@@ -528,14 +549,16 @@
                 if (verbose) {
                     throw e;
                 } else {
-                    System.err.println(e.getMessage());
+                    Log.error(e.getMessage());
                     if (e.getCause() != null && e.getCause() != e) {
-                        System.err.println(e.getCause().getMessage());
+                        Log.error(e.getCause().getMessage());
                     }
-                    System.exit(-1);
+                    return -1;
                 }
             }
         }
+
+        return 0;
     }
 
     private static final String MODULE = "--" + StandardBundlerParam.MODULE.getID();
@@ -553,7 +576,7 @@
         //TODO I18N
         if ("bundlers".equals(bundlerName)) {
             // enumerate bundlers
-            System.out.println("Known Bundlers -- \n");
+            Log.info("Known Bundlers -- \n");
             for (Bundler bundler : Bundlers.createBundlersInstance().getBundlers()) {
                 try {
                     bundler.validate(new HashMap<>());
@@ -567,7 +590,7 @@
                 }
 
                 if (verbose) {
-                    System.out.printf(
+                    Log.infof(
                             "%s - %s - %s\n\t%s\n",
                             bundler.getID(),
                             bundler.getName(),
@@ -575,7 +598,7 @@
                             bundler.getDescription()
                     );
                 } else {
-                    System.out.printf(
+                    Log.infof(
                             "%s - %s - %s\n",
                             bundler.getID(),
                             bundler.getName(),
@@ -587,11 +610,11 @@
             // enumerate parameters for a bundler
             for (Bundler bundler : Bundlers.createBundlersInstance().getBundlers()) {
                 if (bundler.getID().equals(bundlerName)) {
-                    System.out.printf("Bundler Parameters for %s (%s) --\n", bundler.getName(), bundler.getID());
+                    Log.infof("Bundler Parameters for %s (%s) --\n", bundler.getName(), bundler.getID());
                     for (BundlerParamInfo bpi : bundler.getBundleParameters()) {
                         if (bpi.getStringConverter() == null) continue;
                         if (verbose) {
-                            System.out.printf(
+                            Log.infof(
                                     "%s - %s - %s\n\t%s\n",
                                     bpi.getID(),
                                     bpi.getName(),
@@ -599,7 +622,7 @@
                                     bpi.getDescription()
                             );
                         } else {
-                            System.out.printf(
+                            Log.infof(
                                     "%s - %s - %s\n",
                                     bpi.getID(),
                                     bpi.getName(),
@@ -610,7 +633,7 @@
                     return;
                 }
             }
-            System.out.printf("Sorry, no bundler matching the id %s was found.\n", bundlerName);
+            Log.infof("Sorry, no bundler matching the id %s was found.\n", bundlerName);
         }
     }
 }
--- a/modules/jdk.packager/src/main/java/com/sun/javafx/tools/packager/PackagerLib.java	Thu Dec 07 10:50:25 2017 -0800
+++ b/modules/jdk.packager/src/main/java/com/sun/javafx/tools/packager/PackagerLib.java	Thu Dec 07 13:13:58 2017 -0800
@@ -465,7 +465,7 @@
             String name = "bsigned_" + jar.getName();
             File signedJar = new File(parent, name);
 
-            System.out.println("Signing (BLOB) " + jar.getPath());
+            Log.info("Signing (BLOB) " + jar.getPath());
 
             signAsBLOB(jar, signedJar, signature);
 
@@ -481,7 +481,7 @@
             destJar.getParentFile().mkdirs();
             signedJar.renameTo(destJar);
             if (verbose) {
-                System.out.println("Signed as " + destJar.getPath());
+                Log.info("Signed as " + destJar.getPath());
             }
         }
     }
@@ -543,8 +543,8 @@
                 classpath += File.pathSeparator + makeAllParams.classpath;
             }
             if (makeAllParams.verbose) {
-                System.out.println("Executing javac:");
-                System.out.printf("%s %s %s %s %s %s%n",
+                Log.info("Executing javac:");
+                Log.infof("%s %s %s %s %s %s%n",
                         javac.getAbsolutePath(),
                         "-d", compiledDirName,
                         "-cp", classpath,
@@ -609,7 +609,7 @@
             try {
                 String line;
                 while ((line = in.readLine()) != null) {
-                    System.out.println(line);
+                    Log.info(line);
                 }
             } catch (IOException ioe) {
                 com.oracle.tools.packager.Log.verbose(ioe);
@@ -622,7 +622,7 @@
             try {
                 String line;
                 while ((line = err.readLine()) != null) {
-                    System.err.println(line);
+                    Log.error(line);
                 }
             } catch (IOException ioe) {
                 Log.verbose(ioe);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/jdk.packager/src/main/java/jdk/packager/internal/JavaPackagerToolProvider.java	Thu Dec 07 13:13:58 2017 -0800
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package jdk.packager.internal;
+
+import java.io.PrintWriter;
+import java.util.spi.ToolProvider;
+
+public class JavaPackagerToolProvider implements ToolProvider {
+
+    public String name() {
+        return "javapackager";
+    }
+
+    public synchronized int run(PrintWriter out, PrintWriter err, String... args) {
+        try {
+            com.sun.javafx.tools.packager.Main.run(out, err, args);
+        } catch (Exception ignored) {
+            return 1;
+        }
+        return 0;
+    }
+}
--- a/modules/jdk.packager/src/main/java/module-info.java	Thu Dec 07 10:50:25 2017 -0800
+++ b/modules/jdk.packager/src/main/java/module-info.java	Thu Dec 07 13:13:58 2017 -0800
@@ -26,6 +26,13 @@
 /**
  * Defines the Java packager tool, javapackager.
  *
+ * <p>The javapackager is a tool for generating bundles for self-contained applications.
+ * It can be located under the name {@code "javapackager"} using the {@link ToolProvider}, for example:
+ * <pre>{@code
+ * ToolProvider javaPackager = ToolProvider.findFirst("javapackager").orElseThrow(...);
+ * javaPackager.run(...);
+ * }</pre>
+ *
  * @moduleGraph
  * @since 9
  */
@@ -59,4 +66,7 @@
         com.oracle.tools.packager.windows.WinAppBundler,
         com.oracle.tools.packager.windows.WinExeBundler,
         com.oracle.tools.packager.windows.WinMsiBundler;
+
+    provides java.util.spi.ToolProvider
+        with jdk.packager.internal.JavaPackagerToolProvider;
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/jdk.packager/src/test/java/hello/HelloToolProvider.java	Thu Dec 07 13:13:58 2017 -0800
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package hello;
+
+import java.io.FileNotFoundException;
+import java.util.spi.ToolProvider;
+
+public class HelloToolProvider {
+
+    public static void main(String... args) throws FileNotFoundException {
+        ToolProvider javaPackager = ToolProvider.findFirst("javapackager").get();
+        if (javaPackager == null) {
+            System.err.println("Cannot find tool provider for javapackager");
+        } else {
+            if (args.length == 0) {
+                System.out.println("Running packager with arg[0]: -version");
+                javaPackager.run(System.out, System.err, "-version");
+            } else {
+                int i = 0;
+                for (String arg : args) {
+                    System.out.println("Running packager with arg[" + i++ + "]: " + arg);
+                }
+                javaPackager.run(System.out, System.err, args);
+            }
+        }
+    }
+}
+