KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > baseadaptor > HookRegistry


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
12 package org.eclipse.osgi.baseadaptor;
13
14 import java.io.IOException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.*;
17 import org.eclipse.osgi.baseadaptor.hooks.*;
18 import org.eclipse.osgi.framework.adaptor.BundleWatcher;
19 import org.eclipse.osgi.framework.adaptor.FrameworkAdaptor;
20 import org.eclipse.osgi.framework.internal.core.FrameworkProperties;
21 import org.eclipse.osgi.framework.log.FrameworkLogEntry;
22 import org.eclipse.osgi.util.ManifestElement;
23
24 /**
25  * The hook registry is used to store all the hooks which are
26  * configured by the hook configurators.
27  * @see HookConfigurator
28  * @since 3.2
29  */

30 public final class HookRegistry {
31     /**
32      * The hook configurators properties file (&quot;hookconfigurators.properties&quot;) <p>
33      * A framework extension may supply a hook configurators properties file to specify a
34      * list of hook configurators.
35      * @see #HOOK_CONFIGURATORS
36      */

37     public static final String JavaDoc HOOK_CONFIGURATORS_FILE = "hookconfigurators.properties"; //$NON-NLS-1$
38

39     /**
40      * The hook configurators property key (&quot;hookconfigurators.properties&quot;) used in
41      * a hook configurators properties file to specify a comma separated list of fully
42      * qualified hook configurator classes.
43      */

44     public static final String JavaDoc HOOK_CONFIGURATORS = "hook.configurators"; //$NON-NLS-1$
45

46     /**
47      * A system property (&quot;osgi.hook.configurators.include&quot;) used to add additional
48      * hook configurators. This is helpful for configuring optional hook configurators.
49      */

50     public static final String JavaDoc PROP_HOOK_CONFIGURATORS_INCLUDE = "osgi.hook.configurators.include"; //$NON-NLS-1$
51

52     /**
53      * A system property (&quot;osgi.hook.configurators.exclude&quot;) used to exclude
54      * any hook configurators. This is helpful for disabling hook
55      * configurators that is specified in hook configurator properties files.
56      */

57     public static final String JavaDoc PROP_HOOK_CONFIGURATORS_EXCLUDE = "osgi.hook.configurators.exclude"; //$NON-NLS-1$
58

59     /**
60      * A system property (&quot;osgi.hook.configurators&quot;) used to specify the list
61      * of hook configurators. If this property is set then the list of configurators
62      * specified will be the only configurators used.
63      */

64     public static final String JavaDoc PROP_HOOK_CONFIGURATORS = "osgi.hook.configurators"; //$NON-NLS-1$
65

66     private static final String JavaDoc BUILTIN_HOOKS = "builtin.hooks"; //$NON-NLS-1$
67

68     private BaseAdaptor adaptor;
69     private boolean readonly = false;
70     private AdaptorHook[] adaptorHooks = new AdaptorHook[0];
71     private BundleWatcher[] watchers = new BundleWatcher[0];
72     private ClassLoadingHook[] classLoadingHooks = new ClassLoadingHook[0];
73     private ClassLoadingStatsHook[] classLoadingStatsHooks = new ClassLoadingStatsHook[0];
74     private StorageHook[] storageHooks = new StorageHook[0];
75     private BundleFileFactoryHook[] bundleFileFactoryHooks = new BundleFileFactoryHook[0];
76     private BundleFileWrapperFactoryHook[] bundleFileWrapperFactoryHooks = new BundleFileWrapperFactoryHook[0];
77
78     public HookRegistry(BaseAdaptor adaptor) {
79         this.adaptor = adaptor;
80     }
81
82     /**
83      * Initializes the hook configurators. The following steps are used to initialize the hook configurators. <p>
84      * 1. Get a list of hook configurators from all hook configurators properties files on the classpath,
85      * add this list to the overall list of hook configurators, remove duplicates. <p>
86      * 2. Get a list of hook configurators from the (&quot;osgi.hook.configurators.include&quot;) system property
87      * and add this list to the overall list of hook configurators, remove duplicates. <p>
88      * 3. Get a list of hook configurators from the (&quot;osgi.hook.configurators.exclude&quot;) system property
89      * and remove this list from the overall list of hook configurators. <p>
90      * 4. Load each hook configurator class, create a new instance, then call the {@link HookConfigurator#addHooks(HookRegistry)} method <p>
91      * 5. Set this HookRegistry object to read only to prevent any other hooks from being added. <p>
92      * @return an array of error log entries that occurred while initializing the hooks
93      */

94     public FrameworkLogEntry[] initialize() {
95         ArrayList configurators = new ArrayList(5);
96         ArrayList errors = new ArrayList(0); // optimistic that no errors will occur
97
mergeFileHookConfigurators(configurators, errors);
98         mergePropertyHookConfigurators(configurators);
99         loadConfigurators(configurators, errors);
100         // set to read-only
101
readonly = true;
102         return (FrameworkLogEntry[]) errors.toArray(new FrameworkLogEntry[errors.size()]);
103     }
104
105     private void mergeFileHookConfigurators(ArrayList configuratorList, ArrayList errors) {
106         ClassLoader JavaDoc cl = getClass().getClassLoader();
107         // get all hook configurators files in your classloader delegation
108
Enumeration hookConfigurators;
109         try {
110             hookConfigurators = cl != null ? cl.getResources(HookRegistry.HOOK_CONFIGURATORS_FILE) : ClassLoader.getSystemResources(HookRegistry.HOOK_CONFIGURATORS_FILE);
111         } catch (IOException JavaDoc e) {
112             errors.add(new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.ERROR, 0, "getResources error on " + HookRegistry.HOOK_CONFIGURATORS_FILE, 0, e, null)); //$NON-NLS-1$
113
return;
114         }
115         int curBuiltin = 0;
116         while (hookConfigurators.hasMoreElements()) {
117             URL JavaDoc url = (URL JavaDoc) hookConfigurators.nextElement();
118             try {
119                 // check each file for a hook.configurators property
120
Properties configuratorProps = new Properties();
121                 configuratorProps.load(url.openStream());
122                 String JavaDoc hooksValue = configuratorProps.getProperty(HOOK_CONFIGURATORS);
123                 if (hooksValue == null)
124                     continue;
125                 boolean builtin = Boolean.valueOf(configuratorProps.getProperty(BUILTIN_HOOKS)).booleanValue();
126                 String JavaDoc[] configurators = ManifestElement.getArrayFromList(hooksValue, ","); //$NON-NLS-1$
127
for (int i = 0; i < configurators.length; i++)
128                     if (!configuratorList.contains(configurators[i])) {
129                         if (builtin) // make sure the built-in configurators are listed first (bug 170881)
130
configuratorList.add(curBuiltin++, configurators[i]);
131                         else
132                             configuratorList.add(configurators[i]);
133                     }
134             } catch (IOException JavaDoc e) {
135                 errors.add(new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.ERROR, 0, "error loading: " + url.toExternalForm(), 0, e, null)); //$NON-NLS-1$
136
// ignore and continue to next URL
137
}
138         }
139     }
140
141     private void mergePropertyHookConfigurators(ArrayList configuratorList) {
142         // see if there is a configurators list
143
String JavaDoc[] configurators = ManifestElement.getArrayFromList(FrameworkProperties.getProperty(HookRegistry.PROP_HOOK_CONFIGURATORS), ","); //$NON-NLS-1$
144
if (configurators.length > 0) {
145             configuratorList.clear(); // clear the list, we are only going to use the configurators from the list
146
for (int i = 0; i < configurators.length; i++)
147                 if (!configuratorList.contains(configurators[i]))
148                     configuratorList.add(configurators[i]);
149             return; // don't do anything else
150
}
151         // Make sure the configurators from the include property are in the list
152
String JavaDoc[] includeConfigurators = ManifestElement.getArrayFromList(FrameworkProperties.getProperty(HookRegistry.PROP_HOOK_CONFIGURATORS_INCLUDE), ","); //$NON-NLS-1$
153
for (int i = 0; i < includeConfigurators.length; i++)
154             if (!configuratorList.contains(includeConfigurators[i]))
155                 configuratorList.add(includeConfigurators[i]);
156         // Make sure the configurators from the exclude property are no in the list
157
String JavaDoc[] excludeHooks = ManifestElement.getArrayFromList(FrameworkProperties.getProperty(HookRegistry.PROP_HOOK_CONFIGURATORS_EXCLUDE), ","); //$NON-NLS-1$
158
for (int i = 0; i < excludeHooks.length; i++)
159             configuratorList.remove(excludeHooks[i]);
160     }
161
162     private void loadConfigurators(ArrayList configurators, ArrayList errors) {
163         for (Iterator iHooks = configurators.iterator(); iHooks.hasNext();) {
164             String JavaDoc hookName = (String JavaDoc) iHooks.next();
165             try {
166                 Class JavaDoc clazz = Class.forName(hookName);
167                 HookConfigurator configurator = (HookConfigurator) clazz.newInstance();
168                 configurator.addHooks(this);
169             } catch (Throwable JavaDoc t) {
170                 // We expect the follow exeptions may happen; but we need to catch all here
171
// ClassNotFoundException
172
// IllegalAccessException
173
// InstantiationException
174
// ClassCastException
175
errors.add(new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.ERROR, 0, "error loading hook: " + hookName, 0, t, null)); //$NON-NLS-1$
176
}
177         }
178     }
179
180     /**
181      * Returns the list of configured adaptor hooks.
182      * @return the list of configured adaptor hooks.
183      */

