KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > runtime > internal > adaptor > EclipseAppLauncher


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.core.runtime.internal.adaptor;
13
14 import java.lang.reflect.Method JavaDoc;
15 import java.util.Map JavaDoc;
16 import org.eclipse.core.runtime.adaptor.EclipseStarter;
17 import org.eclipse.osgi.framework.adaptor.FrameworkAdaptor;
18 import org.eclipse.osgi.framework.internal.core.FrameworkProperties;
19 import org.eclipse.osgi.framework.log.FrameworkLog;
20 import org.eclipse.osgi.framework.log.FrameworkLogEntry;
21 import org.eclipse.osgi.internal.profile.Profile;
22 import org.eclipse.osgi.service.runnable.*;
23 import org.osgi.framework.*;
24
25 public class EclipseAppLauncher implements ApplicationLauncher {
26     volatile private ParameterizedRunnable runnable = null;
27     private Object JavaDoc appContext = null;
28     private Semaphore runningLock = new Semaphore(1);
29     private Semaphore waitForAppLock = new Semaphore(0);
30     private BundleContext context;
31     private boolean relaunch = false;
32     private boolean failOnNoDefault = false;
33     private FrameworkLog log;
34     public EclipseAppLauncher(BundleContext context, boolean relaunch, boolean failOnNoDefault, FrameworkLog log) {
35         this.context = context;
36         this.relaunch = relaunch;
37         this.failOnNoDefault = failOnNoDefault;
38         this.log = log;
39         findRunnableService();
40     }
41
42     /*
43      * Used for backwards compatibility with < 3.2 runtime
44      */

45     private void findRunnableService() {
46         // look for a ParameterizedRunnable registered as a service by runtimes (3.0, 3.1)
47
String JavaDoc appClass = ParameterizedRunnable.class.getName();
48         ServiceReference[] runRefs = null;
49         try {
50             runRefs = context.getServiceReferences(ParameterizedRunnable.class.getName(), "(&(objectClass=" + appClass + ")(eclipse.application=*))"); //$NON-NLS-1$//$NON-NLS-2$
51
} catch (InvalidSyntaxException e) {
52             // ignore this. It should never happen as we have tested the above format.
53
}
54         if (runRefs != null && runRefs.length > 0) {
55             // found the service use it as the application.
56
runnable = (ParameterizedRunnable) context.getService(runRefs[0]);
57             // we will never be able to relaunch with a pre 3.2 runtime
58
relaunch = false;
59             waitForAppLock.release();
60         }
61     }
62
63     /*
64      * Starts this application launcher on the current thread. This method
65      * should be called by the main thread to ensure that applications are
66      * launched in the main thread.
67      */

68     public Object JavaDoc start(Object JavaDoc defaultContext) throws Exception JavaDoc {
69         // here we assume that launch has been called by runtime before we started
70
// TODO this may be a bad assumption but it works for now because we register the app launcher as a service and runtime synchronously calls launch on the service
71
if (failOnNoDefault && runnable == null)
72             throw new IllegalStateException JavaDoc(EclipseAdaptorMsg.ECLIPSE_STARTUP_ERROR_NO_APPLICATION);
73         Object JavaDoc result = null;
74         do {
75             try {
76                 result = runApplication(defaultContext);
77             } catch (Exception JavaDoc e) {
78                 if (!relaunch || (context.getBundle().getState() & Bundle.ACTIVE) == 0)
79                     throw e;
80                 if (log != null)
81                     log.log(new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.ERROR, 0, EclipseAdaptorMsg.ECLIPSE_STARTUP_APP_ERROR, 1, e, null));
82             }
83         } while (relaunch && (context.getBundle().getState() & Bundle.ACTIVE) != 0);
84         return result;
85     }
86
87     /*
88      * Waits for an application to be launched and the runs the application on the
89      * current thread (main).
90      */

91     private Object JavaDoc runApplication(Object JavaDoc defaultContext) throws Exception JavaDoc {
92         // wait for an application to be launched.
93
waitForAppLock.acquire();
94         // an application is ready; acquire the running lock.
95
// this must happen after we have acquired an application (by acquiring waitForAppLock above).
96
runningLock.acquire();
97         if (EclipseStarter.debug) {
98             String JavaDoc timeString = FrameworkProperties.getProperty("eclipse.startTime"); //$NON-NLS-1$
99
long time = timeString == null ? 0L : Long.parseLong(timeString);
100             System.out.println("Starting application: " + (System.currentTimeMillis() - time)); //$NON-NLS-1$
101
}
102         if (Profile.PROFILE && (Profile.STARTUP || Profile.BENCHMARK))
103             Profile.logTime("EclipseStarter.run(Object)()", "framework initialized! starting application..."); //$NON-NLS-1$ //$NON-NLS-2$
104
try {
105             // run the actual application on the current thread (main).
106
return runnable.run(appContext != null ? appContext : defaultContext);
107         } finally {
108             if (Profile.PROFILE && Profile.STARTUP)
109                 Profile.logExit("EclipseStarter.run(Object)()"); //$NON-NLS-1$
110
// free the runnable application and release the lock to allow another app to be launched.
111
runnable = null;
112             appContext = null;
113             runningLock.release();
114         }
115     }
116
117     public void launch(ParameterizedRunnable app, Object JavaDoc appContext) {
118         waitForAppLock.acquire(-1); // clear out any pending apps notifications
119
if (!runningLock.acquire(-1)) // check to see if an application is currently running
120
throw new IllegalStateException JavaDoc("An application is aready running."); //$NON-NLS-1$
121
this.runnable = app;
122         this.appContext = appContext;
123         waitForAppLock.release(); // notify the main thread to launch an application.
124
runningLock.release(); // release the running lock
125
}
126
127     public void shutdown() {
128         // this method will aquire and keep the runningLock to prevent
129
// all future application launches.
130
if (runningLock.acquire(-1))
131             return; // no application is currently running.
132
ParameterizedRunnable currentRunnable = runnable;
133         if (currentRunnable instanceof ApplicationRunnable) {
134             ((ApplicationRunnable)currentRunnable).stop();
135             runningLock.acquire(60000); // timeout after 1 minute.
136
}
137     }
138
139     /*
140      * Similar to the start method this method will restart the default method on current thread.
141      * This method assumes that the default application was launched at least once and that an ApplicationDescriptor
142      * exists that can be used to relaunch the default application.
143      */

144     public Object JavaDoc reStart(Object JavaDoc argument) throws Exception JavaDoc {
145         ServiceReference ref[] = null;
146         ref = context.getServiceReferences("org.osgi.service.application.ApplicationDescriptor", "(eclipse.application.default=true)"); //$NON-NLS-1$//$NON-NLS-2$
147
if (ref != null && ref.length > 0) {
148             Object JavaDoc defaultApp = context.getService(ref[0]);
149             Method JavaDoc launch = defaultApp.getClass().getMethod("launch", new Class JavaDoc[] {Map JavaDoc.class}); //$NON-NLS-1$
150
launch.invoke(defaultApp, new Object JavaDoc[] {null});
151             return start(argument);
152         }
153         throw new IllegalStateException JavaDoc(EclipseAdaptorMsg.ECLIPSE_STARTUP_ERROR_NO_APPLICATION);
154     }
155 }
156
Popular Tags