Skip to content

Commit

Permalink
Proof of concept: Set TCCL before calling observer methods
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin-confino committed Apr 26, 2017
1 parent 492147a commit cfaf75f
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion impl/src/main/java/org/jboss/weld/event/ObserverMethodImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -296,7 +298,15 @@ protected void sendEvent(T event, Object receiver, CreationalContext<?> creation
preNotify(event, receiver);
// As we are working with the contextual instance, we may not have the
// actual object, but a container proxy (e.g. EJB)
notificationStrategy.invoke(receiver, observerMethod, event, beanManager, creationalContext);
ClassLoader oldCL = null;
try {
ClassLoader newCL = receiver.getClass().getClassLoader();
oldCL = AccessController.doPrivileged( new GetAndSetLoader(newCL));
notificationStrategy.invoke(receiver, observerMethod, event, beanManager, creationalContext);
}
finally {
AccessController.doPrivileged( new GetAndSetLoader(oldCL));
}
} finally {
postNotify(event, receiver);
}
Expand Down Expand Up @@ -361,4 +371,22 @@ public int hashCode() {
public boolean isEventMetadataRequired() {
return eventMetadataRequired;
}


static class GetAndSetLoader implements PrivilegedAction<ClassLoader> {
private ClassLoader newLoader;
GetAndSetLoader(ClassLoader newLoader) {
this.newLoader = newLoader;
}

@Override
public ClassLoader run() {
Thread t = Thread.currentThread();
ClassLoader origLoader = t.getContextClassLoader();
t.setContextClassLoader(newLoader);
return origLoader;
}
}


}

0 comments on commit cfaf75f

Please sign in to comment.