184     public AdaptorHook[] getAdaptorHooks() {
185         return adaptorHooks;
186     }
187
188     /**
189      * Returns the list of configured bundle watchers.
190      * @return the list of configured bundle watchers.
191      */

192     public BundleWatcher[] getWatchers() {
193         return watchers;
194     }
195
196     /**
197      * Returns the list of configured class loading hooks.
198      * @return the list of configured class loading hooks.
199      */

200     public ClassLoadingHook[] getClassLoadingHooks() {
201         return classLoadingHooks;
202     }
203
204     /**
205      * Returns the list of configured class loading stats hooks.
206      * @return the list of configured class loading stats hooks.
207      */

208     public ClassLoadingStatsHook[] getClassLoadingStatsHooks() {
209         return classLoadingStatsHooks;
210     }
211
212     /**
213      * Returns the list of configured storage hooks.
214      * @return the list of configured storage hooks.
215      */

216     public StorageHook[] getStorageHooks() {
217         return storageHooks;
218     }
219
220     /**
221      * Returns the list of configured bundle file factories.
222      * @return the list of configured bundle file factories.
223      */

224     public BundleFileFactoryHook[] getBundleFileFactoryHooks() {
225         return bundleFileFactoryHooks;
226     }
227
228     /**
229      * Returns the configured bundle file wrapper factories
230      * @return the configured bundle file wrapper factories
231      */

