ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(getEasyBeansFactory().getContainer().getClassLoader());
// Do some stuff on ID only if object is there, which means that audit
// component is enabled and that this is an MDB JMS
ICurrentInvocationID currentInvocationID = getMDBMessageEndPointFactory().getCurrentInvocationID();
if (currentInvocationID != null && MessageListener.class.equals(this.listenerInterface)) {
// Check if there is an ID in the message
String auditIDString = null;
try {
auditIDString = ((Message) args[0]).getStringProperty(IAuditID.class.getName());
} catch (JMSException e) {
logger.error("Unable to get the ID in the JMS message", e);
}
if (auditIDString != null) {
// Get the ID stored in this message
AuditIDImpl callerID = new AuditIDImpl(auditIDString);
// Add a new call
AuditIDImpl newID = new AuditIDImpl();
newID.generate();
newID.setParentID(callerID.getLocalID());
currentInvocationID.setAuditID(newID);
} else {
// New invocation is starting there
currentInvocationID.init(null);
}
}
// Create the bean invocation begin event.
String methodEventProviderId = getMDBMessageEndPointFactory().getJ2EEManagedObjectId() + "/"
+ J2EEManagedObjectNamingHelper.getMethodSignature(method);
EZBEventBeanInvocation event = getMDBMessageEndPointFactory().getInvocationEventBegin(methodEventProviderId, args);
long number = event.getInvocationNumber();
this.eventDispatcher.dispatch(event);
EZBEventBeanInvocation endEvent = null;
Method mdbMethod = null;
// Bean is implementing the interface, keep the given method
if (this.listenerInterface.isInstance(getEasyBeansMDB())) {
mdbMethod = method;
} else {
// search the method with the same signature on the bean
try {
mdbMethod = getEasyBeansMDB().getClass().getMethod(method.getName(), method.getParameterTypes());
} catch (SecurityException e) {
throw new IllegalStateException("Cannot deliver the message", e);
} catch (NoSuchMethodException e) {
throw new IllegalStateException("Cannot deliver the message", e);
}
}
// invoke by using reflection
Object result = null;
try {
result = mdbMethod.invoke(getEasyBeansMDB(), args);
endEvent = new EventBeanInvocationEnd(methodEventProviderId, number, null);
} catch (IllegalArgumentException e) {
logger.error("Cannot deliver the message", e);
endEvent = new EventBeanInvocationError(methodEventProviderId, number, e);
throw new IllegalStateException("Cannot deliver the message", e);
} catch (IllegalAccessException e) {
logger.error("Cannot deliver the message", e);
endEvent = new EventBeanInvocationError(methodEventProviderId, number, e);
throw new IllegalStateException("Cannot deliver the message", e);
} catch (InvocationTargetException e) {
endEvent = new EventBeanInvocationError(methodEventProviderId, number, e);
logger.error("Cannot deliver the message", e.getTargetException());
throw new IllegalStateException("Cannot deliver the message", e.getTargetException());
} finally {
// Reset classloader
Thread.currentThread().setContextClassLoader(oldCL);
if (currentInvocationID != null) {
currentInvocationID.setAuditID(null);
}
if (endEvent != null) {
this.eventDispatcher.dispatch(endEvent);
}
}