KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > content > 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.content;
12
13 import java.util.Hashtable JavaDoc;
14 import javax.xml.parsers.SAXParserFactory JavaDoc;
15 import org.eclipse.core.runtime.IExtensionRegistry;
16 import org.eclipse.core.runtime.content.IContentTypeManager;
17 import org.eclipse.osgi.service.debug.DebugOptions;
18 import org.osgi.framework.*;
19 import org.osgi.util.tracker.ServiceTracker;
20 import org.osgi.util.tracker.ServiceTrackerCustomizer;
21
22 /**
23  * The bundle activator for the runtime content manager plug-in.
24  */

25 public class Activator implements BundleActivator, ServiceTrackerCustomizer {
26
27     private static Activator singleton;
28     private static BundleContext bundleContext;
29     private ServiceRegistration contentManagerService = null;
30     private ServiceTracker parserTracker = null;
31     private ServiceTracker debugTracker = null;
32     private ServiceTracker registryTracker = null;
33
34     /*
35      * Return this activator's singleton instance or null if it has not been started.
36      */

37     public static Activator getDefault() {
38         return singleton;
39     }
40
41     /* (non-Javadoc)
42      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
43      */

44     public void start(BundleContext context) throws Exception JavaDoc {
45         bundleContext = context;
46         singleton = this;
47         // ContentTypeManager should be started first
48
ContentTypeManager.startup();
49         contentManagerService = bundleContext.registerService(IContentTypeManager.class.getName(), ContentTypeManager.getInstance(), new Hashtable JavaDoc());
50         registryTracker = new ServiceTracker(context, IExtensionRegistry.class.getName(), this);
51         registryTracker.open();
52     }
53
54     /* (non-Javadoc)
55      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
56      */

57     public void stop(BundleContext context) throws Exception JavaDoc {
58         if (contentManagerService != null) {
59             contentManagerService.unregister();
60             contentManagerService = null;
61         }
62         if (parserTracker != null) {
63             parserTracker.close();
64             parserTracker = null;
65         }
66         if (debugTracker != null) {
67             debugTracker.close();
68             debugTracker = null;
69         }
70         if (registryTracker != null) {
71             registryTracker.close();
72             registryTracker = null;
73         }
74         ContentTypeManager.shutdown();
75         bundleContext = null;
76     }
77
78     /*
79      * Return this plug-in's bundle context.
80      */

81     static BundleContext getContext() {
82         return bundleContext;
83     }
84
85     /*
86      * Return the registered SAX parser factory or null if one
87      * does not exist.
88      */

89     public SAXParserFactory JavaDoc getFactory() {
90         if (parserTracker == null) {
91             parserTracker = new ServiceTracker(bundleContext, SAXParserFactory JavaDoc.class.getName(), null);
92             parserTracker.open();
93         }
94         SAXParserFactory JavaDoc theFactory = (SAXParserFactory JavaDoc) parserTracker.getService();
95         if (theFactory != null)
96             theFactory.setNamespaceAware(true);
97         return theFactory;
98     }
99
100     /*
101      * Return the boolean value in the debug options for the given key, or
102      * return the default value if there is none.
103      */

104     public boolean getBooleanDebugOption(String JavaDoc option, boolean defaultValue) {
105         if (debugTracker == null) {
106             debugTracker = new ServiceTracker(bundleContext, DebugOptions.class.getName(), null);
107             debugTracker.open();
108         }
109         DebugOptions options = (DebugOptions) debugTracker.getService();
110         if (options != null) {
111             String JavaDoc value = options.getOption(option);
112             if (value != null)
113                 return "true".equalsIgnoreCase(value); //$NON-NLS-1$
114
}
115         return defaultValue;
116     }
117
118     public Object JavaDoc addingService(ServiceReference reference) {
119         IExtensionRegistry registry = (IExtensionRegistry) bundleContext.getService(reference);
120         // registry is available; add the change listener
121
ContentTypeManager.addRegistryChangeListener(registry);
122         return registry;
123     }
124
125     public void modifiedService(ServiceReference reference, Object JavaDoc service) {
126         // do nothing
127
}
128
129     public void removedService(ServiceReference reference, Object JavaDoc service) {
130         // registry is unavailable; remove the change listener
131
ContentTypeManager.removeRegistryChangeListener((IExtensionRegistry) service);
132         bundleContext.ungetService(reference);
133     }
134 }
135
Popular Tags