diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-10-29 06:48:59 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-10-29 06:48:59 +0000 |
commit | 4a473240fcfdecb8f7e0a03c3e252d87f1917632 (patch) | |
tree | 2e5fd75a7a9b3185415ffd5b0a03281a2403eae4 /java/sca/modules/extensibility-equinox/src/main/java | |
parent | 3a5430b346320015a8f97c9cc494223452df11d2 (diff) |
Refactor the TCCL processing to ClassLoaderContext
Handle the OSGi service un-registration gracefully
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@830852 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules/extensibility-equinox/src/main/java')
-rw-r--r-- | java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/OSGiExtensionPointRegistry.java | 65 |
1 files changed, 15 insertions, 50 deletions
diff --git a/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/OSGiExtensionPointRegistry.java b/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/OSGiExtensionPointRegistry.java index 470d681555..bd92312235 100644 --- a/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/OSGiExtensionPointRegistry.java +++ b/java/sca/modules/extensibility-equinox/src/main/java/org/apache/tuscany/sca/extensibility/equinox/OSGiExtensionPointRegistry.java @@ -21,14 +21,10 @@ package org.apache.tuscany.sca.extensibility.equinox; import java.util.Dictionary; import java.util.Hashtable; -import java.util.IdentityHashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import java.util.logging.Level; -import java.util.logging.Logger; import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; -import org.apache.tuscany.sca.core.LifeCycleListener; import org.apache.tuscany.sca.extensibility.ServiceDeclaration; import org.apache.tuscany.sca.extensibility.ServiceDiscovery; import org.osgi.framework.Bundle; @@ -38,11 +34,10 @@ import org.osgi.framework.ServiceReference; import org.osgi.framework.ServiceRegistration; /** - * + * OSGi ServiceRegistry based extension point registry */ public class OSGiExtensionPointRegistry extends DefaultExtensionPointRegistry { - private static final Logger logger = Logger.getLogger(OSGiExtensionPointRegistry.class.getName()); - private Map<Class<?>, ServiceRegistration> services = new ConcurrentHashMap<Class<?>, ServiceRegistration>(); + private Map<Class<?>, ServiceRegistration> registrations = new ConcurrentHashMap<Class<?>, ServiceRegistration>(); private BundleContext bundleContext; public OSGiExtensionPointRegistry(BundleContext bundleContext) { @@ -51,27 +46,9 @@ public class OSGiExtensionPointRegistry extends DefaultExtensionPointRegistry { } @Override - protected <T> Object findExtensionPoint(Class<T> extensionPointType) { - ServiceRegistration registration = services.get(extensionPointType); - if (registration != null) { - ServiceReference ref = registration.getReference(); - if (ref != null) { - return ref.getBundle().getBundleContext().getService(ref); - } - } - /* - else { - ServiceReference ref = bundleContext.getServiceReference(extensionPointType.getName()); - if (ref != null) { - return bundleContext.getService(ref); - } - } - */ - return null; - } - - @Override - protected void registerExtensionPoint(Class<?> i, Object extensionPoint, ServiceDeclaration declaration) { + protected void registerExtensionPoint(Class<?> extensionPointType, + Object extensionPoint, + ServiceDeclaration declaration) { BundleContext context = bundleContext; if (declaration instanceof EquinoxServiceDiscoverer.ServiceDeclarationImpl) { EquinoxServiceDiscoverer.ServiceDeclarationImpl declarationImpl = @@ -92,46 +69,34 @@ public class OSGiExtensionPointRegistry extends DefaultExtensionPointRegistry { context = bundle.getBundleContext(); } Dictionary<Object, Object> props = new Hashtable<Object, Object>(); - ServiceRegistration registration = context.registerService(i.getName(), extensionPoint, props); - services.put(i, registration); + ServiceRegistration registration = context.registerService(extensionPointType.getName(), extensionPoint, props); + registrations.put(extensionPointType, registration); + super.registerExtensionPoint(extensionPointType, extensionPoint, declaration); } @Override protected void unregisterExtensionPoint(Class<?> i) { - ServiceRegistration registration = services.get(i); + ServiceRegistration registration = registrations.remove(i); if (registration != null) { registration.unregister(); } - services.remove(i); + super.unregisterExtensionPoint(i); } @Override public synchronized void stop() { - // Get a unique map as an extension point may exist in the map by different keys - Map<LifeCycleListener, LifeCycleListener> map = new IdentityHashMap<LifeCycleListener, LifeCycleListener>(); - for (ServiceRegistration reg : services.values()) { + for (ServiceRegistration reg : registrations.values()) { try { ServiceReference ref = reg.getReference(); if (ref != null) { - Object service = bundleContext.getService(ref); - if (service instanceof LifeCycleListener) { - LifeCycleListener activator = (LifeCycleListener)service; - map.put(activator, activator); - } reg.unregister(); } - } catch (Throwable e) { - logger.log(Level.WARNING, e.getMessage(), e); - } - } - for (LifeCycleListener activator : map.values()) { - try { - activator.stop(); - } catch (Throwable e) { - logger.log(Level.WARNING, e.getMessage(), e); + } catch (IllegalStateException e) { + // Ignore it, the service has been unregistered when the owning bundle stops } } - services.clear(); + registrations.clear(); + super.stop(); } } |