001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: MDBListenerMethodAccess.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.common.ejbs.mdb.containermanaged.access;
025:
026: import static org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.CallbackType.ON_MESSAGE;
027: import static org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.ENTERPRISE_BEAN;
028: import static org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.ENTITY_MANAGER;
029: import static org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.ENTITY_MANAGER_FACTORY;
030: import static org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.RESOURCE_MANAGER;
031: import static org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.TIMER;
032: import static org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.USER_TRANSACTION;
033: import static org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.isEqual;
034: import static org.ow2.easybeans.tests.common.helper.ContextHelper.checkResource;
035:
036: import javax.annotation.Resource;
037: import javax.ejb.ActivationConfigProperty;
038: import javax.ejb.EJB;
039: import javax.ejb.MessageDriven;
040: import javax.ejb.MessageDrivenContext;
041: import javax.ejb.TimerService;
042: import javax.jms.JMSException;
043: import javax.jms.Message;
044: import javax.jms.MessageListener;
045: import javax.persistence.EntityManager;
046: import javax.persistence.EntityManagerFactory;
047: import javax.persistence.PersistenceContext;
048: import javax.persistence.PersistenceUnit;
049: import javax.sql.DataSource;
050: import javax.transaction.UserTransaction;
051:
052: import org.ow2.easybeans.tests.common.ejbs.base.ItfOneMethod01;
053: import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.callbacklogger.BaseInsertOperation;
054: import org.ow2.easybeans.tests.common.jms.JMSManager;
055: import org.ow2.easybeans.tests.common.jms.MessageProperty;
056: import org.ow2.easybeans.tests.common.resources.EMFactoryTester;
057: import org.ow2.easybeans.tests.common.resources.EntityManagerTester;
058: import org.ow2.util.log.Log;
059: import org.ow2.util.log.LogFactory;
060:
061: /**
062: * This bean test operations allowed in message listener methods.
063: * @author Eduardo Studzinski Estima de Castro
064: * @author Gisele Pinheiro Souza
065: */
066: @MessageDriven(messageListenerInterface=MessageListener.class,activationConfig={@ActivationConfigProperty(propertyName="destinationType",propertyValue="javax.jms.Queue"),@ActivationConfigProperty(propertyName="destination",propertyValue=JMSManager.DEFAULT_QUEUE),@ActivationConfigProperty(propertyName="messageSelector",propertyValue="TYPE ='org.ow2.easybeans.tests.common.ejbs.mdb.containermanaged.access.MDBListenerMethodAccess'")})
067: @EJB(name="ejb/bean01",beanInterface=ItfOneMethod01.class,beanName="EJBInjectionBean")
068: @Resource(name="jdbc/ds01",type=javax.sql.DataSource.class,mappedName="jdbc_1")
069: @PersistenceUnit(name="persistence/pu01")
070: @PersistenceContext(name="persistence/pctx01")
071: public class MDBListenerMethodAccess extends BaseInsertOperation {
072:
073: /**
074: * Log helper.
075: */
076: private Log logger = LogFactory
077: .getLog(MDBListenerMethodAccess.class);
078:
079: /**
080: * Message type.
081: */
082: public static final String MESSAGE_TYPE = "org.ow2.easybeans.tests.common.ejbs."
083: + "mdb.containermanaged.access.MDBListenerMethodAccess";
084:
085: /**
086: * ID.
087: */
088: private static final long serialVersionUID = 4108218174206348937L;
089:
090: /**
091: * Context.
092: */
093: @Resource
094: private MessageDrivenContext ctx;
095:
096: /**
097: * Timer.
098: */
099: @Resource
100: private TimerService timer;
101:
102: /**
103: * UserTransaction must not be injected.
104: */
105: @Resource
106: private UserTransaction utx;
107:
108: /**
109: * Verifies if the operations allowed for Message Driven listener methods are working properly.
110: * @param message msg
111: */
112: public void onMessage(final Message message) {
113: String op = null;
114:
115: try {
116: op = message.getStringProperty(MessageProperty.OPERATION
117: .toString());
118: } catch (JMSException e) {
119: logger.debug("Error getting operation type: {0}", e);
120: }
121:
122: if (isEqual(RESOURCE_MANAGER, op)) {
123: DataSource ds = (DataSource) ctx.lookup("jdbc/ds01");
124: checkResource(ds);
125: log(MDBListenerMethodAccess.class, ON_MESSAGE,
126: MDBListenerMethodAccess.class, RESOURCE_MANAGER);
127:
128: } else if (isEqual(ENTERPRISE_BEAN, op)) {
129: ItfOneMethod01 bean = (ItfOneMethod01) ctx
130: .lookup("ejb/bean01");
131: bean.getBool();
132: log(MDBListenerMethodAccess.class, ON_MESSAGE,
133: MDBListenerMethodAccess.class, ENTERPRISE_BEAN);
134:
135: } else if (isEqual(ENTITY_MANAGER_FACTORY, op)) {
136: EntityManagerFactory emf = (EntityManagerFactory) ctx
137: .lookup("persistence/pu01");
138: try {
139: EMFactoryTester.checkInstance(emf, "tmpTable"
140: + this .hashCode());
141: log(MDBListenerMethodAccess.class, ON_MESSAGE,
142: MDBListenerMethodAccess.class,
143: ENTITY_MANAGER_FACTORY);
144: } catch (Exception e) {
145: logger.debug("Error in EntityManagerFactory use: {0}",
146: e);
147: e.printStackTrace();
148: }
149:
150: } else if (isEqual(ENTITY_MANAGER, op)) {
151: EntityManager em = (EntityManager) ctx
152: .lookup("persistence/pctx01");
153: try {
154: EntityManagerTester.checkInstance(em, "tmpTable"
155: + this .hashCode());
156: log(MDBListenerMethodAccess.class, ON_MESSAGE,
157: MDBListenerMethodAccess.class, ENTITY_MANAGER);
158: } catch (Exception e) {
159: logger.debug("Error in EntityManagerFactory use: {0}",
160: e);
161: e.printStackTrace();
162: }
163:
164: } else if (isEqual(TIMER, op)) {
165: timer.hashCode();
166: log(MDBListenerMethodAccess.class, ON_MESSAGE,
167: MDBListenerMethodAccess.class, TIMER);
168:
169: } else if (isEqual(USER_TRANSACTION, op)) {
170: if (utx == null) {
171: log(MDBListenerMethodAccess.class, ON_MESSAGE,
172: MDBListenerMethodAccess.class, USER_TRANSACTION);
173: }
174: }
175:
176: }
177:
178: }
|