/home/aherrick/comment JDK-8200758-branch
authorherrick
Fri, 16 Nov 2018 14:53:42 -0500
branchJDK-8200758-branch
changeset 57028 51cc1a1f91f3
parent 57027 124983f48645
child 57029 750a23664c11
/home/aherrick/comment
src/jdk.jpackager.runtime/share/classes/jdk/jpackager/runtime/singleton/SingleInstanceImpl.java
src/jdk.jpackager.runtime/share/classes/jdk/jpackager/runtime/singleton/SingleInstanceListener.java
src/jdk.jpackager.runtime/share/classes/jdk/jpackager/runtime/singleton/SingleInstanceNewActivation.java
src/jdk.jpackager.runtime/share/classes/jdk/jpackager/runtime/singleton/SingleInstanceService.java
src/jdk.jpackager.runtime/share/classes/module-info.java
src/jdk.jpackager/macosx/native/library/MacPlatform.mm
src/jdk.jpackager/share/native/library/common/WindowsPlatform.cpp
src/jdk.jpackager/share/native/library/common/main.cpp
--- a/src/jdk.jpackager.runtime/share/classes/jdk/jpackager/runtime/singleton/SingleInstanceImpl.java	Fri Nov 16 14:42:43 2018 -0500
+++ b/src/jdk.jpackager.runtime/share/classes/jdk/jpackager/runtime/singleton/SingleInstanceImpl.java	Fri Nov 16 14:53:42 2018 -0500
@@ -25,9 +25,6 @@
 
 package jdk.jpackager.runtime.singleton;
 
-import java.awt.Desktop;
-import java.awt.desktop.OpenFilesHandler;
-import java.awt.desktop.OpenFilesEvent;
 import java.net.ServerSocket;
 import java.net.InetAddress;
 import java.net.Socket;
@@ -76,16 +73,13 @@
         String os = System.getProperty("os.name").toLowerCase();
         if (os.contains("win")) {
             return System.getProperty("user.home")
-                    + "\\AppData\\LocalLow\\Sun\\Java\\JPackager\\tmp";
-        } else if (os.contains("mac") || os.contains("os x")) {
+                    + "\\AppData\\Local\\Java\\JPackager\\tmp";
+        } else if (os.contains("mac")) {
             return System.getProperty("user.home")
-                    + "/Library/Application Support/Oracle/Java/JPackager/tmp";
-        } else if (os.contains("nix") || os.contains("nux")
-                || os.contains("aix")) {
+                    + "/Library/Application Support/Java/JPackager/tmp";
+        } else {
             return System.getProperty("user.home") + "/.java/jpackager/tmp";
         }
-
-        return System.getProperty("java.io.tmpdir");
     }
 
     void addSingleInstanceListener(SingleInstanceListener sil, String id) {
@@ -379,23 +373,6 @@
         silal.forEach(sil -> sil.newActivation(args));
     }
 
