KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > registry > osgi > 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.registry.osgi;
12
13 import java.io.File JavaDoc;
14 import java.util.Hashtable JavaDoc;
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 /**
22  * The extension registry bundle. This activator will create the default OSGi registry
23  * unless told otherwise by setting the following system property to false:
24  * <code>eclipse.createRegistry=false</code>
25  *
26  * The default registry will be stopped on the bundle shutdown.
27  *
28  * @see IRegistryConstants#PROP_DEFAULT_REGISTRY
29  */

30 public class Activator implements BundleActivator {
31
32     private static BundleContext bundleContext;
33
34     /**
35      * Location of the default registry relative to the configuration area
36      */

37     private static final String JavaDoc STORAGE_DIR = "org.eclipse.core.runtime"; //$NON-NLS-1$
38

39     private Object JavaDoc masterRegistryKey = new Object JavaDoc();
40     private Object JavaDoc userRegistryKey = new Object JavaDoc();
41
42     private IExtensionRegistry defaultRegistry = null;
43     private ServiceRegistration registryRegistration;
44     private ServiceRegistration commandRegistration;
45     private RegistryProviderOSGI defaultProvider;
46
47     /**
48      * This method is called upon plug-in activation
49      */

50     public void start(BundleContext context) throws Exception JavaDoc {
51         bundleContext = context;
52         RegistryProperties.setContext(bundleContext);
53         processCommandLine();
54         startRegistry();
55     }
56
57     /**
58      * This method is called when the plug-in is stopped
59      */

60     public void stop(BundleContext context) throws Exception JavaDoc {
61         stopRegistry();
62         RegistryProperties.setContext(null);
63         bundleContext = null;
64     }
65
66     public static BundleContext getContext() {
67         return bundleContext;
68     }
69
70     /**
71      * Look for the no registry cache flag and check to see if we should NOT be lazily loading plug-in
72      * definitions from the registry cache file.
73      * NOTE: this command line processing is only performed in the presence of OSGi
74      *
75      * @param args - command line arguments
76      */

77     private void processCommandLine() {
78         // use a string here instead of the class to prevent class loading.
79
ServiceReference ref = getContext().getServiceReference("org.eclipse.osgi.service.environment.EnvironmentInfo"); //$NON-NLS-1$
80
if (ref == null)
81             return;
82         String JavaDoc[] 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"); //$NON-NLS-1$
88
else if (args[i].equalsIgnoreCase(IRegistryConstants.NO_LAZY_REGISTRY_CACHE_LOADING))
89                 RegistryProperties.setProperty(IRegistryConstants.PROP_NO_LAZY_CACHE_LOADING, "true"); //$NON-NLS-1$
90
}
91     }
92
93     public void startRegistry() throws CoreException {
94         // see if the customer suppressed the creation of default registry
95
String JavaDoc property = bundleContext.getProperty(IRegistryConstants.PROP_DEFAULT_REGISTRY);
96         if (property != null && property.equalsIgnoreCase("false")) //$NON-NLS-1$
97
return;
98
99         // check to see if we need to use null as a userToken
100
if ("true".equals(bundleContext.getProperty(IRegistryConstants.PROP_REGISTRY_NULL_USER_TOKEN))) //$NON-NLS-1$
101
userRegistryKey = null;
102
103         // Determine primary and alternative registry locations. Eclipse extension registry cache
104
// can be found in one of the two locations:
105
// a) in the local configuration area (standard location passed in by the platform) -> priority
106
// b) in the shared configuration area (typically, shared install is used)
107
File JavaDoc[] 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"); //$NON-NLS-1$
114
RegistryProperties.setProperty(IRegistryConstants.PROP_NO_LAZY_CACHE_LOADING, "true"); //$NON-NLS-1$
115
strategy = new RegistryStrategyOSGI(null, null, masterRegistryKey);
116         } else {
117             File JavaDoc primaryDir = new File JavaDoc(configuration.getURL().getPath() + '/' + STORAGE_DIR);
118             boolean primaryReadOnly = configuration.isReadOnly();
119
120             Location parentLocation = configuration.getParentLocation();
121             if (parentLocation != null) {
122                 File JavaDoc secondaryDir = new File JavaDoc(parentLocation.getURL().getFile() + '/' + IRegistryConstants.RUNTIME_NAME);
123                 registryLocations = new File JavaDoc[] {primaryDir, secondaryDir};
124                 readOnlyLocations = new boolean[] {primaryReadOnly, true}; // secondary Eclipse location is always read only
125
} else {
126                 registryLocations = new File JavaDoc[] {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 JavaDoc());
135         defaultProvider = new RegistryProviderOSGI();
136         // Set the registry provider and specify this as a default registry:
137
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