1 11 12 package org.eclipse.osgi.baseadaptor; 13 14 import java.io.*; 15 import java.net.URL ; 16 import java.net.URLConnection ; 17 import java.util.Properties ; 18 import org.eclipse.core.runtime.adaptor.LocationManager; 19 import org.eclipse.osgi.baseadaptor.bundlefile.BundleFile; 20 import org.eclipse.osgi.baseadaptor.hooks.*; 21 import org.eclipse.osgi.framework.adaptor.*; 22 import org.eclipse.osgi.framework.debug.Debug; 23 import org.eclipse.osgi.framework.internal.core.*; 24 import org.eclipse.osgi.framework.internal.core.Constants; 25 import org.eclipse.osgi.framework.log.FrameworkLog; 26 import org.eclipse.osgi.framework.log.FrameworkLogEntry; 27 import org.eclipse.osgi.internal.baseadaptor.*; 28 import org.eclipse.osgi.service.resolver.PlatformAdmin; 29 import org.eclipse.osgi.service.resolver.State; 30 import org.eclipse.osgi.util.NLS; 31 import org.osgi.framework.*; 32 33 43 public class BaseAdaptor implements FrameworkAdaptor{ 44 private static final String PROP_PARENT_CLASSLOADER = "osgi.parentClassloader"; private static final String PARENT_CLASSLOADER_APP = "app"; private static final String PARENT_CLASSLOADER_EXT = "ext"; private static final String PARENT_CLASSLOADER_BOOT = "boot"; private static final String PARENT_CLASSLOADER_FWK = "fwk"; private static ClassLoader bundleClassLoaderParent; 56 static { 57 String type = FrameworkProperties.getProperty(BaseAdaptor.PROP_PARENT_CLASSLOADER, BaseAdaptor.PARENT_CLASSLOADER_BOOT); 59 if (BaseAdaptor.PARENT_CLASSLOADER_FWK.equalsIgnoreCase(type)) 60 bundleClassLoaderParent = FrameworkAdaptor.class.getClassLoader(); 61 else if (BaseAdaptor.PARENT_CLASSLOADER_APP.equalsIgnoreCase(type)) 62 bundleClassLoaderParent = ClassLoader.getSystemClassLoader(); 63 else if (BaseAdaptor.PARENT_CLASSLOADER_EXT.equalsIgnoreCase(type)) { 64 ClassLoader appCL = ClassLoader.getSystemClassLoader(); 65 if (appCL != null) 66 bundleClassLoaderParent = appCL.getParent(); 67 } 68 if (bundleClassLoaderParent == null) 70 bundleClassLoaderParent = new ParentClassLoader(); 71 } 72 73 private static class ParentClassLoader extends ClassLoader { 75 protected ParentClassLoader() { 76 super(null); 77 } 78 } 79 80 private EventPublisher eventPublisher; 81 private ServiceRegistry serviceRegistry; 82 private boolean stopping; 83 private HookRegistry hookRegistry; 84 private FrameworkLog log; 85 private BundleContext context; 86 private BaseStorage storage; 87 private BundleWatcher bundleWatcher; 88 89 93 public BaseAdaptor(String [] args) { 94 if (LocationManager.getConfigurationLocation() == null) 95 LocationManager.initializeLocations(); 96 hookRegistry = new HookRegistry(this); 97 FrameworkLogEntry[] errors = hookRegistry.initialize(); 98 if (errors.length > 0) 99 for (int i = 0; i < errors.length; i++) 100 getFrameworkLog().log(errors[i]); 101 storage = getStorage(); 103 } 105 106 110 public void initialize(EventPublisher publisher) { 111 this.eventPublisher = publisher; 112 serviceRegistry = new ServiceRegistryImpl(); 113 ((ServiceRegistryImpl) serviceRegistry).initialize(); 114 AdaptorHook[] adaptorHooks = getHookRegistry().getAdaptorHooks(); 116 for (int i = 0; i < adaptorHooks.length; i++) 117 adaptorHooks[i].initialize(this); 118 } 119 120 123 public void initializeStorage() throws IOException { 124 storage.initialize(this); 125 } 126 127 130 public void compactStorage() throws IOException { 131 storage.compact(); 132 } 133 134 138 public Properties getProperties() { 139 Properties props = new Properties (); 140 String resource = FrameworkProperties.getProperty(Constants.OSGI_PROPERTIES, Constants.DEFAULT_OSGI_PROPERTIES); 141 try { 142 InputStream in = null; 143 File file = new File(resource); 144 if (file.exists()) 145 in = new FileInputStream(file); 146 if (in == null) 147 in = getClass().getResourceAsStream(resource); 148 if (in != null) { 149 try { 150 props.load(new BufferedInputStream(in)); 151 } finally { 152 try { 153 in.close(); 154 } catch (IOException ee) { 155 } 157 } 158 } else { 159 if (Debug.DEBUG && Debug.DEBUG_GENERAL) 160 Debug.println("Skipping osgi.properties: " + resource); } 162 } catch (IOException e) { 163 if (Debug.DEBUG && Debug.DEBUG_GENERAL) 164 Debug.println("Unable to load osgi.properties: " + e.getMessage()); } 166 storage.addProperties(props); 168 AdaptorHook[] adaptorHooks = getHookRegistry().getAdaptorHooks(); 170 for (int i = 0; i < adaptorHooks.length; i++) 171 adaptorHooks[i].addProperties(props); 172 return props; 173 } 174 175 178 public BundleData[] getInstalledBundles() { 179 return storage.getInstalledBundles(); 180 } 181 182 189 public URLConnection mapLocationToURLConnection(String location) throws BundleException { 190 try { 191 URLConnection result = null; 192 AdaptorHook[] adaptorHooks = getHookRegistry().getAdaptorHooks(); 194 for (int i = 0; i < adaptorHooks.length; i++) { 195 result = adaptorHooks[i].mapLocationToURLConnection(location); 196 if (result != null) 197 return result; 198 } 199 return (new URL (location).openConnection()); 201 } catch (IOException e) { 202 throw new BundleException(NLS.bind(AdaptorMsg.ADAPTOR_URL_CREATE_EXCEPTION, location), e); 203 } 204 } 205 206 209 public BundleOperation installBundle(String location, URLConnection source) { 210 return storage.installBundle(location, source); 211 } 212 213 216 public BundleOperation updateBundle(BundleData bundledata, URLConnection source) { 217 return storage.updateBundle((BaseData) bundledata, source); 218 } 219 220 223 public BundleOperation uninstallBundle(BundleData bundledata) { 224 return storage.uninstallBundle((BaseData) bundledata); 225 } 226 227 230 public long getTotalFreeSpace() throws IOException { 231 return storage.getFreeSpace(); 232 } 233 234 237 public PermissionStorage getPermissionStorage() throws IOException { 238 return storage.getPermissionStorage(); 239 } 240 241 244 public ServiceRegistry getServiceRegistry() { 245 return serviceRegistry; 246 } 247 248 252 public void frameworkStart(BundleContext fwContext) throws BundleException { 253 this.context = fwContext; 254 stopping = false; 255 BundleResourceHandler.setContext(fwContext); 256 storage.frameworkStart(fwContext); 258 AdaptorHook[] adaptorHooks = getHookRegistry().getAdaptorHooks(); 259 for (int i = 0; i < adaptorHooks.length; i++) 260 adaptorHooks[i].frameworkStart(fwContext); 261 } 262 263 267 public void frameworkStop(BundleContext fwContext) throws BundleException { 268 AdaptorHook[] adaptorHooks = getHookRegistry().getAdaptorHooks(); 270 for (int i = 0; i < adaptorHooks.length; i++) 271 adaptorHooks[i].frameworkStop(fwContext); 272 storage.frameworkStop(fwContext); 274 fwContext = null; 275 } 276 277 281 public void frameworkStopping(BundleContext fwContext) { 282 stopping = true; 283 storage.frameworkStopping(fwContext); 285 AdaptorHook[] adaptorHooks = getHookRegistry().getAdaptorHooks(); 287 for (int i = 0; i < adaptorHooks.length; i++) 288 adaptorHooks[i].frameworkStopping(fwContext); 289 } 290 291 294 public int getInitialBundleStartLevel() { 295 return storage.getInitialBundleStartLevel(); 296 } 297 298 301 public void setInitialBundleStartLevel(int value) { 302 storage.setInitialBundleStartLevel(value); 303 } 304 305 311 public FrameworkLog getFrameworkLog() { 312 if (log != null) 313 return log; 314 AdaptorHook[] adaptorHooks = getHookRegistry().getAdaptorHooks(); 315 for (int i = 0; i < adaptorHooks.length; i++) { 316 log = adaptorHooks[i].createFrameworkLog(); 317 if (log != null) 318 return log; 319 } 320 log = new FrameworkLog() { 321 public void log(FrameworkEvent frameworkEvent) { 322 log(new FrameworkLogEntry(frameworkEvent.getBundle().getSymbolicName() == null ? frameworkEvent.getBundle().getLocation() : frameworkEvent.getBundle().getSymbolicName(), FrameworkLogEntry.ERROR, 0, "FrameworkEvent.ERROR", 0, frameworkEvent.getThrowable(), null)); } 324 325 public void log(FrameworkLogEntry logEntry) { 326 System.err.print(logEntry.getEntry() + " "); System.err.println(logEntry.getMessage()); 328 if (logEntry.getThrowable() != null) 329 logEntry.getThrowable().printStackTrace(System.err); 330 } 331 332 public void setWriter(Writer newWriter, boolean append) { 333 } 335 336 public void setFile(File newFile, boolean append) throws IOException { 337 } 339 340 public File getFile() { 341 return null; 343 } 344 345 public void setConsoleLog(boolean consoleLog) { 346 } 348 349 public void close() { 350 } 352 }; 353 return log; 354 } 355 356 359 public BundleData createSystemBundleData() throws BundleException { 360 return new SystemBundleData(this); 361 } 362 363 366 public BundleWatcher getBundleWatcher() { 367 if (bundleWatcher != null) 368 return bundleWatcher; 369 final BundleWatcher[] watchers = hookRegistry.getWatchers(); 370 if (watchers.length == 0) 371 return null; 372 bundleWatcher = new BundleWatcher() { 373 public void watchBundle(Bundle bundle, int type) { 374 for (int i = 0; i < watchers.length; i++) 375 watchers[i].watchBundle(bundle, type); 376 } 377 }; 378 return bundleWatcher; 379 } 380 381 384 public PlatformAdmin getPlatformAdmin() { 385 return storage.getStateManager(); 386 } 387 388 391 public State getState() { 392 return storage.getStateManager().getSystemState(); 393 } 394 395 400 public ClassLoader getBundleClassLoaderParent() { 401 ClassLoader result = null; 403 ClassLoadingHook[] cpManagerHooks = getHookRegistry().getClassLoadingHooks(); 404 for (int i = 0; i < cpManagerHooks.length; i++) { 405 result = cpManagerHooks[i].getBundleClassLoaderParent(); 406 if (result != null) 407 return result; 408 } 409 return bundleClassLoaderParent; 411 } 412 413 417 public void handleRuntimeError(Throwable error) { 418 AdaptorHook[] adaptorHooks = getHookRegistry().getAdaptorHooks(); 419 for (int i = 0; i < adaptorHooks.length; i++) 420 adaptorHooks[i].handleRuntimeError(error); 421 } 422 423 428 public boolean matchDNChain(String pattern, String [] dnChain) { 429 AdaptorHook[] adaptorHooks = getHookRegistry().getAdaptorHooks(); 430 for (int i = 0; i < adaptorHooks.length; i++) 431 if (adaptorHooks[i].matchDNChain(pattern, dnChain)) 432 return true; 433 return false; 434 } 435 436 440 public boolean isStopping() { 441 return stopping; 442 } 443 444 448 public EventPublisher getEventPublisher() { 449 return eventPublisher; 450 } 451 452 456 public HookRegistry getHookRegistry() { 457 return hookRegistry; 458 } 459 460 464 public BundleContext getContext() { 465 return context; 466 } 467 468 490 public BundleFile createBundleFile(Object content, BaseData data) throws IOException { 491 return storage.createBundleFile(content, data); 492 } 493 494 498 public boolean isReadOnly() { 499 return storage.isReadOnly(); 500 } 501 502 507 protected BaseStorage getStorage() { 508 if (storage != null) 509 return storage; 510 StorageHook[] hooks = hookRegistry.getStorageHooks(); 513 for (int i = 0; i < hooks.length && storage == null; i++) 514 if (hooks[i] instanceof BaseStorageHook) 515 storage = ((BaseStorageHook)hooks[i]).getStorage(); 516 return storage; 517 } 518 } 519 | Popular Tags |