1 11 12 package org.eclipse.osgi.baseadaptor; 13 14 import java.io.IOException ; 15 import java.net.URL ; 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 30 public final class HookRegistry { 31 37 public static final String HOOK_CONFIGURATORS_FILE = "hookconfigurators.properties"; 39 44 public static final String HOOK_CONFIGURATORS = "hook.configurators"; 46 50 public static final String PROP_HOOK_CONFIGURATORS_INCLUDE = "osgi.hook.configurators.include"; 52 57 public static final String PROP_HOOK_CONFIGURATORS_EXCLUDE = "osgi.hook.configurators.exclude"; 59 64 public static final String PROP_HOOK_CONFIGURATORS = "osgi.hook.configurators"; 66 private static final String BUILTIN_HOOKS = "builtin.hooks"; 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 94 public FrameworkLogEntry[] initialize() { 95 ArrayList configurators = new ArrayList(5); 96 ArrayList errors = new ArrayList(0); mergeFileHookConfigurators(configurators, errors); 98 mergePropertyHookConfigurators(configurators); 99 loadConfigurators(configurators, errors); 100 readonly = true; 102 return (FrameworkLogEntry[]) errors.toArray(new FrameworkLogEntry[errors.size()]); 103 } 104 105 private void mergeFileHookConfigurators(ArrayList configuratorList, ArrayList errors) { 106 ClassLoader cl = getClass().getClassLoader(); 107 Enumeration hookConfigurators; 109 try { 110 hookConfigurators = cl != null ? cl.getResources(HookRegistry.HOOK_CONFIGURATORS_FILE) : ClassLoader.getSystemResources(HookRegistry.HOOK_CONFIGURATORS_FILE); 111 } catch (IOException e) { 112 errors.add(new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.ERROR, 0, "getResources error on " + HookRegistry.HOOK_CONFIGURATORS_FILE, 0, e, null)); return; 114 } 115 int curBuiltin = 0; 116 while (hookConfigurators.hasMoreElements()) { 117 URL url = (URL ) hookConfigurators.nextElement(); 118 try { 119 Properties configuratorProps = new Properties(); 121 configuratorProps.load(url.openStream()); 122 String hooksValue = configuratorProps.getProperty(HOOK_CONFIGURATORS); 123 if (hooksValue == null) 124 continue; 125 boolean builtin = Boolean.valueOf(configuratorProps.getProperty(BUILTIN_HOOKS)).booleanValue(); 126 String [] configurators = ManifestElement.getArrayFromList(hooksValue, ","); for (int i = 0; i < configurators.length; i++) 128 if (!configuratorList.contains(configurators[i])) { 129 if (builtin) configuratorList.add(curBuiltin++, configurators[i]); 131 else 132 configuratorList.add(configurators[i]); 133 } 134 } catch (IOException e) { 135 errors.add(new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.ERROR, 0, "error loading: " + url.toExternalForm(), 0, e, null)); } 138 } 139 } 140 141 private void mergePropertyHookConfigurators(ArrayList configuratorList) { 142 String [] configurators = ManifestElement.getArrayFromList(FrameworkProperties.getProperty(HookRegistry.PROP_HOOK_CONFIGURATORS), ","); if (configurators.length > 0) { 145 configuratorList.clear(); for (int i = 0; i < configurators.length; i++) 147 if (!configuratorList.contains(configurators[i])) 148 configuratorList.add(configurators[i]); 149 return; } 151 String [] includeConfigurators = ManifestElement.getArrayFromList(FrameworkProperties.getProperty(HookRegistry.PROP_HOOK_CONFIGURATORS_INCLUDE), ","); for (int i = 0; i < includeConfigurators.length; i++) 154 if (!configuratorList.contains(includeConfigurators[i])) 155 configuratorList.add(includeConfigurators[i]); 156 String [] excludeHooks = ManifestElement.getArrayFromList(FrameworkProperties.getProperty(HookRegistry.PROP_HOOK_CONFIGURATORS_EXCLUDE), ","); 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 hookName = (String ) iHooks.next(); 165 try { 166 Class clazz = Class.forName(hookName); 167 HookConfigurator configurator = (HookConfigurator) clazz.newInstance(); 168 configurator.addHooks(this); 169 } catch (Throwable t) { 170 errors.add(new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.ERROR, 0, "error loading hook: " + hookName, 0, t, null)); } 177 } 178 } 179 180 184 public AdaptorHook[] getAdaptorHooks() { 185 return adaptorHooks; 186 } 187 188 192 public BundleWatcher[] getWatchers() { 193 return watchers; 194 } 195 196 200 public ClassLoadingHook[] getClassLoadingHooks() { 201 return classLoadingHooks; 202 } 203 204 208 public ClassLoadingStatsHook[] getClassLoadingStatsHooks() { 209 return classLoadingStatsHooks; 210 } 211 212 216 public StorageHook[] getStorageHooks() { 217 return storageHooks; 218 } 219 220 224 public BundleFileFactoryHook[] getBundleFileFactoryHooks() { 225 return bundleFileFactoryHooks; 226 } 227 228 232 public BundleFileWrapperFactoryHook[] getBundleFileWrapperFactoryHooks() { 233 return bundleFileWrapperFactoryHooks; 234 } 235 236 240 public void addAdaptorHook(AdaptorHook adaptorHook) { 241 adaptorHooks = (AdaptorHook[]) add(adaptorHook, adaptorHooks, new AdaptorHook[adaptorHooks.length + 1]); 242 } 243 244 248 public void addWatcher(BundleWatcher watcher) { 249 watchers = (BundleWatcher[]) add(watcher, watchers, new BundleWatcher[watchers.length + 1]); 250 } 251 252 256 public void addClassLoadingHook(ClassLoadingHook classLoadingHook) { 257 classLoadingHooks = (ClassLoadingHook[]) add(classLoadingHook, classLoadingHooks, new ClassLoadingHook[classLoadingHooks.length + 1]); 258 } 259 260 264 public void addClassLoadingStatsHook(ClassLoadingStatsHook classLoadingStatsHook) { 265 classLoadingStatsHooks = (ClassLoadingStatsHook[]) add(classLoadingStatsHook, classLoadingStatsHooks, new ClassLoadingStatsHook[classLoadingStatsHooks.length + 1]); 266 } 267 268 272 public void addStorageHook(StorageHook storageHook) { 273 storageHooks = (StorageHook[]) add(storageHook, storageHooks, new StorageHook[storageHooks.length + 1]); 274 } 275 276 280 public void addBundleFileFactoryHook(BundleFileFactoryHook factory) { 281 bundleFileFactoryHooks = (BundleFileFactoryHook[]) add(factory, bundleFileFactoryHooks, new BundleFileFactoryHook[bundleFileFactoryHooks.length + 1]); 282 } 283 284 288 public void addBundleFileWrapperFactoryHook(BundleFileWrapperFactoryHook factory) { 289 bundleFileWrapperFactoryHooks = (BundleFileWrapperFactoryHook[]) add(factory, bundleFileWrapperFactoryHooks, new BundleFileWrapperFactoryHook[bundleFileWrapperFactoryHooks.length + 1]); 290 } 291 292 private Object [] add(Object newValue, Object [] oldValues, Object [] newValues) { 293 if (readonly) 294 throw new IllegalStateException ("Cannot add hooks dynamically."); if (oldValues.length > 0) 296 System.arraycopy(oldValues, 0, newValues, 0, oldValues.length); 297 newValues[oldValues.length] = newValue; 298 return newValues; 299 } 300 301 305 public BaseAdaptor getAdaptor() { 306 return adaptor; 307 } 308 } 309 | Popular Tags |