| 1 11 12 package org.eclipse.osgi.framework.internal.core; 13 14 import java.io.File ; 15 import java.io.InputStream ; 16 import java.security.*; 17 import java.util.*; 18 import org.eclipse.osgi.event.BatchBundleListener; 19 import org.eclipse.osgi.framework.debug.Debug; 20 import org.eclipse.osgi.framework.eventmgr.EventDispatcher; 21 import org.eclipse.osgi.framework.eventmgr.EventListeners; 22 import org.eclipse.osgi.internal.profile.Profile; 23 import org.eclipse.osgi.util.NLS; 24 import org.osgi.framework.*; 25 26 32 33 public class BundleContextImpl implements BundleContext, EventDispatcher { 34 public static final String PROP_SCOPE_SERVICE_EVENTS = "osgi.scopeServiceEvents"; public static final boolean scopeEvents = Boolean.valueOf(FrameworkProperties.getProperty(PROP_SCOPE_SERVICE_EVENTS, "true")).booleanValue(); 37 private boolean valid; 38 39 40 protected BundleHost bundle; 44 45 46 protected Framework framework; 47 48 50 protected Hashtable servicesInUse; 51 52 53 protected EventListeners bundleEvent; 54 55 56 protected EventListeners bundleEventSync; 57 58 59 protected EventListeners serviceEvent; 60 61 62 protected EventListeners frameworkEvent; 63 64 65 protected BundleActivator activator; 66 67 68 protected Object contextLock = new Object (); 69 70 76 protected BundleContextImpl(BundleHost bundle) { 77 this.bundle = bundle; 78 valid = true; 79 framework = bundle.framework; 80 bundleEvent = null; 81 bundleEventSync = null; 82 serviceEvent = null; 83 frameworkEvent = null; 84 servicesInUse = null; 85 activator = null; 86 } 87 88 92 protected void close() { 93 valid = false; 94 95 synchronized (framework.serviceEvent) { 96 if (serviceEvent != null) { 97 framework.serviceEvent.removeListener(this); 98 serviceEvent = null; 99 } 100 } 101 synchronized (framework.frameworkEvent) { 102 if (frameworkEvent != null) { 103 framework.frameworkEvent.removeListener(this); 104 frameworkEvent = null; 105 } 106 } 107 synchronized (framework.bundleEvent) { 108 if (bundleEvent != null) { 109 framework.bundleEvent.removeListener(this); 110 bundleEvent = null; 111 } 112 } 113 synchronized (framework.bundleEventSync) { 114 if (bundleEventSync != null) { 115 framework.bundleEventSync.removeListener(this); 116 bundleEventSync = null; 117 } 118 } 119 120 121 ServiceReference[] publishedReferences = null; 122 synchronized (framework.serviceRegistry) { 123 publishedReferences = framework.serviceRegistry.lookupServiceReferences(this); 124 } 125 126 if (publishedReferences != null) { 127 for (int i = 0; i < publishedReferences.length; i++) { 128 try { 129 ((ServiceReferenceImpl) publishedReferences[i]).registration.unregister(); 130 } catch (IllegalStateException e) { 131 132 } 133 } 134 } 135 136 137 if (servicesInUse != null) { 138 int usedSize; 139 ServiceReference[] usedRefs = null; 140 141 synchronized (servicesInUse) { 142 usedSize = servicesInUse.size(); 143 144 if (usedSize > 0) { 145 if (Debug.DEBUG && Debug.DEBUG_SERVICES) { 146 Debug.println("Releasing services"); } 148 149 usedRefs = new ServiceReference[usedSize]; 150 151 Enumeration refsEnum = servicesInUse.keys(); 152 for (int i = 0; i < usedSize; i++) { 153 usedRefs[i] = (ServiceReference) refsEnum.nextElement(); 154 } 155 } 156 } 157 158 for (int i = 0; i < usedSize; i++) { 159 ((ServiceReferenceImpl) usedRefs[i]).registration.releaseService(this); 160 } 161 162 servicesInUse = null; 163 } 164 165 bundle = null; 166 } 167 168 175 public String getProperty(String key) { 176 SecurityManager sm = System.getSecurityManager(); 177 178 if (sm != null) { 179 sm.checkPropertyAccess(key); 180 } 181 182 return (framework.getProperty(key)); 183 } 184 185 190 public org.osgi.framework.Bundle getBundle() { 191 checkValid(); 192 193 return (bundle); 194 } 195 196 207 public org.osgi.framework.Bundle installBundle(String location) throws BundleException { 208 checkValid(); 209 return framework.installBundle(location); 211 } 212 213 226 public org.osgi.framework.Bundle installBundle(String location, InputStream in) throws BundleException { 227 checkValid(); 228 return framework.installBundle(location, in); 230 } 231 232 239 public org.osgi.framework.Bundle getBundle(long id) { 240 return (framework.getBundle(id)); 241 } 242 243 250 public AbstractBundle getBundleByLocation(String location) { 251 return (framework.getBundleByLocation(location)); 252 } 253 254 263 public org.osgi.framework.Bundle[] getBundles() { 264 return framework.getAllBundles(); 265 } 266 267 298 public void addServiceListener(ServiceListener listener, String filter) throws InvalidSyntaxException { 299 checkValid(); 300 301 if (Debug.DEBUG && Debug.DEBUG_EVENTS) { 302 String listenerName = listener.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(listener)); Debug.println("addServiceListener[" + bundle + "](" + listenerName + ", \"" + filter + "\")"); } 305 306 ServiceListener filteredListener = new FilteredServiceListener(filter, listener, this); 307 308 synchronized (framework.serviceEvent) { 309 if (serviceEvent == null) { 310 serviceEvent = new EventListeners(); 311 framework.serviceEvent.addListener(this, this); 312 } 313 314 serviceEvent.addListener(listener, filteredListener); 315 } 316 } 317 318 327 public void addServiceListener(ServiceListener listener) { 328 try { 329 addServiceListener(listener, null); 330 } catch (InvalidSyntaxException e) { 331 if (Debug.DEBUG && Debug.DEBUG_GENERAL) { 332 Debug.println("InvalidSyntaxException w/ null filter" + e.getMessage()); Debug.printStackTrace(e); 334 } 335 } 336 } 337 338 351 public void removeServiceListener(ServiceListener listener) { 352 checkValid(); 353 354 if (Debug.DEBUG && Debug.DEBUG_SERVICES) { 355 String listenerName = listener.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(listener)); Debug.println("removeServiceListener[" + bundle + "](" + listenerName + ")"); } 358 359 synchronized (framework.serviceEvent) { 360 if (serviceEvent != null) { 361 serviceEvent.removeListener(listener); 362 } 363 } 364 } 365 366 380 public void addBundleListener(BundleListener listener) { 381 checkValid(); 382 383 if (Debug.DEBUG && Debug.DEBUG_EVENTS) { 384 String listenerName = listener.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(listener)); Debug.println("addBundleListener[" + bundle + "](" + listenerName + ")"); } 387 388 if (listener instanceof SynchronousBundleListener) { 389 framework.checkAdminPermission(getBundle(), AdminPermission.LISTENER); 390 synchronized (framework.bundleEventSync) { 391 if (bundleEventSync == null) { 392 bundleEventSync = new EventListeners(); 393 framework.bundleEventSync.addListener(this, this); 394 } 395 396 bundleEventSync.addListener(listener, listener); 397 } 398 } else { 399 synchronized (framework.bundleEvent) { 400 if (bundleEvent == null) { 401 bundleEvent = new EventListeners(); 402 framework.bundleEvent.addListener(this, this); 403 } 404 405 bundleEvent.addListener(listener, listener); 406 } 407 } 408 } 409 410 423 public void removeBundleListener(BundleListener listener) { 424 checkValid(); 425 426 if (Debug.DEBUG && Debug.DEBUG_EVENTS) { 427 String listenerName = listener.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(listener)); Debug.println("removeBundleListener[" + bundle + "](" + listenerName + ")"); } 430 431 if (listener instanceof SynchronousBundleListener) { 432 framework.checkAdminPermission(getBundle(), AdminPermission.LISTENER); 433 434 synchronized (framework.bundleEventSync) { 435 if (bundleEventSync != null) { 436 bundleEventSync.removeListener(listener); 437 } 438 } 439 } else { 440 synchronized (framework.bundleEvent) { 441 if (bundleEvent != null) { 442 bundleEvent.removeListener(listener); 443 } 444 } 445 } 446 } 447 448 461 public void addFrameworkListener(FrameworkListener listener) { 462 checkValid(); 463 464 if (Debug.DEBUG && Debug.DEBUG_EVENTS) { 465 String listenerName = listener.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(listener)); Debug.println("addFrameworkListener[" + bundle + "](" + listenerName + ")"); } 468 469 synchronized (framework.frameworkEvent) { 470 if (frameworkEvent == null) { 471 frameworkEvent = new EventListeners(); 472 framework.frameworkEvent.addListener(this, this); 473 } 474 475 frameworkEvent.addListener(listener, listener); 476 } 477 } 478 479 492 public void removeFrameworkListener(FrameworkListener listener) { 493 checkValid(); 494 495 if (Debug.DEBUG && Debug.DEBUG_EVENTS) { 496 String listenerName = listener.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(listener)); Debug.println("removeFrameworkListener[" + bundle + "](" + listenerName + ")"); } 499 500 synchronized (framework.frameworkEvent) { 501 if (frameworkEvent != null) { 502 frameworkEvent.removeListener(listener); 503 } 504 } 505 } 506 507 568 public org.osgi.framework.ServiceRegistration registerService(String [] clazzes, Object service, Dictionary properties) { 569 checkValid(); 570 571 if (service == null) { 572 if (Debug.DEBUG && Debug.DEBUG_SERVICES) { 573 Debug.println("Service object is null"); } 575 576 throw new IllegalArgumentException (Msg.SERVICE_ARGUMENT_NULL_EXCEPTION); 577 } 578 579 int size = clazzes.length; 580 581 if (size == 0) { 582 if (Debug.DEBUG && Debug.DEBUG_SERVICES) { 583 Debug.println("Classes array is empty"); } 585 586 throw new IllegalArgumentException (Msg.SERVICE_EMPTY_CLASS_LIST_EXCEPTION); 587 } 588 589 590 String [] copy = new String [clazzes.length]; 591 for (int i = clazzes.length - 1; i >= 0; i--) 593 copy[i] = clazzes[i].intern(); 594 clazzes = copy; 595 596 597 framework.checkRegisterServicePermission(clazzes); 598 599 if (!(service instanceof ServiceFactory)) { 600 String invalidService = checkServiceClass(clazzes, service); 601 if (invalidService != null) { 602 if (Debug.DEBUG && Debug.DEBUG_SERVICES) { 603 Debug.println("Service object is not an instanceof " + invalidService); } 605 throw new IllegalArgumentException (NLS.bind(Msg.SERVICE_NOT_INSTANCEOF_CLASS_EXCEPTION, invalidService)); 606 } 607 } 608 609 return (createServiceRegistration(clazzes, service, properties)); 610 } 611 612 static String checkServiceClass(final String [] clazzes, final Object serviceObject) { 614 ClassLoader cl = (ClassLoader ) AccessController.doPrivileged(new PrivilegedAction() { 615 public Object run() { 616 return serviceObject.getClass().getClassLoader(); 617 } 618 }); 619 for (int i = 0; i < clazzes.length; i++) { 620 try { 621 Class serviceClazz = cl == null ? Class.forName(clazzes[i]) : cl.loadClass(clazzes[i]); 622 if (!serviceClazz.isInstance(serviceObject)) 623 return clazzes[i]; 624 } catch (ClassNotFoundException e) { 625 if (extensiveCheckServiceClass(clazzes[i], serviceObject.getClass())) 627 return clazzes[i]; 628 } 629 } 630 return null; 631 } 632 633 private static boolean extensiveCheckServiceClass(String clazz, Class serviceClazz) { 634 if (clazz.equals(serviceClazz.getName())) 635 return false; 636 Class [] interfaces = serviceClazz.getInterfaces(); 637 for (int i = 0; i < interfaces.length; i++) 638 if (!extensiveCheckServiceClass(clazz, interfaces[i])) 639 return false; 640 Class superClazz = serviceClazz.getSuperclass(); 641 if (superClazz != null) 642 if (!extensiveCheckServiceClass(clazz, superClazz)) 643 return false; 644 return true; 645 } 646 647 656 protected ServiceRegistrationImpl createServiceRegistration(String [] clazzes, Object service, Dictionary properties) { 657 return (new ServiceRegistrationImpl(this, clazzes, service, properties)); 658 } 659 660 672 public org.osgi.framework.ServiceRegistration registerService(String clazz, Object service, Dictionary properties) { 673 String [] clazzes = new String [] {clazz}; 674 675 return (registerService(clazzes, service, properties)); 676 } 677 678 721 public org.osgi.framework.ServiceReference[] getServiceReferences(String clazz, String filter) throws InvalidSyntaxException { 722 checkValid(); 723 if (Debug.DEBUG && Debug.DEBUG_SERVICES) { 724 Debug.println("getServiceReferences(" + clazz + ", \"" + filter + "\")"); } 726 return (framework.getServiceReferences(clazz, filter, this, false)); 727 } 728 729 public ServiceReference[] getAllServiceReferences(String clazz, String filter) throws InvalidSyntaxException { 730 checkValid(); 731 if (Debug.DEBUG && Debug.DEBUG_SERVICES) { 732 Debug.println("getAllServiceReferences(" + clazz + ", \"" + filter + "\")"); } 734 return (framework.getServiceReferences(clazz, filter, this, true)); 735 } 736 737 757 public org.osgi.framework.ServiceReference getServiceReference(String clazz) { 758 checkValid(); 759 760 if (Debug.DEBUG && Debug.DEBUG_SERVICES) { 761 Debug.println("getServiceReference(" + clazz + ")"); } 763 764 try { 765 ServiceReference[] references = framework.getServiceReferences(clazz, null, this, false); 766 767 if (references != null) { 768 int index = 0; 769 770 int length = references.length; 771 772 if (length > 1) { 773 int rankings[] = new int[length]; 774 int count = 0; 775 int maxRanking = Integer.MIN_VALUE; 776 777 for (int i = 0; i < length; i++) { 778 int ranking = ((ServiceReferenceImpl) references[i]).getRanking(); 779 780 rankings[i] = ranking; 781 782 if (ranking > maxRanking) { 783 index = i; 784 maxRanking = ranking; 785 count = 1; 786 } else { 787 if (ranking == maxRanking) { 788 count++; 789 } 790 } 791 } 792 793 if (count > 1) { 794 long minId = Long.MAX_VALUE; 795 796 for (int i = 0; i < length; i++) { 797 if (rankings[i] == maxRanking) { 798 long id = ((ServiceReferenceImpl) references[i]).getId(); 799 800 if (id < minId) { 801 index = i; 802 minId = id; 803 } 804 } 805 } 806 } 807 } 808 809 return (references[index]); 810 } 811 } catch (InvalidSyntaxException e) { 812 if (Debug.DEBUG && Debug.DEBUG_GENERAL) { 813 Debug.println("InvalidSyntaxException w/ null filter" + e.getMessage()); Debug.printStackTrace(e); 815 } 816 } 817 818 return (null); 819 } 820 821 |