KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > container > mdb > MDBMessageListenerEndPoint


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: MDBMessageListenerEndPoint.java 1132 2006-09-29 13:56:41Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.container.mdb;
27
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30
31 import javax.jms.Message JavaDoc;
32 import javax.jms.MessageListener JavaDoc;
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 /**
39  * This message listener end point is used for MDB message which use JMS as
40  * message listener interface.
41  * @author Florent Benoit
42  */

43 public class MDBMessageListenerEndPoint extends MDBMessageEndPoint implements MessageListener JavaDoc {
44
45     /**
46      * Logger.
47      */

48     private static JLog logger = JLogFactory.getLog(MDBMessageListenerEndPoint.class);
49
50     /**
51      * Use reflection or not ?
52      */

53     private boolean useReflection = false;
54
55     /**
56      * onMessage() method.
57      */

58     private Method JavaDoc onMessageMethod = null;
59
60     /**
61      * Constructor : Build an endpoint with a reference to the message end point
62      * factory.
63      * @param mdbMessageEndPointFactory the message end point factory.
64      * @param easyBeansMDB the message driven bean object that is wrapped.
65      */

66     public MDBMessageListenerEndPoint(final MDBMessageEndPointFactory mdbMessageEndPointFactory,
67             final EasyBeansMDB easyBeansMDB) {
68         super(mdbMessageEndPointFactory, easyBeansMDB);
69         // if instance is not implementing the interface, use reflection for message delivery.
70
if (!(easyBeansMDB instanceof MessageListener JavaDoc)) {
71             logger.debug("Use reflection for message delivery as instance {0} is not implementing the interface", easyBeansMDB);
72             useReflection = true;
73             // get method
74
try {
75                 onMessageMethod = easyBeansMDB.getClass().getMethod("onMessage", new Class JavaDoc[] {Message JavaDoc.class});
76             } catch (SecurityException JavaDoc e) {
77                 throw new IllegalArgumentException JavaDoc("Cannot get the onMessage method on instance '" + easyBeansMDB + "'.", e);
78             } catch (NoSuchMethodException JavaDoc e) {
79                 logger.error("No onMessage method found on bean named {0}", easyBeansMDB);
80                 throw new IllegalArgumentException JavaDoc("Cannot get the onMessage method on instance '" + easyBeansMDB + "'.", e);
81             }
82         }
83     }
84
85     /**
86      * Passes a message to the listener.
87      * @param message - the message passed to the listener
88      */

89     public void onMessage(final Message JavaDoc message) {
90         // set classloader to EJB classloader
91
ClassLoader JavaDoc oldCL = Thread.currentThread().getContextClassLoader();
92         Thread.currentThread().setContextClassLoader(getEasyBeansFactory().getContainer().getClassLoader());
93         try {
94             if (!useReflection) {
95                 ((MessageListener JavaDoc) getEasyBeansMDB()).onMessage(message);
96             } else {
97                 // invoke by using reflection
98
try {
99                     onMessageMethod.invoke(getEasyBeansMDB(), message);
100                 } catch (IllegalArgumentException JavaDoc e) {
101                     throw new IllegalStateException JavaDoc("Cannot deliver the message", e);
102                 } catch (IllegalAccessException JavaDoc e) {
103                     throw new IllegalStateException JavaDoc("Cannot deliver the message", e);
104                 } catch (InvocationTargetException JavaDoc e) {
105                     logger.error("Cannot deliver the message", e.getTargetException());
106                     throw new IllegalStateException JavaDoc("Cannot deliver the message", e.getTargetException());
107                 }
108             }
109         } finally {
110             Thread.currentThread().setContextClassLoader(oldCL);
111         }
112     }
113
114 }
115
Popular Tags