Skip to content

Commit

Permalink
[WELD-1313] Avoir direct access to public static thread locals in wel…
Browse files Browse the repository at this point in the history
…d-osgi and use accessors
  • Loading branch information
gnodet authored and alesj committed Jan 30, 2013
1 parent 4e32a91 commit 6c07b76
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 74 deletions.
Expand Up @@ -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<Object> broadcastingCDIEvent =
(Event<Object>) context.getService(reference);
try {
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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<Object> instance =
(Instance<Object>) context.getService(reference);
try {
Expand All @@ -246,9 +245,8 @@ public void serviceChanged(ServiceEvent event) {
catch(Throwable t) {
//ignore
}
if (!set) {
WeldOSGiExtension.currentBundle.remove();
}
WeldOSGiExtension.setCurrentBundle(previousBundle);
WeldOSGiExtension.setCurrentContext(previousContext);
}
}
}
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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<Object> instance) {
Expand Down
Expand Up @@ -104,8 +104,8 @@ public class WeldOSGiExtension implements Extension {
WeldOSGiExtension.class);
private static boolean autoRunInHybridMode = true;
// hack for weld integration
public static ThreadLocal<Long> currentBundle = new ThreadLocal<Long>();
public static ThreadLocal<BundleContext> currentContext = new ThreadLocal<BundleContext>();
private static ThreadLocal<BundleContext> currentContext = new ThreadLocal<BundleContext>();
private static ThreadLocal<Bundle> currentBundle = new ThreadLocal<Bundle>();
private HashMap<Type, Set<InjectionPoint>> servicesToBeInjected =
new HashMap<Type, Set<InjectionPoint>>();
private HashMap<Type, Set<InjectionPoint>> serviceProducerToBeInjected =
Expand All @@ -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");
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -313,8 +340,8 @@ public void removeListeners() {

void afterDeployment(@Observes AfterDeploymentValidation event) {
if (listener != null) {
currentContext.remove();
currentBundle.remove();
setCurrentContext(null);
setCurrentBundle(null);
}
}

Expand Down Expand Up @@ -385,7 +412,7 @@ private void addService(AfterBeanDiscovery event,
for (Iterator<InjectionPoint> 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);
Expand All @@ -398,7 +425,7 @@ private void addServiceProducer(AfterBeanDiscovery event,
for (Iterator<InjectionPoint> 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);
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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) {
Expand Down
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}

}
}
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -47,7 +47,8 @@ public BundleSingleton(Class<? extends T> 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();
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 6c07b76

Please sign in to comment.