KickJava   Java API By Example, From Geeks To Geeks.

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


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.equinox.internal.app;
13
14 import java.security.AccessController JavaDoc;
15 import java.security.PrivilegedAction JavaDoc;
16 import org.eclipse.core.runtime.IContributor;
17 import org.eclipse.core.runtime.IExtensionRegistry;
18 import org.eclipse.core.runtime.spi.RegistryContributor;
19 import org.eclipse.osgi.framework.log.FrameworkLog;
20 import org.eclipse.osgi.framework.log.FrameworkLogEntry;
21 import org.eclipse.osgi.service.debug.DebugOptions;
22 import org.eclipse.osgi.service.environment.EnvironmentInfo;
23 import org.osgi.framework.*;
24 import org.osgi.service.packageadmin.PackageAdmin;
25 import org.osgi.util.tracker.ServiceTracker;
26 import org.osgi.util.tracker.ServiceTrackerCustomizer;
27
28 public class Activator implements BundleActivator, ServiceTrackerCustomizer {
29     public static final String JavaDoc PI_APP = "org.eclipse.equinox.app"; //$NON-NLS-1$
30
public static boolean DEBUG = false;
31     private static BundleContext context;
32     // PackageAdmin is a system service that never goes away as long
33
// as the framwork is active. No need to track it!!
34
private static PackageAdmin packageAdmin;
35     private static EclipseAppContainer container;
36     // tracks the FrameworkLog service
37
private static ServiceTracker frameworkLog;
38     // tracks the extension registry and app launcher services
39
private ServiceTracker registryTracker;
40     private IExtensionRegistry registry;
41
42     public void start(BundleContext bc) {
43         context = bc;
44         // doing simple get service here because we expect the PackageAdmin service to always be available
45
ServiceReference ref = context.getServiceReference(PackageAdmin.class.getName());
46         if (ref != null)
47             packageAdmin = (PackageAdmin) context.getService(ref);
48         getDebugOptions(context);
49         processCommandLineArgs();
50         // set the app manager context before starting the container
51
AppPersistence.start(context);
52         // we must have an extension registry started before we can start the container
53
registryTracker = new ServiceTracker(context, IExtensionRegistry.class.getName(), this);
54         registryTracker.open();
55         // start the app commands for the console
56
try {
57             AppCommands.create(context);
58         } catch (NoClassDefFoundError JavaDoc e) {
59             // catch incase CommandProvider is not available
60
}
61     }
62
63     public void stop(BundleContext bc) {
64         // stop the app commands for the console
65
try {
66             AppCommands.destroy(context);
67         } catch (NoClassDefFoundError JavaDoc e) {
68             // catch incase CommandProvider is not available
69
}
70         // close the registry tracker; this will stop the container if it was started
71
registryTracker.close();
72         registryTracker = null;
73         // unset the app manager context after the container has been stopped
74
AppPersistence.stop();
75         if (frameworkLog != null) {
76             frameworkLog.close();
77             frameworkLog = null;
78         }
79         packageAdmin = null; // we do not unget PackageAdmin here; let the framework do it for us
80
context = null;
81     }
82
83     private void getDebugOptions(BundleContext context) {
84         ServiceReference debugRef = context.getServiceReference(DebugOptions.class.getName());
85         if (debugRef == null)
86             return;
87         DebugOptions debugOptions = (DebugOptions) context.getService(debugRef);
88         DEBUG = debugOptions.getBooleanOption(PI_APP + "/debug", false); //$NON-NLS-1$
89
context.ungetService(debugRef);
90     }
91
92     private void processCommandLineArgs() {
93         ServiceReference infoRef = context.getServiceReference(EnvironmentInfo.class.getName());
94         if (infoRef == null)
95             return;
96         EnvironmentInfo envInfo = (EnvironmentInfo) context.getService(infoRef);
97         if (envInfo == null)
98             return;
99         String JavaDoc[] args = envInfo.getNonFrameworkArgs();
100         context.ungetService(infoRef);
101         CommandLineArgs.processCommandLine(args);
102     }
103
104     public Object JavaDoc addingService(ServiceReference reference) {
105         if (container != null)
106             return null; // container is already started; do nothing
107
Object JavaDoc service = context.getService(reference);
108         if (registry == null && service instanceof IExtensionRegistry) {
109             registry = (IExtensionRegistry) service;
110             // create and start the app container
111
container = new EclipseAppContainer(context, registry);
112             container.start();
113             return service;
114         }
115         // this means there is more than one registry; we don't need a second one
116
context.ungetService(reference);
117         return null;
118     }
119
120     public void modifiedService(ServiceReference reference, Object JavaDoc service) {
121         // do nothing
122
}
123
124     public void removedService(ServiceReference reference, Object JavaDoc service) {
125         // either the registry or launcher is going away
126
if (service == registry)
127             registry = null;
128         if (container == null)
129             return; // do nothing; we have not started the container yet
130
// stop the app container
131
container.stop();
132         container = null;
133     }
134
135     // helper used to protect callers from permission checks when opening service trackers
136
static void openTracker(final ServiceTracker tracker, final boolean allServices) {
137         if (System.getSecurityManager() == null)
138             tracker.open(allServices);
139         else
140             AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
141                 public Object JavaDoc run() {
142                     tracker.open(allServices);
143                     return null;
144                 }
145             });
146     }
147
148     // helper used to protect callers from permission checks when get services
149
static Object JavaDoc getService(final ServiceTracker tracker) {
150         if (System.getSecurityManager() == null)
151             return tracker.getService();
152         return AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
153             public Object JavaDoc run() {
154                 return tracker.getService();
155             }
156         });
157     }
158
159     // helper used to protect callers from permission checks when getting locations
160
static String JavaDoc getLocation(final Bundle bundle) {
161         if (System.getSecurityManager() == null)
162             return bundle.getLocation();
163         return (String JavaDoc) AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
164             public Object JavaDoc run() {
165                 return bundle.getLocation();
166             }
167         });
168     }
169
170     // helper method to get a bundle from a contributor.
171
static Bundle getBundle(IContributor contributor) {
172         if (contributor instanceof RegistryContributor) {
173             try {
174                 long id = Long.parseLong(((RegistryContributor) contributor).getActualId());
175                 if (context != null)
176                     return context.getBundle(id);
177             } catch (NumberFormatException JavaDoc e) {
178                 // try using the name of the contributor below
179
}
180         }
181         if (packageAdmin == null)
182             return null;
183         Bundle[] bundles = packageAdmin.getBundles(contributor.getName(), null);
184         if (bundles == null)
185             return null;
186         //Return the first bundle that is not installed or uninstalled
187
for (int i = 0; i < bundles.length; i++)
188             if ((bundles[i].getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0)
189                 return bundles[i];
190         return null;
191     }
192
193     static BundleContext getContext() {
194         return context;
195     }
196
197     public static EclipseAppContainer getContainer() {
198         return container;
199     }
200
201     static void log(FrameworkLogEntry entry) {
202         BundleContext bc = context;
203         if (bc == null)
204             return;
205         ServiceReference ref = null;
206         try {
207             ref = bc.getServiceReference(FrameworkLog.class.getName());
208             if (ref == null)
209                 return;
210             FrameworkLog log = (FrameworkLog) context.getService(ref);
211             if (log != null)
212                 log.log(entry);
213         } finally {
214             if (ref != null)
215                 bc.ungetService(ref);
216         }
217     }
218 }
219
Popular Tags