-    void setOpenFileHandler() {
-        String os = System.getProperty("os.name").toLowerCase();
-        if (!os.contains("mac") && !os.contains("os x")) {
-            return;
-        }
-
-        Desktop.getDesktop().setOpenFileHandler(new OpenFilesHandler() {
-            @Override
-            public void openFiles(OpenFilesEvent e) {
-                List<String> arguments = new ArrayList<>();
-                e.getFiles().forEach(file -> arguments.add(file.toString()));
-                performNewActivation(arguments.toArray(
-                                    new String[arguments.size()]));
-            }
-        });
-    }
-
     void removeSingleInstanceListener(SingleInstanceListener sil) {
         if (sil == null) {
             return;
--- a/src/jdk.jpackager.runtime/share/classes/jdk/jpackager/runtime/singleton/SingleInstanceListener.java	Fri Nov 16 14:42:43 2018 -0500
+++ b/src/jdk.jpackager.runtime/share/classes/jdk/jpackager/runtime/singleton/SingleInstanceListener.java	Fri Nov 16 14:53:42 2018 -0500
@@ -27,19 +27,25 @@
 
 /**
  * The {@code SingleInstanceListener} interface is used for implementing
- * Single Instance functionality for Java Packager.
+ * Single Instance functionality for applications packaged by jpackager.
  *
  * @since 12
  */
 public interface SingleInstanceListener {
 
     /**
-     * This method should be implemented by the application to
-     * handle the single instance behaviour - how should the application
-     * handle the arguments when another instance of the application is
-     * invoked with params.
+     * {@code newActivation()} should be implemented by the application to
+     * handle the single instance behavior.
+     * When a single instance application is running, the launcher of a
+     * secondary instance of that application will call {@code newActivation()}
+     * in the first running instance instead of launching another instance of
+     * the application.
      *
-     * @param params parameters for the application main
+     * @param args
+     * Arguments from the instances of the application will be passed
+     * into the {@code newActivation()} method. An application developer can
+     * decide how to handle the arguments passed by implementating this method.
      */
-    public void newActivation(String... params);
+    public void newActivation(String... args);
+
 }
--- a/src/jdk.jpackager.runtime/share/classes/jdk/jpackager/runtime/singleton/SingleInstanceNewActivation.java	Fri Nov 16 14:42:43 2018 -0500
+++ b/src/jdk.jpackager.runtime/share/classes/jdk/jpackager/runtime/singleton/SingleInstanceNewActivation.java	Fri Nov 16 14:53:42 2018 -0500
@@ -33,8 +33,8 @@
 
     public static void main(String[] args) {
 
-        if (args.length < 2) {
-            // no user args specified
+        if (args.length < 1) {
+            // need to be at least pid
             return;
         }
 
@@ -42,7 +42,13 @@
         String appId = SingleInstanceService.APP_ID_PREFIX + args[0];
 
         if (SingleInstanceService.isServerRunning(appId)) {
-            String[] newArgs = Arrays.copyOfRange(args, 1, args.length);
+            String[] newArgs;
+            if (args.length > 1) {
+                newArgs = Arrays.copyOfRange(args, 1, args.length);
+            } else {
+                // no application args
+                newArgs = new String[0];
+            }
             SingleInstanceService.connectToServer(newArgs);
         }
     }
--- a/src/jdk.jpackager.runtime/share/classes/jdk/jpackager/runtime/singleton/SingleInstanceService.java	Fri Nov 16 14:42:43 2018 -0500
+++ b/src/jdk.jpackager.runtime/share/classes/jdk/jpackager/runtime/singleton/SingleInstanceService.java	Fri Nov 16 14:53:42 2018 -0500
@@ -37,8 +37,9 @@
 
 /**
  * The {@code SingleInstanceService} class provides public methods for using
- * Single Instance functionality for Java Packager. To use these methods,
- * the option named "-singleton" must be specified on jpackager command line.
+ * Single Instance functionality in applications packaged by jpackager.
+ * To use these methods, the option named "--singleton" must
+ * be specified on the jpackager command line.
  *
  * @since 12
  */
@@ -81,56 +82,42 @@
     }
 
     /**
-     * Registers {@code SingleInstanceListener} for current process.
-     * If the {@code SingleInstanceListener} object is already registered, or
-     * {@code slistener} is {@code null}, then the registration is skipped.
+     * Adds the given slistener for the current process.
+     *
+     * If the given slistener object is null or
+     * has already been added, then nothing is added.
      *
-     * @param slistener the listener to handle the single instance behaviour.
+     * @param slistener the {@code SingleInstanceListener} to add
      */
-    public static void registerSingleInstance(
+    public static void addSingleInstanceListener(
             SingleInstanceListener slistener) {
-        registerSingleInstance(slistener, false);
+        if (instance == null) {
+            synchronized(SingleInstanceService.class) {
+                if (instance == null) {
+                    instance = new SingleInstanceImpl();
+                }
+            }
+        }
+        instance.addSingleInstanceListener(slistener,
+                APP_ID_PREFIX + ProcessHandle.current().pid());
     }
 
     /**
-     * Registers {@code SingleInstanceListener} for current process.
-     * If the {@code SingleInstanceListener} object is already registered, or
-     * {@code slistener} is {@code null}, then the registration is skipped.
+     * removes the given slistener for the current process.
      *
-     * @param slistener the listener to handle the single instance behaviour.
-     * @param setFileHandler if {@code true}, the listener is notified when the
-     *         application is asked to open a list of files. If OS is not MacOS,
-     *         the parameter is ignored.
+     * If the given slistener object is null or
+     * has not already been added, nothing is removed.
+     *
+     * @param slistener the {@code SingleInstanceListener} to remove.
      */
-    public static void registerSingleInstance(SingleInstanceListener slistener,
-                                              boolean setFileHandler) {
-        String appId = APP_ID_PREFIX + ProcessHandle.current().pid();
-        registerSingleInstanceForId(slistener, appId, setFileHandler);
-    }
-
-    static void registerSingleInstanceForId(SingleInstanceListener slistener,
-            String stringId, boolean setFileHandler) {
-        // register SingleInstanceListener for given Id
-        instance = new SingleInstanceImpl();
-        instance.addSingleInstanceListener(slistener, stringId);
-        if (setFileHandler) {
-            instance.setOpenFileHandler();
+    public static void removeSingleInstanceListener(
+            SingleInstanceListener slistener) {
+        if (instance != null) {
+            instance.removeSingleInstanceListener(slistener);
         }
     }
 
     /**
-     * Unregisters {@code SingleInstanceListener} for current process.
-     * If the {@code SingleInstanceListener} object is not registered, or
-     * {@code slistener} is {@code null}, then the unregistration is skipped.
-     *
-     * @param slistener the listener for unregistering.
-     */
-    public static void unregisterSingleInstance(
-            SingleInstanceListener slistener) {
-        instance.removeSingleInstanceListener(slistener);
-    }
-
-    /**
      * Returns true if single instance server is running for the id
      */
     static boolean isServerRunning(String id) {
--- a/src/jdk.jpackager.runtime/share/classes/module-info.java	Fri Nov 16 14:42:43 2018 -0500
+++ b/src/jdk.jpackager.runtime/share/classes/module-info.java	Fri Nov 16 14:53:42 2018 -0500
@@ -33,6 +33,4 @@
 module jdk.jpackager.runtime {
     exports jdk.jpackager.runtime.singleton;
 
-    requires java.desktop;
-
 }
--- a/src/jdk.jpackager/macosx/native/library/MacPlatform.mm	Fri Nov 16 14:42:43 2018 -0500
+++ b/src/jdk.jpackager/macosx/native/library/MacPlatform.mm	Fri Nov 16 14:53:42 2018 -0500
@@ -48,7 +48,7 @@
 #endif //__OBJC__
 
 #define MAC_JPACKAGER_TMP_DIR \
-        "/Library/Application Support/Oracle/Java/JPackager/tmp"
+        "/Library/Application Support/Java/JPackager/tmp"
 
 NSString* StringToNSString(TString Value) {
     NSString* result = [NSString stringWithCString:Value.c_str()
--- a/src/jdk.jpackager/share/native/library/common/WindowsPlatform.cpp	Fri Nov 16 14:42:43 2018 -0500
+++ b/src/jdk.jpackager/share/native/library/common/WindowsPlatform.cpp	Fri Nov 16 14:53:42 2018 -0500
@@ -39,7 +39,7 @@
 #include <regex>
 
 #define WINDOWS_JPACKAGER_TMP_DIR \
-        L"\\AppData\\LocalLow\\Sun\\Java\\JPackager\\tmp"
+        L"\\AppData\\Local\\Java\\JPackager\\tmp"
 
 
 class Registry {
--- a/src/jdk.jpackager/share/native/library/common/main.cpp	Fri Nov 16 14:42:43 2018 -0500
+++ b/src/jdk.jpackager/share/native/library/common/main.cpp	Fri Nov 16 14:53:42 2018 -0500
@@ -118,9 +118,7 @@
             if (package.CheckForSingleInstance()) {
                 // reactivate the first instance if the process Id is valid
                 platform.reactivateAnotherInstance();
-                if (package.GetArgs().size() > 0 &&
-                        platform.GetSingleInstanceProcessId() != 0) {
-                    // if user specified args, pass them to the first instance
+                if (platform.GetSingleInstanceProcessId() != 0) {
                     return RunVM(SINGLE_INSTANCE_NOTIFICATION_LAUNCH);
                 }
                 return true;