Summary
-------
Add functionality to launch single instance apps (for javapackager).
Problem
-------
For java apps bundled with javapackager, Windows and Linux by default launch new instance of the application (MacOS does the same thing if user launches it using .../<appname>.app/Contents/MacOS/<appname> file). We need to provide to the users a way to launch single instance apps. In addition, we need to provide API to pass arguments of the new (second) instance to the already running application (first instance). The first instance needs to be reactivated after user launches second one (the latter terminates after passing arguments).
Solution
--------
Add checks for single instance in the code of javapackager launcher. In case if existing instance is not detected, continue regular launch. Otherwise, try to reactivate existing instance and pass provided arguments (if any).
Add new interface to pass arguments to the existing instance: jdk.packager.services.singleton.SingleInstanceListener. User will need to override one method to handle new arguments: "pubic void newActivation(String[] params)". To register and unregister the listener, user will need to use public methods of jdk.packager.services.singleton.SingleInstanceService.
Add jdk.packager.services.singleton.SingleInstanceImpl to encapsulate implementation details of single instance of the packager (on java side). Add additional option for Mac for setting java.awt.desktop.OpenFilesHandler, so the listener will be notified when the application is asked to open a list of files
Specification
-------------
There will be new option for javapackager: "-singleton". The option prevents from launching multiple instances of application.
New interface jdk.packager.services.singleton.SingleInstanceListener will be used to handle the single instance behaviour - how should the application handle the arguments when another instance of the application is invoked with parameters. javadoc will be added for the interface and its methods. Users will need to override one method of the SingleInstanceListener to handle new arguments: "pubic void newActivation(String[] params)".
To pass arguments of the new (second) instance to the already running application (first instance), users will have to register / unregister app using the following three public methods of jdk.packager.services.singleton.SingleInstanceService(the class doesn't have other public methods/fields)::
/**
* 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 javapackager command line.
*
* @since 10
*/
public class SingleInstanceService {
/**
* 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.
*
* @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.
*/
public static void registerSingleInstance(SingleInstanceListener slistener, boolean setFileHandler);
/**
* 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.
*
* @param slistener the listener to handle the single instance behaviour.
*/
public static void registerSingleInstance(SingleInstanceListener 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);
}
/**
* The {@code SingleInstanceListener} interface is used for implementing
* Single Instance functionality for Java Packager.
*
* @since 10
*/
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.
*
* @param params parameters for the application main
*/
public void newActivation(String... params);
}