1 11 package org.eclipse.core.internal.preferences; 12 13 import java.util.Hashtable ; 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 26 public class Activator implements BundleActivator, ServiceTrackerCustomizer { 27 28 public static final String PI_PREFERENCES = "org.eclipse.equinox.preferences"; 30 34 private static final String PROP_REGISTER_PERF_SERVICE = "eclipse.service.pref"; private static final String PROP_CUSTOMIZATION = "eclipse.pluginCustomization"; 38 42 private ServiceTracker registryServiceTracker; 43 44 47 private static BundleContext bundleContext; 48 49 52 private ServiceRegistration preferencesService = null; 53 54 57 private ServiceRegistration osgiPreferencesService = null; 58 59 62 public void start(BundleContext context) throws Exception { 63 bundleContext = context; 64 PreferencesOSGiUtils.getDefault().openServices(); 66 processCommandLine(); 67 68 boolean shouldRegister = !"false".equalsIgnoreCase(context.getProperty(PROP_REGISTER_PERF_SERVICE)); if (shouldRegister) { 70 preferencesService = bundleContext.registerService(IPreferencesService.class.getName(), PreferencesService.getDefault(), new Hashtable ()); 71 osgiPreferencesService = bundleContext.registerService(org.osgi.service.prefs.PreferencesService.class.getName(), new OSGiPreferencesServiceManager(bundleContext), null); 72 } 73 registryServiceTracker = new ServiceTracker(bundleContext, "org.eclipse.core.runtime.IExtensionRegistry", this); registryServiceTracker.open(); 76 } 77 78 81 public void stop(BundleContext context) throws Exception { 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 105 public synchronized Object addingService(ServiceReference reference) { 106 Object service = bundleContext.getService(reference); 107 if (service != null) { 110 try { 111 Object helper = new PreferenceServiceRegistryHelper(PreferencesService.getDefault(), service); 112 PreferencesService.getDefault().setRegistryHelper(helper); 113 } catch (Exception e) { 114 RuntimeLog.log(new Status(IStatus.ERROR, PI_PREFERENCES, 0, PrefsMessages.noRegistry, e)); 115 } catch (NoClassDefFoundError error) { 116 return null; 123 } 124 } 125 return service; 127 } 128 129 132 public void modifiedService(ServiceReference reference, Object service) { 133 } 134 135 138 public synchronized void removedService(ServiceReference reference, Object service) { 139 PreferencesService.getDefault().setRegistryHelper(null); 140 bundleContext.ungetService(reference); 141 } 142 143 146 private void processCommandLine() { 147 String 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 [] 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) DefaultPreferences.pluginCustomizationFile = args[i + 1]; 169 break; } 171 } 172 } 173 } 174 | Popular Tags |