1 25 26 package org.objectweb.easybeans.container.mdb; 27 28 import java.lang.reflect.InvocationTargetException ; 29 import java.lang.reflect.Method ; 30 31 import javax.jms.Message ; 32 import javax.jms.MessageListener ; 33 34 import org.objectweb.easybeans.api.bean.EasyBeansMDB; 35 import org.objectweb.easybeans.log.JLog; 36 import org.objectweb.easybeans.log.JLogFactory; 37 38 43 public class MDBMessageListenerEndPoint extends MDBMessageEndPoint implements MessageListener { 44 45 48 private static JLog logger = JLogFactory.getLog(MDBMessageListenerEndPoint.class); 49 50 53 private boolean useReflection = false; 54 55 58 private Method onMessageMethod = null; 59 60 66 public MDBMessageListenerEndPoint(final MDBMessageEndPointFactory mdbMessageEndPointFactory, 67 final EasyBeansMDB easyBeansMDB) { 68 super(mdbMessageEndPointFactory, easyBeansMDB); 69 if (!(easyBeansMDB instanceof MessageListener )) { 71 logger.debug("Use reflection for message delivery as instance {0} is not implementing the interface", easyBeansMDB); 72 useReflection = true; 73 try { 75 onMessageMethod = easyBeansMDB.getClass().getMethod("onMessage", new Class [] {Message .class}); 76 } catch (SecurityException e) { 77 throw new IllegalArgumentException ("Cannot get the onMessage method on instance '" + easyBeansMDB + "'.", e); 78 } catch (NoSuchMethodException e) { 79 logger.error("No onMessage method found on bean named {0}", easyBeansMDB); 80 throw new IllegalArgumentException ("Cannot get the onMessage method on instance '" + easyBeansMDB + "'.", e); 81 } 82 } 83 } 84 85 89 public void onMessage(final Message message) { 90 ClassLoader oldCL = Thread.currentThread().getContextClassLoader(); 92 Thread.currentThread().setContextClassLoader(getEasyBeansFactory().getContainer().getClassLoader()); 93 try { 94 if (!useReflection) { 95 ((MessageListener ) getEasyBeansMDB()).onMessage(message); 96 } else { 97 try { 99 onMessageMethod.invoke(getEasyBeansMDB(), message); 100 } catch (IllegalArgumentException e) { 101 throw new IllegalStateException ("Cannot deliver the message", e); 102 } catch (IllegalAccessException e) { 103 throw new IllegalStateException ("Cannot deliver the message", e); 104 } catch (InvocationTargetException e) { 105 logger.error("Cannot deliver the message", e.getTargetException()); 106 throw new IllegalStateException ("Cannot deliver the message", e.getTargetException()); 107 } 108 } 109 } finally { 110 Thread.currentThread().setContextClassLoader(oldCL); 111 } 112 } 113 114 } 115 | Popular Tags |