package org.eclipse.core.internal.registry.osgi;
import java.io.File;
import java.util.Hashtable;
import org.eclipse.core.internal.adapter.AdapterManagerListener;
import org.eclipse.core.internal.registry.*;
import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.spi.RegistryStrategy;
import org.eclipse.osgi.service.datalocation.Location;
import org.osgi.framework.*;
public class Activator implements BundleActivator {
private static BundleContext bundleContext;
private static final String STORAGE_DIR = "org.eclipse.core.runtime";
private Object masterRegistryKey = new Object();
private Object userRegistryKey = new Object();
private IExtensionRegistry defaultRegistry = null;
private ServiceRegistration<?> registryRegistration;
private ServiceRegistration<?> commandRegistration;
private RegistryProviderOSGI defaultProvider;
private AdapterManagerListener adapterManagerListener = null;
@Override
public void start(BundleContext context) throws Exception {
bundleContext = context;
RegistryProperties.setContext(bundleContext);
processCommandLine();
startRegistry();
adapterManagerListener = new AdapterManagerListener();
}
@Override
public void stop(BundleContext context) throws Exception {
if (adapterManagerListener != null)
adapterManagerListener.stop();
stopRegistry();
RegistryProperties.setContext(null);
bundleContext = null;
}
public static BundleContext getContext() {
return bundleContext;
}
private void processCommandLine() {
ServiceReference<?> ref = getContext().getServiceReference("org.eclipse.osgi.service.environment.EnvironmentInfo");
if (ref == null)
return;
String[] args = EquinoxUtils.getCommandLine(bundleContext, ref);
if (args == null || args.length == 0)
return;
for (String arg : args) {
if (arg.equalsIgnoreCase(IRegistryConstants.NO_REGISTRY_CACHE)) {
RegistryProperties.setProperty(IRegistryConstants.PROP_NO_REGISTRY_CACHE, "true");
} else if (arg.equalsIgnoreCase(IRegistryConstants.NO_LAZY_REGISTRY_CACHE_LOADING)) {
RegistryProperties.setProperty(IRegistryConstants.PROP_NO_LAZY_CACHE_LOADING, "true");
} else if (arg.equalsIgnoreCase(IRegistryConstants.MULTI_LANGUAGE)) {
RegistryProperties.setProperty(IRegistryConstants.PROP_MULTI_LANGUAGE, "true");
}
}
}
public void startRegistry() throws CoreException {
String property = bundleContext.getProperty(IRegistryConstants.PROP_DEFAULT_REGISTRY);
if (property != null && property.equalsIgnoreCase("false"))
return;
if ("true".equals(bundleContext.getProperty(IRegistryConstants.PROP_REGISTRY_NULL_USER_TOKEN)))
userRegistryKey = null;
File[] registryLocations;
boolean[] readOnlyLocations;
RegistryStrategy strategy = null;
Location configuration = OSGIUtils.getDefault().getConfigurationLocation();
if (configuration == null) {
RegistryProperties.setProperty(IRegistryConstants.PROP_NO_REGISTRY_CACHE, "true");
RegistryProperties.setProperty(IRegistryConstants.PROP_NO_LAZY_CACHE_LOADING, "true");
strategy = new RegistryStrategyOSGI(null, null, masterRegistryKey);
} else {
File primaryDir = new File(configuration.getURL().getPath() + '/' + STORAGE_DIR);
boolean primaryReadOnly = configuration.isReadOnly();
Location parentLocation = configuration.getParentLocation();
if (parentLocation != null) {
File secondaryDir = new File(parentLocation.getURL().getFile() + '/' + IRegistryConstants.RUNTIME_NAME);
registryLocations = new File[] {primaryDir, secondaryDir};
readOnlyLocations = new boolean[] {primaryReadOnly, true};
} else {
registryLocations = new File[] {primaryDir};
readOnlyLocations = new boolean[] {primaryReadOnly};
}
strategy = new EquinoxRegistryStrategy(registryLocations, readOnlyLocations, masterRegistryKey);
}
defaultRegistry = RegistryFactory.createRegistry(strategy, masterRegistryKey, userRegistryKey);
registryRegistration = Activator.getContext().registerService(IExtensionRegistry.class.getName(), defaultRegistry, new Hashtable<String, Object>());
defaultProvider = new RegistryProviderOSGI(defaultRegistry);
RegistryProviderFactory.setDefault(defaultProvider);
commandRegistration = EquinoxUtils.registerCommandProvider(Activator.getContext());
}
private void stopRegistry() {
if (defaultRegistry != null) {
RegistryProviderFactory.releaseDefault();
registryRegistration.unregister();
defaultRegistry.stop(masterRegistryKey);
}
if (commandRegistration != null)
commandRegistration.unregister();
}
}