1 11 12 package org.eclipse.osgi.framework.internal.core; 13 14 import java.io.IOException ; 15 import java.net.URL ; 16 import java.util.Enumeration ; 17 import org.eclipse.osgi.framework.adaptor.*; 18 import org.eclipse.osgi.framework.debug.Debug; 19 import org.eclipse.osgi.framework.log.FrameworkLogEntry; 20 import org.eclipse.osgi.service.resolver.BundleDescription; 21 import org.eclipse.osgi.util.NLS; 22 import org.osgi.framework.*; 23 24 public class BundleHost extends AbstractBundle { 25 29 private BundleLoaderProxy proxy; 30 31 32 protected BundleContextImpl context; 33 34 35 protected BundleFragment[] fragments; 36 37 public BundleHost(BundleData bundledata, Framework framework) throws BundleException { 38 super(bundledata, framework); 39 context = null; 40 fragments = null; 41 } 42 43 46 protected void load() { 47 if (Debug.DEBUG && Debug.DEBUG_GENERAL) { 48 if ((state & (INSTALLED)) == 0) { 49 Debug.println("Bundle.load called when state != INSTALLED: " + this); Debug.printStackTrace(new Exception ("Stack trace")); } 52 if (proxy != null) { 53 Debug.println("Bundle.load called when proxy != null: " + this); Debug.printStackTrace(new Exception ("Stack trace")); } 56 } 57 58 if (framework.isActive()) { 59 SecurityManager sm = System.getSecurityManager(); 60 61 if (sm != null && framework.permissionAdmin != null) { 62 domain = framework.permissionAdmin.createProtectionDomain(this); 63 } 64 65 } 66 proxy = null; 67 } 68 69 76 protected boolean reload(AbstractBundle newBundle) { 77 if (Debug.DEBUG && Debug.DEBUG_GENERAL) { 78 if ((state & (INSTALLED | RESOLVED)) == 0) { 79 Debug.println("Bundle.reload called when state != INSTALLED | RESOLVED: " + this); Debug.printStackTrace(new Exception ("Stack trace")); } 82 } 83 84 boolean exporting = false; 85 86 if (framework.isActive()) { 87 if (state == RESOLVED) { 88 BundleLoaderProxy curProxy = getLoaderProxy(); 89 exporting = curProxy.inUse(); 90 if (exporting) 91 curProxy.getBundleLoader().createClassLoader(); 93 else 94 closeBundleLoader(proxy); 95 state = INSTALLED; 96 proxy = null; 97 fragments = null; 98 } 99 100 } else { 101 102 try { 103 this.bundledata.close(); 104 } catch (IOException e) { 105 } 107 } 108 this.bundledata = newBundle.bundledata; 109 this.bundledata.setBundle(this); 110 if (framework.isActive() && System.getSecurityManager() != null && framework.permissionAdmin != null) 112 domain = framework.permissionAdmin.createProtectionDomain(this); 113 return (exporting); 114 } 115 116 120 protected void refresh() { 121 if (Debug.DEBUG && Debug.DEBUG_GENERAL) { 122 if ((state & (UNINSTALLED | INSTALLED | RESOLVED)) == 0) { 123 Debug.println("Bundle.reload called when state != UNINSTALLED | INSTALLED | RESOLVED: " + this); Debug.printStackTrace(new Exception ("Stack trace")); } 126 } 127 if (state == RESOLVED) { 128 closeBundleLoader(proxy); 129 proxy = null; 130 fragments = null; 131 state = INSTALLED; 132 } 135 manifestLocalization = null; 136 } 137 138 144 protected boolean unload() { 145 if (Debug.DEBUG && Debug.DEBUG_GENERAL) { 146 if ((state & (UNINSTALLED | INSTALLED | RESOLVED)) == 0) { 147 Debug.println("Bundle.unload called when state != UNINSTALLED | INSTALLED | RESOLVED: " + this); Debug.printStackTrace(new Exception ("Stack trace")); } 150 } 151 152 boolean exporting = false; 153 154 if (framework.isActive()) { 155 if (state == RESOLVED) { 156 BundleLoaderProxy curProxy = getLoaderProxy(); 157 exporting = curProxy.inUse(); 158 if (exporting) 159 curProxy.getBundleLoader().createClassLoader(); 161 else 162 closeBundleLoader(proxy); 163 164 state = INSTALLED; 165 proxy = null; 166 fragments = null; 167 domain = null; 168 } 169 } 170 if (!exporting) { 171 try { 172 this.bundledata.close(); 173 } catch (IOException e) { } 175 } 176 177 return (exporting); 178 } 179 180 private BundleLoader checkLoader() { 181 checkValid(); 182 183 if (!isResolved()) { 185 if (!framework.packageAdmin.resolveBundles(new Bundle[] {this})) { 186 return null; 187 } 188 } 189 if (Debug.DEBUG && Debug.DEBUG_GENERAL) { 190 if ((state & (STARTING | ACTIVE | STOPPING | RESOLVED)) == 0) { 191 Debug.println("Bundle.checkLoader() called when state != STARTING | ACTIVE | STOPPING | RESOLVED: " + this); Debug.printStackTrace(new Exception ("Stack trace")); } 194 } 195 196 BundleLoader loader = getBundleLoader(); 197 if (loader == null) { 198 if (Debug.DEBUG && Debug.DEBUG_GENERAL) { 199 Debug.println("Bundle.checkLoader() called when loader == null: " + this); Debug.printStackTrace(new Exception ("Stack trace")); } 202 return null; 203 } 204 return loader; 205 } 206 207 215 protected Class loadClass(String name, boolean checkPermission) throws ClassNotFoundException { 216 if (checkPermission) { 217 try { 218 framework.checkAdminPermission(this, AdminPermission.CLASS); 219 } catch (SecurityException e) { 220 throw new ClassNotFoundException (); 221 } 222 } 223 BundleLoader loader = checkLoader(); 224 if (loader == null) 225 throw new ClassNotFoundException (NLS.bind(Msg.BUNDLE_CNFE_NOT_RESOLVED, name, getBundleData().getLocation())); 226 try { 227 return (loader.loadClass(name)); 228 } catch (ClassNotFoundException e) { 229 if (!(e instanceof StatusException) && (bundledata.getStatus() & Constants.BUNDLE_LAZY_START) != 0 && !testStateChanging(Thread.currentThread())) 232 try { 233 framework.secureAction.start(this, START_TRANSIENT); 235 } catch (BundleException be) { 236 framework.adaptor.getFrameworkLog().log(new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.WARNING, 0, be.getMessage(), 0, be, null)); 237 } 238 throw e; 239 } 240 } 241 242 259 public URL getResource(String name) { 260 BundleLoader loader = null; 261 try { 262 framework.checkAdminPermission(this, AdminPermission.RESOURCE); 263 } catch (SecurityException ee) { 264 return null; 265 } 266 loader = checkLoader(); 267 if (loader == null) 268 return null; 269 return (loader.findResource(name)); 270 } 271 272 public Enumeration getResources(String name) throws IOException { 273 BundleLoader loader = null; 274 try { 275 framework.checkAdminPermission(this, AdminPermission.RESOURCE); 276 } catch (SecurityException ee) { 277 return null; 278 } 279 loader = checkLoader(); 280 if (loader == null) 281 return null; 282 Enumeration result = loader.getResources(name); 283 if (result != null && result.hasMoreElements()) 284 return result; 285 return null; 286 } 287 288 293 protected void startWorker(int options) throws BundleException { 294 if ((options & START_TRANSIENT) == 0) { 295 setStatus(Constants.BUNDLE_STARTED, true); 296 setStatus(Constants.BUNDLE_ACTIVATION_POLICY, (options & START_ACTIVATION_POLICY) != 0); 297 if (Debug.DEBUG && Debug.MONITOR_ACTIVATION) 298 new Exception ("A persistent start has been called on bundle: " + getBundleData()).printStackTrace(); 299 } 300 if (!framework.active || (state & ACTIVE) != 0) 301 return; 302 303 if (state == INSTALLED) { 304 if (!framework.packageAdmin.resolveBundles(new Bundle[] {this})) 305 throw new BundleException(getResolutionFailureMessage()); 306 } 307 308 if (getStartLevel() > framework.startLevelManager.getStartLevel()){ 309 if ((options & START_TRANSIENT) != 0) { 310 String msg = NLS.bind(Msg.BUNDLE_TRANSIENT_START_ERROR, this); 312 throw new BundleException(msg, new BundleStatusException(msg, StatusException.CODE_WARNING, this)); 314 } 315 return; 316 } 317 if ((options & START_ACTIVATION_POLICY) != 0 && (state & STARTING) == 0) { 318 if ((bundledata.getStatus() & Constants.BUNDLE_LAZY_START) != 0) { 320 state = STARTING; 322 framework.publishBundleEvent(BundleEvent.LAZY_ACTIVATION, this); 323 return; 324 } 325 } 326 327 if (Debug.DEBUG && Debug.DEBUG_GENERAL) { 328 Debug.println("Bundle: Active sl = " + framework.startLevelManager.getStartLevel() + "; Bundle " + getBundleId() + " sl = " + getStartLevel()); } 330 331 state = STARTING; 332 framework.publishBundleEvent(BundleEvent.STARTING, this); 333 context = getContext(); 334 long start = 0; 336 if (Debug.DEBUG) { 337 BundleWatcher bundleStats = framework.adaptor.getBundleWatcher(); 338 if (bundleStats != null) 339 bundleStats.watchBundle(this, BundleWatcher.START_ACTIVATION); 340 if (Debug.DEBUG_BUNDLE_TIME) { 341 start = System.currentTimeMillis(); 342 System.out.println("Starting " + getSymbolicName()); } 344 } 345 try { 346 context.start(); 347 348 if (framework.active) { 349 state = ACTIVE; 350 351 if (Debug.DEBUG && Debug.DEBUG_GENERAL) { 352 Debug.println("->started " + this); } 354 355 framework.publishBundleEvent(BundleEvent.STARTED, this); 356 } 357 358 } catch (BundleException e) { 359 state = STOPPING; 361 framework.publishBundleEvent(BundleEvent.STOPPING, this); 362 363 context.close(); 364 context = null; 365 366 state = RESOLVED; 367 framework.publishBundleEvent(BundleEvent.STOPPED, this); 370 throw e; 371 } finally { 372 if (Debug.DEBUG) { 373 BundleWatcher bundleStats = framework.adaptor.getBundleWatcher(); 374 if (bundleStats != null) 375 bundleStats.watchBundle(this, BundleWatcher.END_ACTIVATION); 376 if (Debug.DEBUG_BUNDLE_TIME) 377 System.out.println("End starting " + getSymbolicName() + " " + (System.currentTimeMillis() - start)); } 379 } 380 381 if (state == UNINSTALLED) { 382 context.close(); 383 context = null; 384 throw new BundleException(NLS.bind(Msg.BUNDLE_UNINSTALLED_EXCEPTION, getBundleData().getLocation())); 385 } 386 } 387 388 protected boolean readyToResume() { 389 if (getStartLevel() > framework.startLevelManager.getStartLevel()) 391 return false; 392 int status = bundledata.getStatus(); 393 if ((status & Constants.BUNDLE_STARTED) == 0) 395 return false; 396 if ((status & Constants.BUNDLE_ACTIVATION_POLICY) == 0 || (status & Constants.BUNDLE_LAZY_START) == 0) 397 return true; 398 if (!isResolved()) 399 return false; 401 state = STARTING; 403 framework.publishBundleEvent(BundleEvent.LAZY_ACTIVATION, this); 404 return false; 405 } 406 407 412 protected BundleContextImpl createContext() { 413 return (new BundleContextImpl(this)); 414 } 415 416 421 protected synchronized BundleContextImpl getContext() { 422 if (context == null) { 423 if ((state & (STARTING | ACTIVE | STOPPING)) != 0) 426 context = createContext(); 427 } 428 return (context); 429 } 430 431 436 protected void stopWorker(int options) throws BundleException { 437 if ((options & STOP_TRANSIENT) == 0) { 438 setStatus(Constants.BUNDLE_STARTED, false); 439 setStatus(Constants.BUNDLE_ACTIVATION_POLICY, false); 440 if (Debug.DEBUG && Debug.MONITOR_ACTIVATION) 441 new Exception ("A persistent start has been called on bundle: " + getBundleData()).printStackTrace(); 442 } 443 if (framework.active) { 444 if ((state & (STOPPING | RESOLVED | INSTALLED)) != 0) { 445 return; 446 } 447 if (Debug.DEBUG) { 448 BundleWatcher bundleStats = framework.adaptor.getBundleWatcher(); 449 if (bundleStats != null) 450 bundleStats.watchBundle(this, BundleWatcher.START_DEACTIVATION); 451 } 452 state = STOPPING; 453 framework.publishBundleEvent(BundleEvent.STOPPING, this); 454 try { 455 if (context != null) 457 context.stop(); 458 } finally { 459 if (context != null) { 460 context.close(); 461 context = null; 462 } 463 464 checkValid(); 465 466 state = RESOLVED; 467 468 if (Debug.DEBUG && Debug.DEBUG_GENERAL) { 469 Debug.println("->stopped " + this); } 471 472 framework.publishBundleEvent(BundleEvent.STOPPED, this); 473 if (Debug.DEBUG) { 474 BundleWatcher bundleStats = framework.adaptor.getBundleWatcher(); 475 if (bundleStats != null) 476 bundleStats.watchBundle(this, BundleWatcher.END_DEACTIVATION); 477 } 478 } 479 } 480 } 481 482 498 public org.osgi.framework.ServiceReference[] getRegisteredServices() { 499 checkValid(); 500 501 if (context == null) { 502 return (null); 503 } 504 505 return (context.getRegisteredServices()); 506 } 507 508 524 public org.osgi.framework.ServiceReference[] getServicesInUse() { 525 checkValid(); 526 527 if (context == null) { 528 return (null); 529 } 530 531 return (context.getServicesInUse()); 532 } 533 534 protected Bundle[] getFragments() { 535 synchronized (framework.bundles) { 536 if (fragments == null) 537 return null; 538 Bundle[] result = new Bundle[fragments.length]; 539 System.arraycopy(fragments, 0, result, 0, result.length); 540 return result; 541 } 542 } 543 544 552 protected void attachFragment(BundleFragment fragment) throws BundleException { 553 BundleLoader loader = getLoaderProxy().getBasicBundleLoader(); 555 if (loader != null) 558 loader.attachFragment(fragment); 559 560 if (fragments == null) { 561 fragments = new BundleFragment[] {fragment}; 562 } else { 563 boolean inserted = false; 564 BundleFragment[] newFragments = new BundleFragment[fragments.length + 1]; 567 for (int i = 0; i < fragments.length; i++) { 568 if (fragment == fragments[i]) 569 return; if (!inserted && fragment.getBundleId() < fragments[i].getBundleId()) { 571 if (loader != null) { 575 throw new BundleException(NLS.bind(Msg.BUNDLE_LOADER_ATTACHMENT_ERROR, fragments[i].getSymbolicName(), getSymbolicName())); 576 } 577 newFragments[i] = fragment; 578 inserted = true; 579 } 580 newFragments[inserted ? i + 1 : i] = fragments[i]; 581 } 582 if (!inserted) 583 newFragments[newFragments.length - 1] = fragment; 584 fragments = newFragments; 585 } 586 } 587 588 protected BundleLoader getBundleLoader() { 589 BundleLoaderProxy curProxy = getLoaderProxy(); 590 return curProxy == null ? null : curProxy.getBundleLoader(); 591 } 592 593 protected synchronized BundleLoaderProxy getLoaderProxy() { 594 if (proxy != null) 595 return proxy; 596 BundleDescription bundleDescription = getBundleDescription(); 597 if (bundleDescription == null) 598 return null; 599 proxy = new BundleLoaderProxy(this, bundleDescription); 600 bundleDescription.setUserObject(proxy); 601 return proxy; 602 } 603 604 static void closeBundleLoader(BundleLoaderProxy proxy) { 605 if (proxy == null) 606 return; 607 BundleLoader loader = proxy.getBasicBundleLoader(); 609 if (loader != null) 610 loader.close(); 611 proxy.setStale(); 612 BundleDescription description = proxy.getBundleDescription(); 615 description.setUserObject(null); 616 } 617 } 618 | Popular Tags |