KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > runtime > Activator


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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 package org.eclipse.core.internal.runtime;
12
13 import java.net.URL JavaDoc;
14 import java.util.*;
15 import org.eclipse.core.internal.boot.PlatformURLBaseConnection;
16 import org.eclipse.core.internal.boot.PlatformURLHandler;
17 import org.eclipse.core.runtime.IAdapterManager;
18 import org.eclipse.osgi.framework.log.FrameworkLog;
19 import org.eclipse.osgi.service.datalocation.Location;
20 import org.eclipse.osgi.service.debug.DebugOptions;
21 import org.eclipse.osgi.service.localization.BundleLocalization;
22 import org.eclipse.osgi.service.urlconversion.URLConverter;
23 import org.osgi.framework.*;
24 import org.osgi.service.packageadmin.PackageAdmin;
25 import org.osgi.service.url.URLConstants;
26 import org.osgi.service.url.URLStreamHandlerService;
27 import org.osgi.util.tracker.ServiceTracker;
28
29 /**
30  * The Common runtime plugin class.
31  *
32  * This class can only be used if OSGi plugin is available.
33  */

34 public class Activator implements BundleActivator {
35
36     /**
37      * Table to keep track of all the URL converter services.
38      */

39     private static Map urlTrackers = new HashMap();
40     private static BundleContext bundleContext;
41     private static Activator singleton;
42     private ServiceRegistration platformURLConverterService = null;
43     private ServiceRegistration adapterManagerService = null;
44     private ServiceTracker installLocationTracker = null;
45     private ServiceTracker instanceLocationTracker = null;
46     private ServiceTracker configLocationTracker = null;
47     private ServiceTracker bundleTracker = null;
48     private ServiceTracker debugTracker = null;
49     private ServiceTracker logTracker = null;
50     private ServiceTracker localizationTracker = null;
51
52     /*
53      * Returns the singleton for this Activator. Callers should be aware that
54      * this will return null if the bundle is not active.
55      */

56     public static Activator getDefault() {
57         return singleton;
58     }
59
60     /**
61      * Print a debug message to the console.
62      * Pre-pend the message with the current date and the name of the current thread.
63      */

64     public static void message(String JavaDoc message) {
65         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
66         buffer.append(new Date(System.currentTimeMillis()));
67         buffer.append(" - ["); //$NON-NLS-1$
68
buffer.append(Thread.currentThread().getName());
69         buffer.append("] "); //$NON-NLS-1$
70
buffer.append(message);
71         System.out.println(buffer.toString());
72     }
73
74     /* (non-Javadoc)
75      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
76      */

77     public void start(BundleContext context) throws Exception JavaDoc {
78         bundleContext = context;
79         singleton = this;
80         Dictionary urlProperties = new Hashtable();
81         urlProperties.put("protocol", "platform"); //$NON-NLS-1$ //$NON-NLS-2$
82
platformURLConverterService = context.registerService(URLConverter.class.getName(), new PlatformURLConverter(), urlProperties);
83         adapterManagerService = context.registerService(IAdapterManager.class.getName(), AdapterManager.getDefault(), null);
84         installPlatformURLSupport();
85     }
86
87     /*
88      * Return the configuration location service, if available.
89      */

90     public Location getConfigurationLocation() {
91         if (configLocationTracker == null) {
92             Filter filter = null;
93             try {
94                 filter = bundleContext.createFilter(Location.CONFIGURATION_FILTER);
95             } catch (InvalidSyntaxException e) {
96                 // should not happen
97
}
98             configLocationTracker = new ServiceTracker(bundleContext, filter, null);
99             configLocationTracker.open();
100         }
101         return (Location) configLocationTracker.getService();
102     }
103
104     /*
105      * Return the debug options service, if available.
106      */

107     public DebugOptions getDebugOptions() {
108         if (debugTracker == null) {
109             debugTracker = new ServiceTracker(bundleContext, DebugOptions.class.getName(), null);
110             debugTracker.open();
111         }
112         return (DebugOptions) debugTracker.getService();
113     }
114
115     /*
116      * Return the framework log service, if available.
117      */

118     public FrameworkLog getFrameworkLog() {
119         if (logTracker == null) {
120             logTracker = new ServiceTracker(bundleContext, FrameworkLog.class.getName(), null);
121             logTracker.open();
122         }
123         return (FrameworkLog) logTracker.getService();
124     }
125
126     /*
127      * Return the instance location service, if available.
128      */

129     public Location getInstanceLocation() {
130         if (instanceLocationTracker == null) {
131             Filter filter = null;
132             try {
133                 filter = bundleContext.createFilter(Location.INSTANCE_FILTER);
134             } catch (InvalidSyntaxException e) {
135                 // ignore this. It should never happen as we have tested the above format.
136
}
137             instanceLocationTracker = new ServiceTracker(bundleContext, filter, null);
138             instanceLocationTracker.open();
139         }
140         return (Location) instanceLocationTracker.getService();
141     }
142
143     /**
144      * Return the resolved bundle with the specified symbolic name.
145      *
146      * @see PackageAdmin#getBundles(String, String)
147      */

148     public Bundle getBundle(String JavaDoc symbolicName) {
149         PackageAdmin admin = getBundleAdmin();
150         if (admin == null)
151             return null;
152         Bundle[] bundles = admin.getBundles(symbolicName, null);
153         if (bundles == null)
154             return null;
155         //Return the first bundle that is not installed or uninstalled
156
for (int i = 0; i < bundles.length; i++) {
157             if ((bundles[i].getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) {
158                 return bundles[i];
159             }
160         }
161         return null;
162     }
163
164     /*
165      * Return the package admin service, if available.
166      */

167     private PackageAdmin getBundleAdmin() {
168         if (bundleTracker == null) {
169             bundleTracker = new ServiceTracker(getContext(), PackageAdmin.class.getName(), null);
170             bundleTracker.open();
171         }
172         return (PackageAdmin) bundleTracker.getService();
173     }
174
175     /*
176      * Return an array of fragments for the given bundle host.
177      */

178     public Bundle[] getFragments(Bundle host) {
179         PackageAdmin admin = getBundleAdmin();
180         if (admin == null)
181             return new Bundle[0];
182         return admin.getFragments(host);
183     }
184
185     /*
186      * Return the install location service if available.
187      */

188     public Location getInstallLocation() {
189         if (installLocationTracker == null) {
190             Filter filter = null;
191             try {
192                 filter = bundleContext.createFilter(Location.INSTALL_FILTER);
193             } catch (InvalidSyntaxException e) {
194                 // should not happen
195
}
196             installLocationTracker = new ServiceTracker(bundleContext, filter, null);
197             installLocationTracker.open();
198         }
199         return (Location) installLocationTracker.getService();
200     }
201
202     /**
203      * Returns the bundle id of the bundle that contains the provided object, or
204      * <code>null</code> if the bundle could not be determined.
205      */

206     public String JavaDoc getBundleId(Object JavaDoc object) {
207         if (object == null)
208             return null;
209         if (bundleTracker == null) {
210             message("Bundle tracker is not set"); //$NON-NLS-1$
211
return null;
212         }
213         PackageAdmin packageAdmin = (PackageAdmin) bundleTracker.getService();
214         if (packageAdmin == null)
215             return null;
216
217         Bundle source = packageAdmin.getBundle(object.getClass());
218         if (source != null && source.getSymbolicName() != null)
219             return source.getSymbolicName();
220         return null;
221     }
222
223     public ResourceBundle getLocalization(Bundle bundle, String JavaDoc locale) {
224         if (localizationTracker == null) {
225             BundleContext context = Activator.getContext();
226             if (context == null) {
227                 message("ResourceTranslator called before plugin is started"); //$NON-NLS-1$
228
return null;
229             }
230             localizationTracker = new ServiceTracker(context, BundleLocalization.class.getName(), null);
231             localizationTracker.open();
232         }
233         BundleLocalization location = (BundleLocalization) localizationTracker.getService();
234         if (location != null)
235             return location.getLocalization(bundle, locale);
236
237         return null;
238     }
239
240     /* (non-Javadoc)
241      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
242      */

243     public void stop(BundleContext context) throws Exception JavaDoc {
244         closeURLTrackerServices();
245         if (platformURLConverterService != null) {
246             platformURLConverterService.unregister();
247             platformURLConverterService = null;
248         }
249         if (adapterManagerService != null) {
250             adapterManagerService.unregister();
251             adapterManagerService = null;
252         }
253         if (installLocationTracker != null) {
254             installLocationTracker.close();
255             installLocationTracker = null;
256         }
257         if (configLocationTracker != null) {
258             configLocationTracker.close();
259             configLocationTracker = null;
260         }
261         if (bundleTracker != null) {
262             bundleTracker.close();
263             bundleTracker = null;
264         }
265         if (debugTracker != null) {
266             debugTracker.close();
267             debugTracker = null;
268         }
269         if (logTracker != null) {
270             logTracker.close();
271             logTracker = null;
272         }
273         if (instanceLocationTracker != null) {
274             instanceLocationTracker.close();
275             instanceLocationTracker = null;
276         }
277         if (localizationTracker != null) {
278             localizationTracker.close();
279             localizationTracker = null;
280         }
281         bundleContext = null;
282         singleton = null;
283     }
284
285     /*
286      * Return this bundle's context.
287      */

288     static BundleContext getContext() {
289         return bundleContext;
290     }
291
292     /*
293      * Let go of all the services that we acquired and kept track of.
294      */

295     private static void closeURLTrackerServices() {
296         synchronized (urlTrackers) {
297             if (!urlTrackers.isEmpty()) {
298                 for (Iterator iter = urlTrackers.keySet().iterator(); iter.hasNext();) {
299                     String JavaDoc key = (String JavaDoc) iter.next();
300                     ServiceTracker tracker = (ServiceTracker) urlTrackers.get(key);
301                     tracker.close();
302                 }
303                 urlTrackers = new HashMap();
304             }
305         }
306     }
307
308     /*
309      * Return the URL Converter for the given URL. Return null if we can't
310      * find one.
311      */

312     public static URLConverter getURLConverter(URL JavaDoc url) {
313         String JavaDoc protocol = url.getProtocol();
314         synchronized (urlTrackers) {
315             ServiceTracker tracker = (ServiceTracker) urlTrackers.get(protocol);
316             if (tracker == null) {
317                 // get the right service based on the protocol
318
String JavaDoc FILTER_PREFIX = "(&(objectClass=" + URLConverter.class.getName() + ")(protocol="; //$NON-NLS-1$ //$NON-NLS-2$
319
String JavaDoc FILTER_POSTFIX = "))"; //$NON-NLS-1$
320
Filter filter = null;
321                 try {
322                     filter = getContext().createFilter(FILTER_PREFIX + protocol + FILTER_POSTFIX);
323                 } catch (InvalidSyntaxException e) {
324                     return null;
325                 }
326                 tracker = new ServiceTracker(getContext(), filter, null);
327                 tracker.open();
328                 // cache it in the registry
329
urlTrackers.put(protocol, tracker);
330             }
331             return (URLConverter) tracker.getService();
332         }
333     }
334
335     /**
336      * Register the platform URL support as a service to the URLHandler service
337      */

338     private void installPlatformURLSupport() {
339         PlatformURLPluginConnection.startup();
340         PlatformURLFragmentConnection.startup();
341         PlatformURLMetaConnection.startup();
342         PlatformURLConfigConnection.startup();
343
344         Location service = getInstallLocation();
345         if (service != null)
346             PlatformURLBaseConnection.startup(service.getURL());
347
348         Hashtable properties = new Hashtable(1);
349         properties.put(URLConstants.URL_HANDLER_PROTOCOL, new String JavaDoc[] {PlatformURLHandler.PROTOCOL});
350         getContext().registerService(URLStreamHandlerService.class.getName(), new PlatformURLHandler(), properties);
351     }
352
353 }
354
Popular Tags