232     public BundleFileWrapperFactoryHook[] getBundleFileWrapperFactoryHooks() {
233         return bundleFileWrapperFactoryHooks;
234     }
235
236     /**
237      * Adds a adaptor hook to this hook registry.
238      * @param adaptorHook an adaptor hook object.
239      */

240     public void addAdaptorHook(AdaptorHook adaptorHook) {
241         adaptorHooks = (AdaptorHook[]) add(adaptorHook, adaptorHooks, new AdaptorHook[adaptorHooks.length + 1]);
242     }
243
244     /**
245      * Adds a bundle watcher to this hook registry.
246      * @param watcher a bundle watcher object.
247      */

248     public void addWatcher(BundleWatcher watcher) {
249         watchers = (BundleWatcher[]) add(watcher, watchers, new BundleWatcher[watchers.length + 1]);
250     }
251
252     /**
253      * Adds a class loading hook to this hook registry.
254      * @param classLoadingHook a class loading hook object.
255      */

256     public void addClassLoadingHook(ClassLoadingHook classLoadingHook) {
257         classLoadingHooks = (ClassLoadingHook[]) add(classLoadingHook, classLoadingHooks, new ClassLoadingHook[classLoadingHooks.length + 1]);
258     }
259
260     /**
261      * Adds a class loading stats hook to this hook registry.
262      * @param classLoadingStatsHook a class loading hook object.
263      */

264     public void addClassLoadingStatsHook(ClassLoadingStatsHook classLoadingStatsHook) {
265         classLoadingStatsHooks = (ClassLoadingStatsHook[]) add(classLoadingStatsHook, classLoadingStatsHooks, new ClassLoadingStatsHook[classLoadingStatsHooks.length + 1]);
266     }
267
268     /**
269      * Adds a storage hook to this hook registry.
270      * @param storageHook a storage hook object.
271      */

272     public void addStorageHook(StorageHook storageHook) {
273         storageHooks = (StorageHook[]) add(storageHook, storageHooks, new StorageHook[storageHooks.length + 1]);
274     }
275
276     /**
277      * Adds a bundle file factory to this hook registry.
278      * @param factory a bundle file factory object.
279      */

280     public void addBundleFileFactoryHook(BundleFileFactoryHook factory) {
281         bundleFileFactoryHooks = (BundleFileFactoryHook[]) add(factory, bundleFileFactoryHooks, new BundleFileFactoryHook[bundleFileFactoryHooks.length + 1]);
282     }
283
284     /**
285      * Adds a bundle file wrapper factory for this hook registry
286      * @param factory a bundle file wrapper factory object.
287      */

288     public void addBundleFileWrapperFactoryHook(BundleFileWrapperFactoryHook factory) {
289         bundleFileWrapperFactoryHooks = (BundleFileWrapperFactoryHook[]) add(factory, bundleFileWrapperFactoryHooks, new BundleFileWrapperFactoryHook[bundleFileWrapperFactoryHooks.length + 1]);
290     }
291
292     private Object JavaDoc[] add(Object JavaDoc newValue, Object JavaDoc[] oldValues, Object JavaDoc[] newValues) {
293         if (readonly)
294             throw new IllegalStateException JavaDoc("Cannot add hooks dynamically."); //$NON-NLS-1$
295
if (oldValues.length > 0)
296             System.arraycopy(oldValues, 0, newValues, 0, oldValues.length);
297         newValues[oldValues.length] = newValue;
298         return newValues;
299     }
300
301     /**
302      * Returns the base adaptor associated with this hook registry.
303      * @return the base adaptor associated with this hook registry.
304      */

305     public BaseAdaptor getAdaptor() {
306         return adaptor;
307     }
308 }
309
Popular Tags