1 11 package org.eclipse.core.internal.registry.osgi; 12 13 import java.io.File ; 14 import java.util.Hashtable ; 15 import org.eclipse.core.internal.registry.*; 16 import org.eclipse.core.runtime.*; 17 import org.eclipse.core.runtime.spi.RegistryStrategy; 18 import org.eclipse.osgi.service.datalocation.Location; 19 import org.osgi.framework.*; 20 21 30 public class Activator implements BundleActivator { 31 32 private static BundleContext bundleContext; 33 34 37 private static final String STORAGE_DIR = "org.eclipse.core.runtime"; 39 private Object masterRegistryKey = new Object (); 40 private Object userRegistryKey = new Object (); 41 42 private IExtensionRegistry defaultRegistry = null; 43 private ServiceRegistration registryRegistration; 44 private ServiceRegistration commandRegistration; 45 private RegistryProviderOSGI defaultProvider; 46 47 50 public void start(BundleContext context) throws Exception { 51 bundleContext = context; 52 RegistryProperties.setContext(bundleContext); 53 processCommandLine(); 54 startRegistry(); 55 } 56 57 60 public void stop(BundleContext context) throws Exception { 61 stopRegistry(); 62 RegistryProperties.setContext(null); 63 bundleContext = null; 64 } 65 66 public static BundleContext getContext() { 67 return bundleContext; 68 } 69 70 77 private void processCommandLine() { 78 ServiceReference ref = getContext().getServiceReference("org.eclipse.osgi.service.environment.EnvironmentInfo"); if (ref == null) 81 return; 82 String [] args = EquinoxUtils.getCommandLine(bundleContext, ref); 83 if (args == null || args.length == 0) 84 return; 85 for (int i = 0; i < args.length; i++) { 86 if (args[i].equalsIgnoreCase(IRegistryConstants.NO_REGISTRY_CACHE)) 87 RegistryProperties.setProperty(IRegistryConstants.PROP_NO_REGISTRY_CACHE, "true"); else if (args[i].equalsIgnoreCase(IRegistryConstants.NO_LAZY_REGISTRY_CACHE_LOADING)) 89 RegistryProperties.setProperty(IRegistryConstants.PROP_NO_LAZY_CACHE_LOADING, "true"); } 91 } 92 93 public void startRegistry() throws CoreException { 94 String property = bundleContext.getProperty(IRegistryConstants.PROP_DEFAULT_REGISTRY); 96 if (property != null && property.equalsIgnoreCase("false")) return; 98 99 if ("true".equals(bundleContext.getProperty(IRegistryConstants.PROP_REGISTRY_NULL_USER_TOKEN))) userRegistryKey = null; 102 103 File [] registryLocations; 108 boolean[] readOnlyLocations; 109 110 RegistryStrategy strategy = null; 111 Location configuration = OSGIUtils.getDefault().getConfigurationLocation(); 112 if (configuration == null) { 113 RegistryProperties.setProperty(IRegistryConstants.PROP_NO_REGISTRY_CACHE, "true"); RegistryProperties.setProperty(IRegistryConstants.PROP_NO_LAZY_CACHE_LOADING, "true"); strategy = new RegistryStrategyOSGI(null, null, masterRegistryKey); 116 } else { 117 File primaryDir = new File (configuration.getURL().getPath() + '/' + STORAGE_DIR); 118 boolean primaryReadOnly = configuration.isReadOnly(); 119 120 Location parentLocation = configuration.getParentLocation(); 121 if (parentLocation != null) { 122 File secondaryDir = new File (parentLocation.getURL().getFile() + '/' + IRegistryConstants.RUNTIME_NAME); 123 registryLocations = new File [] {primaryDir, secondaryDir}; 124 readOnlyLocations = new boolean[] {primaryReadOnly, true}; } else { 126 registryLocations = new File [] {primaryDir}; 127 readOnlyLocations = new boolean[] {primaryReadOnly}; 128 } 129 strategy = new EquinoxRegistryStrategy(registryLocations, readOnlyLocations, masterRegistryKey); 130 } 131 132 defaultRegistry = RegistryFactory.createRegistry(strategy, masterRegistryKey, userRegistryKey); 133 134 registryRegistration = Activator.getContext().registerService(IExtensionRegistry.class.getName(), defaultRegistry, new Hashtable ()); 135 defaultProvider = new RegistryProviderOSGI(); 136 RegistryProviderFactory.setDefault(defaultProvider); 138 commandRegistration = EquinoxUtils.registerCommandProvider(Activator.getContext()); 139 } 140 141 private void stopRegistry() { 142 if (defaultRegistry != null) { 143 RegistryProviderFactory.releaseDefault(); 144 defaultProvider.release(); 145 registryRegistration.unregister(); 146 defaultRegistry.stop(masterRegistryKey); 147 } 148 if (commandRegistration != null) 149 commandRegistration.unregister(); 150 } 151 152 } 153 | Popular Tags |