1

I had an rcp application which runs for only first run, when a user attempts to re-execute the application, second instance behaves as a client which encodes and sends its arguments over the socket to the first instance which acts as a server and then exits silently. The first instance receives and decodes that message, then behaves as if it had been invoked with those arguments.

so far so good i made internal protocol specification for passing arguments between two instances.

I could not bring the first instance(RCP application) to front. It is in minimized state only,

this is in continuation to my previous question

the change i made to previous post is start method of application class

public Object start(IApplicationContext context) throws Exception {
        if (!ApplicationInstanceManager.registerInstance()) {
            return IApplication.EXIT_OK;
        }
        ApplicationInstanceManager
                .setApplicationInstanceListener(new ApplicationInstanceListener() {
                    public void newInstanceCreated() {
                        Display.getDefault().asyncExec(new Runnable() {
                            public void run() {
                                System.out.println("New instance detected...");

                                //Display.getCurrent().getActiveShell()
                                        .forceActive();// this gives null
                                                        // pointer exception
                                                        // hence commented
                            }
                        });
                    }
                });
        Display display = PlatformUI.createDisplay();
        try {
            int returnCode = PlatformUI.createAndRunWorkbench(display,
                    new ApplicationWorkbenchAdvisor());
            if (returnCode == PlatformUI.RETURN_RESTART)
                return IApplication.EXIT_RESTART;
            else
                return IApplication.EXIT_OK;
        } finally {
            display.dispose();
        }
    }

below line is stopping me to bring Application to front

Display.getCurrent().getActiveShell().forceActive();

generates null pointer exception at getActiveShell()

how can i maximize the previous instance or bring it to front

Community
  • 1
  • 1
srk
  • 4,857
  • 12
  • 65
  • 109

2 Answers2

2

I wrote an instance manager to restrict my RCP to a single instance.

Here's the code that goes in Application.java, in the start method:

    if (!ApplicationInstanceManager.registerInstance()) {
        return IApplication.EXIT_OK;
    }

    ApplicationInstanceManager
            .setApplicationInstanceListener(new ApplicationInstanceListener() {
                public void newInstanceCreated() {
                    Display.getDefault().asyncExec(new Runnable() {
                        public void run() {
                            if (DEBUG)
                                System.out.println("New instance detected...");
                            Display.getCurrent().getActiveShell().forceActive();
                        }
                    });
                }
            });

Here's the listener interface:

public interface ApplicationInstanceListener {

     public void newInstanceCreated();

}

And here's the Manager class:

public class ApplicationInstanceManager {

    private static final boolean DEBUG = true;

    private static ApplicationInstanceListener subListener;

    /** Randomly chosen, but static, high socket number */
    public static final int SINGLE_INSTANCE_NETWORK_SOCKET = 44331;

    /** Must end with newline */
    public static final String SINGLE_INSTANCE_SHARED_KEY = "$$RabidNewInstance$$\n";

    /**
     * Registers this instance of the application.
     * 
     * @return true if first instance, false if not.
     */
    public static boolean registerInstance() {
        // returnValueOnError should be true if lenient (allows app to run on
        // network error) or false if strict.
        boolean returnValueOnError = true;
        // try to open network socket
        // if success, listen to socket for new instance message, return true
        // if unable to open, connect to existing and send new instance message,
        // return false
        try {
            final ServerSocket socket = new ServerSocket(
                    SINGLE_INSTANCE_NETWORK_SOCKET, 10, InetAddress
                            .getLocalHost());
            if (DEBUG)
                System.out
                        .println("Listening for application instances on socket "
                                + SINGLE_INSTANCE_NETWORK_SOCKET);
            Thread instanceListenerThread = new InstanceListenerThread(socket);
            instanceListenerThread.start();
            // listen
        } catch (UnknownHostException e) {
            EclipseLogging.logError(RabidPlugin.getDefault(),
                    RabidPlugin.PLUGIN_ID, e);
            return returnValueOnError;
        } catch (IOException e) {
            return portTaken(returnValueOnError, e);

        }
        return true;
    }

