diff --git a/environments/osgi/core/extension/src/main/java/org/jboss/weld/environment/osgi/impl/extension/ExtensionActivator.java b/environments/osgi/core/extension/src/main/java/org/jboss/weld/environment/osgi/impl/extension/ExtensionActivator.java index a8e996c04fa..1e18a181e3a 100644 --- a/environments/osgi/core/extension/src/main/java/org/jboss/weld/environment/osgi/impl/extension/ExtensionActivator.java +++ b/environments/osgi/core/extension/src/main/java/org/jboss/weld/environment/osgi/impl/extension/ExtensionActivator.java @@ -169,8 +169,8 @@ public void bundleChanged(BundleEvent event) { break; } for (ServiceReference reference : cdiEventServiceReferences) { - boolean set = WeldOSGiExtension.currentBundle.get() != null; - WeldOSGiExtension.currentBundle.set(reference.getBundle().getBundleId()); + BundleContext previousContext = WeldOSGiExtension.setCurrentContext(reference.getBundle().getBundleContext()); + Bundle previousBundle = WeldOSGiExtension.setCurrentBundle(reference.getBundle()); Event broadcastingCDIEvent = (Event) context.getService(reference); try { @@ -182,9 +182,8 @@ public void bundleChanged(BundleEvent event) { if (resultingWeldOSGiBundleEvent != null) { fireAllEvent(resultingWeldOSGiBundleEvent, broadcastingCDIEvent); } - if (!set) { - WeldOSGiExtension.currentBundle.remove(); - } + WeldOSGiExtension.setCurrentBundle(previousBundle); + WeldOSGiExtension.setCurrentContext(previousContext); } } } @@ -229,8 +228,8 @@ public void serviceChanged(ServiceEvent event) { break; } for (ServiceReference reference : cdiInstanceServiceReferences) { - boolean set = WeldOSGiExtension.currentBundle.get() != null; - WeldOSGiExtension.currentBundle.set(reference.getBundle().getBundleId()); + BundleContext previousContext = WeldOSGiExtension.setCurrentContext(reference.getBundle().getBundleContext()); + Bundle previousBundle = WeldOSGiExtension.setCurrentBundle(reference.getBundle()); Instance instance = (Instance) context.getService(reference); try { @@ -246,9 +245,8 @@ public void serviceChanged(ServiceEvent event) { catch(Throwable t) { //ignore } - if (!set) { - WeldOSGiExtension.currentBundle.remove(); - } + WeldOSGiExtension.setCurrentBundle(previousBundle); + WeldOSGiExtension.setCurrentContext(previousContext); } } } diff --git a/environments/osgi/core/extension/src/main/java/org/jboss/weld/environment/osgi/impl/extension/beans/DynamicServiceHandler.java b/environments/osgi/core/extension/src/main/java/org/jboss/weld/environment/osgi/impl/extension/beans/DynamicServiceHandler.java index 62899adff99..526a584dbee 100644 --- a/environments/osgi/core/extension/src/main/java/org/jboss/weld/environment/osgi/impl/extension/beans/DynamicServiceHandler.java +++ b/environments/osgi/core/extension/src/main/java/org/jboss/weld/environment/osgi/impl/extension/beans/DynamicServiceHandler.java @@ -101,7 +101,8 @@ public Object invoke(Object proxy, Method method, Object[] args) logger.trace("Call on the DynamicServiceHandler {} for method {}", this, method); - WeldOSGiExtension.currentBundle.set(ctx.getBundle().getBundleId()); + BundleContext previousContext = WeldOSGiExtension.setCurrentContext(ctx); + Bundle previousBundle = WeldOSGiExtension.setCurrentBundle(ctx.getBundle()); //intercept HashCode method when the handler is not allready registered //map.put() need a correct hashCode() method to use //see OSGiServiceBean @@ -139,7 +140,8 @@ public Object invoke(Object proxy, Method method, Object[] args) } finally { ctx.ungetService(reference); - WeldOSGiExtension.currentBundle.remove(); + WeldOSGiExtension.setCurrentBundle(previousBundle); + WeldOSGiExtension.setCurrentContext(previousContext); } /*Object instanceToUse = this.tracker.waitForService(timeout); try { diff --git a/environments/osgi/core/extension/src/main/java/org/jboss/weld/environment/osgi/impl/extension/service/EmbeddedContainer.java b/environments/osgi/core/extension/src/main/java/org/jboss/weld/environment/osgi/impl/extension/service/EmbeddedContainer.java index 748b2c094f3..c7444e0d979 100644 --- a/environments/osgi/core/extension/src/main/java/org/jboss/weld/environment/osgi/impl/extension/service/EmbeddedContainer.java +++ b/environments/osgi/core/extension/src/main/java/org/jboss/weld/environment/osgi/impl/extension/service/EmbeddedContainer.java @@ -87,12 +87,14 @@ public EmbeddedContainer(BundleContext context) { public EmbeddedCDIContainer initialize() { logger.trace("Entering EmbeddedContainer : " + "initialize() with no parameter"); - WeldOSGiExtension.currentContext.set(context); + Bundle previousBundle = WeldOSGiExtension.setCurrentBundle(context.getBundle()); + BundleContext previousContext = WeldOSGiExtension.setCurrentContext(context); container.initialize(); context.addBundleListener(listener); context.addServiceListener(listener); container.getEvent(); - WeldOSGiExtension.currentContext.remove(); + WeldOSGiExtension.setCurrentBundle(previousBundle); + WeldOSGiExtension.setCurrentContext(previousContext); return container; } @@ -167,8 +169,8 @@ public void bundleChanged(BundleEvent event) { bundleEvent = new BundleEvents.BundleUpdated(bundle); break; } - boolean set = WeldOSGiExtension.currentBundle.get() != null; - WeldOSGiExtension.currentBundle.set(context.getBundle().getBundleId()); + Bundle previousBundle = WeldOSGiExtension.setCurrentBundle(context.getBundle()); + BundleContext previousContext = WeldOSGiExtension.setCurrentContext(context); try { //broadcast the OSGi event through CDI event system container.getEvent().select(BundleEvent.class).fire(event); @@ -180,9 +182,8 @@ public void bundleChanged(BundleEvent event) { //broadcast the corresponding Weld-OSGi event fireAllEvent(bundleEvent, container.getEvent()); } - if (!set) { - WeldOSGiExtension.currentBundle.remove(); - } + WeldOSGiExtension.setCurrentBundle(previousBundle); + WeldOSGiExtension.setCurrentContext(previousContext); } @Override @@ -203,8 +204,8 @@ public void serviceChanged(ServiceEvent event) { serviceEvent = new ServiceEvents.ServiceDeparture(ref, context); break; } - boolean set = WeldOSGiExtension.currentBundle.get() != null; - WeldOSGiExtension.currentBundle.set(context.getBundle().getBundleId()); + Bundle previousBundle = WeldOSGiExtension.setCurrentBundle(context.getBundle()); + BundleContext previousContext = WeldOSGiExtension.setCurrentContext(context); try { //broadcast the OSGi event through CDI event system container.getEvent().select(ServiceEvent.class).fire(event); @@ -216,9 +217,8 @@ public void serviceChanged(ServiceEvent event) { //broadcast the corresponding Weld-OSGi event fireAllEvent(serviceEvent, container.getEvent(), container.getInstance()); } - if (!set) { - WeldOSGiExtension.currentBundle.remove(); - } + WeldOSGiExtension.setCurrentBundle(previousBundle); + WeldOSGiExtension.setCurrentContext(previousContext); } private void fireAllEvent(AbstractServiceEvent event, Event broadcaster, Instance instance) { diff --git a/environments/osgi/core/extension/src/main/java/org/jboss/weld/environment/osgi/impl/extension/service/WeldOSGiExtension.java b/environments/osgi/core/extension/src/main/java/org/jboss/weld/environment/osgi/impl/extension/service/WeldOSGiExtension.java index 05843a4fd15..83ab63fb99f 100644 --- a/environments/osgi/core/extension/src/main/java/org/jboss/weld/environment/osgi/impl/extension/service/WeldOSGiExtension.java +++ b/environments/osgi/core/extension/src/main/java/org/jboss/weld/environment/osgi/impl/extension/service/WeldOSGiExtension.java @@ -104,8 +104,8 @@ public class WeldOSGiExtension implements Extension { WeldOSGiExtension.class); private static boolean autoRunInHybridMode = true; // hack for weld integration - public static ThreadLocal currentBundle = new ThreadLocal(); - public static ThreadLocal currentContext = new ThreadLocal(); + private static ThreadLocal currentContext = new ThreadLocal(); + private static ThreadLocal currentBundle = new ThreadLocal(); private HashMap> servicesToBeInjected = new HashMap>(); private HashMap> serviceProducerToBeInjected = @@ -119,6 +119,34 @@ public class WeldOSGiExtension implements Extension { private HybridListener listener; private BundleContextDelegate delegate; + public static BundleContext setCurrentContext(BundleContext current) { + BundleContext previous = currentContext.get(); + if (current == null) { + currentContext.remove(); + } else { + currentContext.set(current); + } + return previous; + } + + public static Bundle setCurrentBundle(Bundle current) { + Bundle previous = currentBundle.get(); + if (current == null) { + currentBundle.remove(); + } else { + currentBundle.set(current); + } + return previous; + } + + public static Bundle getCurrentBundle() { + return currentBundle.get(); + } + + public static BundleContext getCurrentContext() { + return currentContext.get(); + } + void registerCDIOSGiBeans(@Observes BeforeBeanDiscovery event, BeanManager manager) { logger.debug("Observe a BeforeBeanDiscovery event"); @@ -137,7 +165,7 @@ void registerCDIOSGiBeans(@Observes BeforeBeanDiscovery event, this.beanManager = manager; if (!Activator.osgiStarted() && WeldOSGiExtension.autoRunInHybridMode) { delegate = new BundleContextDelegate(); - currentContext.set(delegate); + setCurrentContext(delegate); } } @@ -292,15 +320,14 @@ private void runExtensionInHybridMode() { * Provided for server integration purposes. * * @param bc - * @param activator */ private HybridListener runInHybridMode(BundleContext bc) { HybridListener list = new HybridListener(bc, this); delegate.setContext(bc); bc.addBundleListener(list); bc.addServiceListener(list); - currentContext.set(bc); - currentBundle.set(bc.getBundle().getBundleId()); + setCurrentContext(bc); + setCurrentBundle(bc.getBundle()); return list; } @@ -313,8 +340,8 @@ public void removeListeners() { void afterDeployment(@Observes AfterDeploymentValidation event) { if (listener != null) { - currentContext.remove(); - currentBundle.remove(); + setCurrentContext(null); + setCurrentBundle(null); } } @@ -385,7 +412,7 @@ private void addService(AfterBeanDiscovery event, for (Iterator iterator = injectionPoints.iterator(); iterator.hasNext();) { final InjectionPoint injectionPoint = iterator.next(); - beans.add(new OSGiServiceBean(injectionPoint, currentContext.get())); + beans.add(new OSGiServiceBean(injectionPoint, getCurrentContext())); } for (OSGiServiceBean bean : beans) { event.addBean(bean); @@ -398,7 +425,7 @@ private void addServiceProducer(AfterBeanDiscovery event, for (Iterator iterator = injectionPoints.iterator(); iterator.hasNext();) { final InjectionPoint injectionPoint = iterator.next(); - beans.add(new OSGiServiceProducerBean(injectionPoint, currentContext.get())); + beans.add(new OSGiServiceProducerBean(injectionPoint, getCurrentContext())); } for (OSGiServiceProducerBean bean : beans) { event.addBean(bean); @@ -496,8 +523,8 @@ public void bundleChanged(BundleEvent event) { bundleEvent = new BundleEvents.BundleUpdated(bundle); break; } - boolean set = WeldOSGiExtension.currentBundle.get() != null; - WeldOSGiExtension.currentBundle.set(context.getBundle().getBundleId()); + Bundle previousBundle = WeldOSGiExtension.setCurrentBundle(context.getBundle()); + BundleContext previousContext = WeldOSGiExtension.setCurrentContext(context); try { //broadcast the OSGi event through CDI event system extension.beanManager.fireEvent(event); @@ -509,9 +536,8 @@ public void bundleChanged(BundleEvent event) { //broadcast the corresponding Weld-OSGi event fireAllBundleEvent(bundleEvent); } - if (!set) { - WeldOSGiExtension.currentBundle.remove(); - } + WeldOSGiExtension.setCurrentBundle(previousBundle); + WeldOSGiExtension.setCurrentContext(previousContext); } @Override @@ -532,8 +558,8 @@ public void serviceChanged(ServiceEvent event) { serviceEvent = new ServiceEvents.ServiceDeparture(ref, context); break; } - boolean set = WeldOSGiExtension.currentBundle.get() != null; - WeldOSGiExtension.currentBundle.set(context.getBundle().getBundleId()); + Bundle previousBundle = WeldOSGiExtension.setCurrentBundle(context.getBundle()); + BundleContext previousContext = WeldOSGiExtension.setCurrentContext(context); try { //broadcast the OSGi event through CDI event system extension.beanManager.fireEvent(event); @@ -545,9 +571,8 @@ public void serviceChanged(ServiceEvent event) { //broadcast the corresponding Weld-OSGi event fireAllServiceEvent(serviceEvent); } - if (!set) { - WeldOSGiExtension.currentBundle.remove(); - } + WeldOSGiExtension.setCurrentBundle(previousBundle); + WeldOSGiExtension.setCurrentContext(previousContext); } private void fireAllServiceEvent(AbstractServiceEvent event) { diff --git a/environments/osgi/core/extension/src/main/java/org/jboss/weld/environment/osgi/impl/integration/IntegrationActivator.java b/environments/osgi/core/extension/src/main/java/org/jboss/weld/environment/osgi/impl/integration/IntegrationActivator.java index b9bfac1697c..6bf3b1bf89c 100644 --- a/environments/osgi/core/extension/src/main/java/org/jboss/weld/environment/osgi/impl/integration/IntegrationActivator.java +++ b/environments/osgi/core/extension/src/main/java/org/jboss/weld/environment/osgi/impl/integration/IntegrationActivator.java @@ -211,9 +211,8 @@ private void startManagement(Bundle bundle) { return; } logger.debug("Managing {}", bundle.getSymbolicName()); - boolean set = WeldOSGiExtension.currentBundle.get() != null; - WeldOSGiExtension.currentBundle.set(bundle.getBundleId()); - WeldOSGiExtension.currentContext.set(bundle.getBundleContext()); + Bundle previousBundle = WeldOSGiExtension.setCurrentBundle(bundle); + BundleContext previousContext = WeldOSGiExtension.setCurrentContext(bundle.getBundleContext()); CDIContainer holder = factory().createContainer(bundle); logger.trace("CDI container created"); holder.initialize(); @@ -251,17 +250,15 @@ private void startManagement(Bundle bundle) { else { logger.debug("Bundle {} is not a bean bundle", bundle.getSymbolicName()); } - if (!set) { - WeldOSGiExtension.currentBundle.remove(); - } - WeldOSGiExtension.currentContext.remove(); + WeldOSGiExtension.setCurrentBundle(previousBundle); + WeldOSGiExtension.setCurrentContext(previousContext); holder.setReady(); } private void stopManagement(Bundle bundle) { logger.debug("Unmanaging {}", bundle.getSymbolicName()); - boolean set = WeldOSGiExtension.currentBundle.get() != null; - WeldOSGiExtension.currentBundle.set(bundle.getBundleId()); + Bundle previousBundle = WeldOSGiExtension.setCurrentBundle(bundle); + BundleContext previousContext = WeldOSGiExtension.setCurrentContext(bundle.getBundleContext()); CDIContainer container = managed.get(bundle.getBundleId()); if (started.get() && managed.containsKey(bundle.getBundleId())) { if (container != null) { @@ -299,13 +296,12 @@ private void stopManagement(Bundle bundle) { logger.debug("Bundle {} is not a bean bundle", bundle.getSymbolicName()); } } - if (!set) { - WeldOSGiExtension.currentBundle.remove(); - } + WeldOSGiExtension.setCurrentBundle(previousBundle); + WeldOSGiExtension.setCurrentContext(previousContext); } public CDIContainerFactory factory() { return (CDIContainerFactory) context.getService(factoryRef); } -} \ No newline at end of file +} diff --git a/environments/osgi/core/extension/src/main/java/org/jboss/weld/environment/osgi/impl/integration/ServicePublisher.java b/environments/osgi/core/extension/src/main/java/org/jboss/weld/environment/osgi/impl/integration/ServicePublisher.java index 4dbcbbcf086..6d910d53954 100644 --- a/environments/osgi/core/extension/src/main/java/org/jboss/weld/environment/osgi/impl/integration/ServicePublisher.java +++ b/environments/osgi/core/extension/src/main/java/org/jboss/weld/environment/osgi/impl/integration/ServicePublisher.java @@ -25,6 +25,7 @@ import org.jboss.weld.environment.osgi.impl.extension.beans.RegistrationsHolderImpl; import org.jboss.weld.environment.osgi.impl.extension.service.WeldOSGiExtension; import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -283,8 +284,9 @@ private void publish(Class clazz, } } if (registration != null) { - WeldOSGiExtension.currentBundle.set(bundle.getBundleId()); + Bundle previousBundle = WeldOSGiExtension.setCurrentBundle(bundle); instance.select(RegistrationsHolderImpl.class).get().addRegistration(registration); + WeldOSGiExtension.setCurrentBundle(previousBundle); } } diff --git a/environments/osgi/core/integration/src/main/java/org/jboss/weld/environment/osgi/impl/WeldCDIContainer.java b/environments/osgi/core/integration/src/main/java/org/jboss/weld/environment/osgi/impl/WeldCDIContainer.java index fb75721cc46..fd87f1fa529 100644 --- a/environments/osgi/core/integration/src/main/java/org/jboss/weld/environment/osgi/impl/WeldCDIContainer.java +++ b/environments/osgi/core/integration/src/main/java/org/jboss/weld/environment/osgi/impl/WeldCDIContainer.java @@ -90,18 +90,13 @@ public void fire(InterBundleEvent event) { + "an inter bundle event: {}", bundle, event); - Long set = WeldOSGiExtension.currentBundle.get(); - WeldOSGiExtension.currentBundle.set(bundle.getBundleId()); + Bundle previousBundle = WeldOSGiExtension.setCurrentBundle(bundle); container.getEvent().select(InterBundleEvent.class, new SpecificationAnnotation( event.type()), new SentAnnotation()).fire( event); - if (set != null) { - WeldOSGiExtension.currentBundle.set(set); - } else { - WeldOSGiExtension.currentBundle.remove(); - } + WeldOSGiExtension.setCurrentBundle(previousBundle); } @Override diff --git a/environments/osgi/core/integration/src/main/java/org/jboss/weld/environment/osgi/impl/integration/BundleSingletonProvider.java b/environments/osgi/core/integration/src/main/java/org/jboss/weld/environment/osgi/impl/integration/BundleSingletonProvider.java index 298d57f64c8..5b4ecc2b241 100644 --- a/environments/osgi/core/integration/src/main/java/org/jboss/weld/environment/osgi/impl/integration/BundleSingletonProvider.java +++ b/environments/osgi/core/integration/src/main/java/org/jboss/weld/environment/osgi/impl/integration/BundleSingletonProvider.java @@ -47,7 +47,8 @@ public BundleSingleton(Class clazz) { } private Long getId() { - Long value = WeldOSGiExtension.currentBundle.get(); + Bundle bundle = WeldOSGiExtension.getCurrentBundle(); + Long value = bundle != null ? bundle.getBundleId() : null; // fix with a patched version of weld ProxyMethodHandler // if (value == null) { // return FrameworkUtil.getBundle(ProxyMethodHandler.currentCaller.get()).getBundleId(); @@ -88,10 +89,9 @@ public T get(String id) { if (maybeBundle != null) { if (!maybeBundle.getSymbolicName() .equals("org.jboss.weld.osgi.weld-osgi")) { - WeldOSGiExtension.currentBundle. - set(maybeBundle.getBundleId()); + Bundle previousBundle = WeldOSGiExtension.setCurrentBundle(maybeBundle); maybeObject = get(null); - WeldOSGiExtension.currentBundle.remove(); + WeldOSGiExtension.setCurrentBundle(previousBundle); if (maybeObject != null) { return maybeObject; } diff --git a/environments/osgi/core/integration/src/main/java/org/jboss/weld/environment/osgi/impl/integration/Weld.java b/environments/osgi/core/integration/src/main/java/org/jboss/weld/environment/osgi/impl/integration/Weld.java index 44e5f957673..55711673d34 100644 --- a/environments/osgi/core/integration/src/main/java/org/jboss/weld/environment/osgi/impl/integration/Weld.java +++ b/environments/osgi/core/integration/src/main/java/org/jboss/weld/environment/osgi/impl/integration/Weld.java @@ -75,8 +75,7 @@ public boolean initialize() { ClassLoader old = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); // ------------- - boolean set = WeldOSGiExtension.currentBundle.get() != null; - WeldOSGiExtension.currentBundle.set(bundle.getBundleId()); + Bundle previousBundle = WeldOSGiExtension.setCurrentBundle(bundle); try { bootstrap = new WeldBootstrap(); BundleDeployment deployment = createDeployment(bootstrap); @@ -107,9 +106,7 @@ public boolean initialize() { t); t.printStackTrace(); } finally { - if (!set) { - WeldOSGiExtension.currentBundle.remove(); - } + WeldOSGiExtension.setCurrentBundle(previousBundle); Thread.currentThread().setContextClassLoader(old); } return started;