KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > ejbs > mdb > containermanaged > access > MDBListenerMethodAccess


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: MDBListenerMethodAccess.java 1165 2006-10-18 13:44:16Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.ejbs.mdb.containermanaged.access;
26
27 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.CallbackType.ON_MESSAGE;
28 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.ENTERPRISE_BEAN;
29 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.ENTITY_MANAGER;
30 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.ENTITY_MANAGER_FACTORY;
31 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.RESOURCE_MANAGER;
32 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.TIMER;
33 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.USER_TRANSACTION;
34 import static org.objectweb.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.isEqual;
35 import static org.objectweb.easybeans.tests.common.helper.ContextHelper.checkResource;
36
37 import javax.annotation.Resource;
38 import javax.ejb.ActivationConfigProperty JavaDoc;
39 import javax.ejb.EJB JavaDoc;
40 import javax.ejb.MessageDriven JavaDoc;
41 import javax.ejb.MessageDrivenContext JavaDoc;
42 import javax.ejb.TimerService JavaDoc;
43 import javax.jms.JMSException JavaDoc;
44 import javax.jms.Message JavaDoc;
45 import javax.jms.MessageListener JavaDoc;
46 import javax.persistence.EntityManager;
47 import javax.persistence.EntityManagerFactory;
48 import javax.persistence.PersistenceContext;
49 import javax.persistence.PersistenceUnit;
50 import javax.sql.DataSource JavaDoc;
51 import javax.transaction.UserTransaction JavaDoc;
52
53 import org.objectweb.easybeans.log.JLog;
54 import org.objectweb.easybeans.log.JLogFactory;
55 import org.objectweb.easybeans.tests.common.ejbs.base.ItfOneMethod01;
56 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.callbacklogger.BaseInsertOperation;
57 import org.objectweb.easybeans.tests.common.jms.JMSManager;
58 import org.objectweb.easybeans.tests.common.jms.MessageProperty;
59 import org.objectweb.easybeans.tests.common.resources.EMFactoryTester;
60 import org.objectweb.easybeans.tests.common.resources.EntityManagerTester;
61
62 /**
63  * This bean test operations allowed in message listener methods.
64  * @author Eduardo Studzinski Estima de Castro
65  * @author Gisele Pinheiro Souza
66  */

67 @MessageDriven JavaDoc(messageListenerInterface = MessageListener JavaDoc.class, activationConfig = {
68         @ActivationConfigProperty JavaDoc(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
69         @ActivationConfigProperty JavaDoc(propertyName = "destination", propertyValue = JMSManager.DEFAULT_QUEUE),
70         @ActivationConfigProperty JavaDoc(propertyName = "messageSelector", propertyValue =
71             "TYPE ='org.objectweb.easybeans.tests.common.ejbs.mdb.containermanaged.access.MDBListenerMethodAccess'")
72         })
73 @EJB JavaDoc(name = "ejb/bean01", beanInterface = ItfOneMethod01.class, beanName = "EJBInjectionBean")
74 @Resource(name = "jdbc/ds01", type = javax.sql.DataSource JavaDoc.class, mappedName = "jdbc_1")
75 @PersistenceUnit(name = "persistence/pu01")
76 @PersistenceContext(name = "persistence/pctx01")
77 public class MDBListenerMethodAccess extends BaseInsertOperation {
78
79     /**
80      * Log helper.
81      */

82     private JLog logger = JLogFactory.getLog(MDBListenerMethodAccess.class);
83
84     /**
85      * Message type.
86      */

87     public static final String JavaDoc MESSAGE_TYPE = "org.objectweb.easybeans.tests.common.ejbs."
88             + "mdb.containermanaged.access.MDBListenerMethodAccess";
89
90     /**
91      * ID.
92      */

93     private static final long serialVersionUID = 4108218174206348937L;
94
95     /**
96      * Context.
97      */

98     @Resource
99     private MessageDrivenContext JavaDoc ctx;
100
101     /**
102      * Timer.
103      */

104     @Resource
105     private TimerService JavaDoc timer;
106
107     /**
108      * UserTransaction must not be injected.
109      */

110     @Resource
111     private UserTransaction JavaDoc utx;
112
113     /**
114      * Verifies if the operations allowed for Message Driven listener methods are working properly.
115      * @param message msg
116      */

117     public void onMessage(final Message JavaDoc message) {
118         String JavaDoc op = null;
119
120         try {
121             op = message.getStringProperty(MessageProperty.OPERATION.toString());
122         } catch (JMSException JavaDoc e) {
123             logger.debug("Error getting operation type: {0}", e);
124         }
125
126         if (isEqual(RESOURCE_MANAGER, op)) {
127             DataSource JavaDoc ds = (DataSource JavaDoc) ctx.lookup("jdbc/ds01");
128             checkResource(ds);
129             log(MDBListenerMethodAccess.class, ON_MESSAGE, MDBListenerMethodAccess.class, RESOURCE_MANAGER);
130
131         } else if (isEqual(ENTERPRISE_BEAN, op)) {
132             ItfOneMethod01 bean = (ItfOneMethod01) ctx.lookup("ejb/bean01");
133             bean.getBool();
134             log(MDBListenerMethodAccess.class, ON_MESSAGE, MDBListenerMethodAccess.class, ENTERPRISE_BEAN);
135
136         } else if (isEqual(ENTITY_MANAGER_FACTORY, op)) {
137             EntityManagerFactory emf = (EntityManagerFactory) ctx.lookup("persistence/pu01");
138             try {
139                 EMFactoryTester.checkInstance(emf, "tmpTable" + this.hashCode());
140                 log(MDBListenerMethodAccess.class, ON_MESSAGE, MDBListenerMethodAccess.class, ENTITY_MANAGER_FACTORY);
141             } catch (Exception JavaDoc e) {
142                 logger.debug("Error in EntityManagerFactory use: {0}", e);
143                 e.printStackTrace();
144             }
145
146         } else if (isEqual(ENTITY_MANAGER, op)) {
147             EntityManager em = (EntityManager) ctx.lookup("persistence/pctx01");
148             try {
149                 EntityManagerTester.checkInstance(em, "tmpTable" + this.hashCode());
150                 log(MDBListenerMethodAccess.class, ON_MESSAGE, MDBListenerMethodAccess.class, ENTITY_MANAGER);
151             } catch (Exception JavaDoc e) {
152                 logger.debug("Error in EntityManagerFactory use: {0}", e);
153                 e.printStackTrace();
154             }
155
156         } else if (isEqual(TIMER, op)) {
157             timer.hashCode();
158             log(MDBListenerMethodAccess.class, ON_MESSAGE, MDBListenerMethodAccess.class, TIMER);
159
160         } else if (isEqual(USER_TRANSACTION, op)){
161             if (utx == null){
162                 log(MDBListenerMethodAccess.class, ON_MESSAGE, MDBListenerMethodAccess.class, USER_TRANSACTION);
163             }
164         }
165
166     }
167
168 }
169
Popular Tags