package org.eclipse.core.internal.preferences;
import java.util.Hashtable;
import org.eclipse.core.internal.runtime.RuntimeLog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.IPreferencesService;
import org.eclipse.osgi.service.environment.EnvironmentInfo;
import org.osgi.framework.*;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;
public class Activator implements BundleActivator, ServiceTrackerCustomizer<Object, Object> {
public static final String PI_PREFERENCES = "org.eclipse.equinox.preferences";
private static final String PROP_REGISTER_PERF_SERVICE = "eclipse.service.pref";
private static final String PROP_CUSTOMIZATION = "eclipse.pluginCustomization";
private ServiceTracker<?, ?> registryServiceTracker;
private static BundleContext bundleContext;
private ServiceRegistration<IPreferencesService> preferencesService;
private ServiceRegistration<org.osgi.service.prefs.PreferencesService> osgiPreferencesService;
@Override
public void start(BundleContext context) throws Exception {
bundleContext = context;
PreferencesOSGiUtils.getDefault().openServices();
processCommandLine();
boolean shouldRegister = !"false".equalsIgnoreCase(context.getProperty(PROP_REGISTER_PERF_SERVICE));
if (shouldRegister) {
preferencesService = bundleContext.registerService(IPreferencesService.class, PreferencesService.getDefault(), new Hashtable<String, Object>());
osgiPreferencesService = bundleContext.registerService(org.osgi.service.prefs.PreferencesService.class, new OSGiPreferencesServiceManager(bundleContext), null);
}
registryServiceTracker = new ServiceTracker<>(bundleContext, "org.eclipse.core.runtime.IExtensionRegistry", this);
registryServiceTracker.open();
}
@Override
public void stop(BundleContext context) throws Exception {
PreferencesOSGiUtils.getDefault().closeServices();
if (registryServiceTracker != null) {
registryServiceTracker.close();
registryServiceTracker = null;
}
if (preferencesService != null) {
preferencesService.unregister();
preferencesService = null;
}
if (osgiPreferencesService != null) {
osgiPreferencesService.unregister();
osgiPreferencesService = null;
}
bundleContext = null;
}
static BundleContext getContext() {
return bundleContext;
}
@Override
public synchronized Object addingService(ServiceReference<Object> reference) {
Object service = bundleContext.getService(reference);
if (service != null) {
try {
Object helper = new PreferenceServiceRegistryHelper(PreferencesService.getDefault(), service);
PreferencesService.getDefault().setRegistryHelper(helper);
} catch (Exception e) {
RuntimeLog.log(new Status(IStatus.ERROR, PI_PREFERENCES, 0, PrefsMessages.noRegistry, e));
} catch (NoClassDefFoundError error) {
return null;
}
}
return service;
}
@Override
public void modifiedService(ServiceReference<Object> reference, Object service) {
}
@Override
public synchronized void removedService(ServiceReference<Object> reference, Object service) {
PreferencesService.getDefault().setRegistryHelper(null);
bundleContext.ungetService(reference);
}
private void processCommandLine() {
String value = bundleContext.getProperty(PROP_CUSTOMIZATION);
if (value != null) {
DefaultPreferences.pluginCustomizationFile = value;
return;
}
ServiceTracker<?, EnvironmentInfo> environmentTracker = new ServiceTracker<>(bundleContext, EnvironmentInfo.class, null);
environmentTracker.open();
EnvironmentInfo environmentInfo = environmentTracker.getService();
environmentTracker.close();
if (environmentInfo == null)
return;
String[] args = environmentInfo.getNonFrameworkArgs();
if (args == null || args.length == 0)
return;
for (int i = 0; i < args.length; i++) {
if (args[i].equalsIgnoreCase(IPreferencesConstants.PLUGIN_CUSTOMIZATION)) {
if (args.length > i + 1)
DefaultPreferences.pluginCustomizationFile = args[i + 1];
break;
}
}
}
}