    private static boolean portTaken(boolean returnValueOnError, IOException e) {
        if (DEBUG)
            System.out.println("Port is already taken.  "
                    + "Notifying first instance.");
        try {
            Socket clientSocket = new Socket(InetAddress.getLocalHost(),
                    SINGLE_INSTANCE_NETWORK_SOCKET);
            OutputStream out = clientSocket.getOutputStream();
            out.write(SINGLE_INSTANCE_SHARED_KEY.getBytes());
            out.close();
            clientSocket.close();
            System.out.println("Successfully notified first instance.");
            return false;
        } catch (UnknownHostException e1) {
            EclipseLogging.logError(RabidPlugin.getDefault(),
                    RabidPlugin.PLUGIN_ID, e);
            return returnValueOnError;
        } catch (IOException e1) {
            EclipseLogging
                    .logError(
                            RabidPlugin.getDefault(),
                            RabidPlugin.PLUGIN_ID,
                            "Error connecting to local port for single instance notification",
                            e);
            return returnValueOnError;
        }
    }

    public static void setApplicationInstanceListener(
            ApplicationInstanceListener listener) {
        subListener = listener;
    }

    private static void fireNewInstance() {
        if (subListener != null) {
            subListener.newInstanceCreated();
        }
    }

    public static void main(String[] args) {
        if (!ApplicationInstanceManager.registerInstance()) {
            // instance already running.
            System.out.println("Another instance of this application "
                    + "is already running.  Exiting.");
            System.exit(0);
        }
        ApplicationInstanceManager
                .setApplicationInstanceListener(new ApplicationInstanceListener() {
                    public void newInstanceCreated() {
                        System.out.println("New instance detected...");
                        // this is where your handler code goes...
                    }
                });
    }

    public static class InstanceListenerThread extends Thread {

        private ServerSocket socket;

        public InstanceListenerThread(ServerSocket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            boolean socketClosed = false;
            while (!socketClosed) {
                if (socket.isClosed()) {
                    socketClosed = true;
                } else {
                    try {
                        Socket client = socket.accept();
                        BufferedReader in = new BufferedReader(
                                new InputStreamReader(client.getInputStream()));
                        String message = in.readLine();
                        if (SINGLE_INSTANCE_SHARED_KEY.trim().equals(
                                message.trim())) {
                            if (DEBUG)
                                System.out.println("Shared key matched - "
                                        + "new application instance found");
                            fireNewInstance();
                        }
                        in.close();
                        client.close();
                    } catch (IOException e) {
                        socketClosed = true;
                    }
                }
            }
        }
    }
}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • Alternatively, you can use any available port, and write that port to a file from which others can read it. Note that forceActive() is a request the OS may not follow completely. – Andy Thomas Dec 20 '11 at 20:10
  • @raghav: It's not. Add a static boolean to the Application, – Gilbert Le Blanc Dec 22 '11 at 01:28
  • @Gilbert Display.getCurrent().getActiveShell().forceActive(); is generating nullpointer exception – srk Dec 26 '11 at 19:01
2

After your IApplication start up, you can also check and lock the OSGi instance location using org.eclipse.osgi.service.datalocation.Location.isSet() and org.eclipse.osgi.service.datalocation.Location.lock()

The location is usually retrieved from your Activator using code like:

    public Location getInstanceLocation() {
    if (locationTracker == null) {
        Filter filter = null;
        try {
            filter = context.createFilter(Location.INSTANCE_FILTER);
        } catch (InvalidSyntaxException e) {
            // ignore this. It should never happen as we have tested the
            // above format.
        }
        locationTracker = new ServiceTracker(context, filter, null);
        locationTracker.open();
    }
    return (Location) locationTracker.getService();
}
Paul Webster
  • 10,614
  • 1
  • 25
  • 32