KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > preferences > 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.preferences;
12
13 import java.util.Hashtable JavaDoc;
14 import org.eclipse.core.internal.runtime.RuntimeLog;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.core.runtime.preferences.IPreferencesService;
18 import org.eclipse.osgi.service.environment.EnvironmentInfo;
19 import org.osgi.framework.*;
20 import org.osgi.util.tracker.ServiceTracker;
21 import org.osgi.util.tracker.ServiceTrackerCustomizer;
22
23 /**
24  * The Preferences bundle activator.
25  */

26 public class Activator implements BundleActivator, ServiceTrackerCustomizer {
27
28     public static final String JavaDoc PI_PREFERENCES = "org.eclipse.equinox.preferences"; //$NON-NLS-1$
29

30     /**
31      * Eclipse property. Set to <code>false</code> to avoid registering JobManager
32      * as an OSGi service.
33      */

34     private static final String JavaDoc PROP_REGISTER_PERF_SERVICE = "eclipse.service.pref"; //$NON-NLS-1$
35
// the system property
36
private static final String JavaDoc PROP_CUSTOMIZATION = "eclipse.pluginCustomization"; //$NON-NLS-1$
37

38     /**
39      * Track the registry service - only register preference service if the registry is
40      * available
41      */

42     private ServiceTracker registryServiceTracker;
43
44     /**
45      * The bundle associated this plug-in
46      */

47     private static BundleContext bundleContext;
48
49     /**
50      * This plugin provides a Preferences service.
51      */

52     private ServiceRegistration preferencesService = null;
53
54     /**
55      * This plugin provides the OSGi Preferences service.
56      */

57     private ServiceRegistration osgiPreferencesService = null;
58
59     /* (non-Javadoc)
60      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
61      */

62     public void start(BundleContext context) throws Exception JavaDoc {
63         bundleContext = context;
64         // Open the services first before processing the command-line args, order is important! (Bug 150288)
65
PreferencesOSGiUtils.getDefault().openServices();
66         processCommandLine();
67         
68         boolean shouldRegister = !"false".equalsIgnoreCase(context.getProperty(PROP_REGISTER_PERF_SERVICE)); //$NON-NLS-1$
69
if (shouldRegister) {
70             preferencesService = bundleContext.registerService(IPreferencesService.class.getName(), PreferencesService.getDefault(), new Hashtable JavaDoc());
71             osgiPreferencesService = bundleContext.registerService(org.osgi.service.prefs.PreferencesService.class.getName(), new OSGiPreferencesServiceManager(bundleContext), null);
72         }
73         // use the string for the class name here in case the registry isn't around
74
registryServiceTracker = new ServiceTracker(bundleContext, "org.eclipse.core.runtime.IExtensionRegistry", this); //$NON-NLS-1$
75
registryServiceTracker.open();
76     }
77
78     /* (non-Javadoc)
79      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
80      */

81     public void stop(BundleContext context) throws Exception JavaDoc {
82         PreferencesOSGiUtils.getDefault().closeServices();
83         if (registryServiceTracker != null) {
84             registryServiceTracker.close();
85             registryServiceTracker = null;
86         }
87         if (preferencesService != null) {
88             preferencesService.unregister();
89             preferencesService = null;
90         }
91         if (osgiPreferencesService != null) {
92             osgiPreferencesService.unregister();
93             osgiPreferencesService = null;
94         }
95         bundleContext = null;
96     }
97
98     static BundleContext getContext() {
99         return bundleContext;
100     }
101
102     /* (non-Javadoc)
103      * @see org.osgi.util.tracker.ServiceTrackerCustomizer#addingService(org.osgi.framework.ServiceReference)
104      */

105     public synchronized Object JavaDoc addingService(ServiceReference reference) {
106         Object JavaDoc service = bundleContext.getService(reference);
107         // this check is important as it avoids early loading of PreferenceServiceRegistryHelper and allows
108
// this bundle to operate with out necessarily resolving against the registry
109
if (service != null) {
110             try {
111                 Object JavaDoc helper = new PreferenceServiceRegistryHelper(PreferencesService.getDefault(), service);
112                 PreferencesService.getDefault().setRegistryHelper(helper);
113             } catch (Exception JavaDoc e) {
114                 RuntimeLog.log(new Status(IStatus.ERROR, PI_PREFERENCES, 0, PrefsMessages.noRegistry, e));
115             } catch (NoClassDefFoundError JavaDoc error) {
116                 // Normally this catch would not be needed since we should never see the
117
// IExtensionRegistry service without resolving against registry.
118
// However, the check is very lenient with split packages and this can happen when
119
// the preferences bundle is already resolved at the time the registry bundle is installed.
120
// For this case we ignore the error. When refreshed the bundle will be rewired correctly.
121
// null is returned because we don't want to track this particular service reference.
122
return null;
123             }
124         }
125         //return the registry service so we track it
126
return service;
127     }
128
129     /* (non-Javadoc)
130      * @see org.osgi.util.tracker.ServiceTrackerCustomizer#modifiedService(org.osgi.framework.ServiceReference, java.lang.Object)
131      */

132     public void modifiedService(ServiceReference reference, Object JavaDoc service) {
133     }
134
135     /* (non-Javadoc)
136      * @see org.osgi.util.tracker.ServiceTrackerCustomizer#removedService(org.osgi.framework.ServiceReference, java.lang.Object)
137      */

138     public synchronized void removedService(ServiceReference reference, Object JavaDoc service) {
139         PreferencesService.getDefault().setRegistryHelper(null);
140         bundleContext.ungetService(reference);
141     }
142
143     /**
144      * Look for the plug-in customization file in the system properties and command-line args.
145      */

146     private void processCommandLine() {
147         // check the value of the system property first because its quicker.
148
// if it exists, then set it otherwise look at the command-line arguments.
149
String JavaDoc value = bundleContext.getProperty(PROP_CUSTOMIZATION);
150         if (value != null) {
151             DefaultPreferences.pluginCustomizationFile = value;
152             return;
153         }
154
155         ServiceTracker environmentTracker = new ServiceTracker(bundleContext, EnvironmentInfo.class.getName(), null);
156         environmentTracker.open();
157         EnvironmentInfo environmentInfo = (EnvironmentInfo) environmentTracker.getService();
158         environmentTracker.close();
159         if (environmentInfo == null)
160             return;
161         String JavaDoc[] args = environmentInfo.getNonFrameworkArgs();
162         if (args == null || args.length == 0)
163             return;
164
165         for (int i = 0; i < args.length; i++) {
166             if (args[i].equalsIgnoreCase(IPreferencesConstants.PLUGIN_CUSTOMIZATION)) {
167                 if (args.length > i + 1) // make sure the file name is actually there
168
DefaultPreferences.pluginCustomizationFile = args[i + 1];
169                 break; // only interested in this one
170
}
171         }
172     }
173 }
174
Popular Tags