KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > equinox > internal > app > DefaultApplicationListener


1 /*******************************************************************************
2  * Copyright (c) 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.equinox.internal.app;
13
14 import org.eclipse.osgi.framework.log.FrameworkLogEntry;
15 import org.eclipse.osgi.service.runnable.ApplicationRunnable;
16 import org.eclipse.osgi.util.NLS;
17 import org.osgi.framework.*;
18 import org.osgi.service.application.ApplicationHandle;
19 import org.osgi.util.tracker.ServiceTracker;
20 import org.osgi.util.tracker.ServiceTrackerCustomizer;
21
22 /**
23  * Listens for the default ApplicationHandle which run on any thread to be destroyed. This is used to force the main
24  * thread to wait while a default application runs on another thread.
25  *
26  * A main threaded application may be launched using this class to launch the main threaded application.
27  */

28 public class DefaultApplicationListener implements ApplicationRunnable, ServiceTrackerCustomizer {
29     private boolean running = true; // indicates the default application is running
30
private EclipseAppHandle launchMainApp; // a handle to a main threaded application
31
private final ServiceTracker handleTracker; // tracks the default application handle
32
private Object JavaDoc result; // holds the result from the default application
33

34     public DefaultApplicationListener(EclipseAppHandle defaultApp) {
35         ServiceReference defaultRef = defaultApp.getServiceReference();
36         if (defaultRef == null) {
37             // service has been unregistered; application has ended already,
38
// save the result for latter
39
result = defaultApp.waitForResult(100);
40             handleTracker = null;
41             return;
42         }
43         ServiceTracker defaultAppTracker = new ServiceTracker(Activator.getContext(), defaultRef, this);
44         defaultAppTracker.open();
45         EclipseAppHandle trackedApp = (EclipseAppHandle) defaultAppTracker.getService();
46         if (trackedApp == null) {
47             // service has been unregistered; application has ended aready,
48
// save the result for latter
49
result = defaultApp.waitForResult(100);
50             handleTracker = null;
51         } else {
52             handleTracker = defaultAppTracker;
53         }
54     }
55
56     public Object JavaDoc run(Object JavaDoc context) {
57         if (handleTracker == null)
58             return getResult(); // app has ended, return the result
59
try {
60             while (waitOnRunning()) {
61                 EclipseAppHandle mainHandle = getMainHandle();
62                 if (mainHandle != null) {
63                     // while we were waiting for the default application to end someone asked for a main threaded app to launch
64
// note that we cannot hold the this lock while launching a main threaded application
65
try {
66                         mainHandle.run(null);
67                     } catch (Exception JavaDoc e) {
68                         String JavaDoc message = NLS.bind(Messages.application_error_starting, mainHandle.getInstanceId());
69                         Activator.log(new FrameworkLogEntry(Activator.PI_APP, FrameworkLogEntry.WARNING, 0, message, 0, e, null));
70                     }
71                     unsetMainHandle(mainHandle);
72                 }
73             }
74         } finally {
75             handleTracker.close();
76         }
77         return getResult();
78     }
79
80     private synchronized EclipseAppHandle getMainHandle() {
81         return launchMainApp;
82     }
83
84     private synchronized void unsetMainHandle(EclipseAppHandle mainHandle) {
85         if (launchMainApp == mainHandle)
86             launchMainApp = null;
87     }
88
89     private synchronized boolean waitOnRunning() {
90         if (!running)
91             return false;
92         try {
93             wait(100);
94         } catch (InterruptedException JavaDoc e) {
95             // do nothing
96
}
97         return running;
98     }
99
100     public void stop() {
101         if (handleTracker == null)
102             return;
103         // force the default application to quit
104
ApplicationHandle handle =(ApplicationHandle) handleTracker.getService();
105         if (handle != null) {
106             try {
107                 handle.destroy();
108             } catch (Throwable JavaDoc t) {
109                 String JavaDoc message = NLS.bind(Messages.application_error_stopping, handle.getInstanceId());
110                 Activator.log(new FrameworkLogEntry(Activator.PI_APP, FrameworkLogEntry.WARNING, 0, message, 0, t, null));
111             }
112         }
113     }
114
115     public Object JavaDoc addingService(ServiceReference reference) {
116         return Activator.getContext().getService(reference);
117     }
118
119     public void modifiedService(ServiceReference reference, Object JavaDoc service) {
120         // do nothing
121
}
122
123     synchronized public void removedService(ServiceReference reference, Object JavaDoc service) {
124         running = false;
125         // only wait for 5 seconds; this may timeout if forcing an application to quit takes too long
126
// this should never timeout if the application exited normally.
127
result = ((EclipseAppHandle) service).waitForResult(5000);
128         EclipseAppHandle mainHandle = getMainHandle();
129         if (mainHandle != null)
130             // default application has quit; now force the main threaded application to quit
131
try {
132                 mainHandle.destroy();
133             } catch (Throwable JavaDoc t) {
134                 String JavaDoc message = NLS.bind(Messages.application_error_stopping, mainHandle.getInstanceId());
135                 Activator.log(new FrameworkLogEntry(Activator.PI_APP, FrameworkLogEntry.WARNING, 0, message, 0, t, null));
136             }
137         this.notify();
138     }
139     synchronized void launch(EclipseAppHandle app) {
140         launchMainApp = app;
141         this.notify();
142     }
143
144     private synchronized Object JavaDoc getResult() {
145         return result;
146     }
147 }
148
Popular Tags