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: MDBBeanManagedContext.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.common.ejbs.mdb.beanmanaged.context;
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.GET_CALLER_PRINCIPAL;
028: import static org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.GET_ROLLBACK_ONLY;
029: import static org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.SET_ROLLBACK_ONLY;
030: import static org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.TIMER;
031: import static org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.USER_TRANSACTION;
032: import static org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.isEqual;
033: import static org.ow2.easybeans.tests.common.resources.TimerServiceTester.checkInstance;
034:
035: import javax.annotation.Resource;
036: import javax.ejb.ActivationConfigProperty;
037: import javax.ejb.MessageDriven;
038: import javax.ejb.MessageDrivenContext;
039: import javax.ejb.TransactionManagement;
040: import javax.ejb.TransactionManagementType;
041: import javax.jms.JMSException;
042: import javax.jms.Message;
043: import javax.jms.MessageListener;
044:
045: import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.callbacklogger.BaseInsertOperation;
046: import org.ow2.easybeans.tests.common.jms.JMSManager;
047: import org.ow2.easybeans.tests.common.jms.MessageProperty;
048: import org.ow2.util.log.Log;
049: import org.ow2.util.log.LogFactory;
050:
051: /**
052: * This bean tests the MessageDrivenContext.
053: * @author Eduardo Studzinski Estima de Castro
054: * @author Gisele Pinheiro Souza
055: */
056: @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.beanmanaged.context.MDBBeanManagedContext'")})
057: @TransactionManagement(TransactionManagementType.BEAN)
058: public class MDBBeanManagedContext extends BaseInsertOperation {
059:
060: /**
061: * Message type.
062: */
063: public static final String MESSAGE_TYPE = "org.ow2.easybeans.tests.common.ejbs.mdb.beanmanaged.context.MDBBeanManagedContext";
064:
065: /**
066: * Context.
067: */
068: @Resource
069: private MessageDrivenContext ctx;
070:
071: /**
072: * Logger.
073: */
074: private Log logger = LogFactory.getLog(MDBBeanManagedContext.class);
075:
076: /**
077: * Verifies the MessageDrivenContext methods.
078: * @param msg message
079: */
080: public void onMessage(final Message msg) {
081: String op = null;
082:
083: try {
084: op = msg.getStringProperty(MessageProperty.OPERATION
085: .toString());
086: } catch (JMSException e) {
087: logger.debug("Error getting operation type: {0}", e);
088: }
089:
090: if (ctx == null) {
091: logger.debug("The SessionContext reference is null.");
092: } else if (isEqual(GET_CALLER_PRINCIPAL, op)) {
093:
094: // TODO: test case
095:
096: } else if (isEqual(GET_ROLLBACK_ONLY, op)) {
097: try {
098: ctx.getRollbackOnly();
099: } catch (IllegalStateException e) {
100: log(MDBBeanManagedContext.class, ON_MESSAGE,
101: MDBBeanManagedContext.class, GET_ROLLBACK_ONLY);
102: } catch (Exception e) {
103: logger.debug("Unexpected error.", e);
104: }
105:
106: } else if (isEqual(SET_ROLLBACK_ONLY, op)) {
107: try {
108: ctx.setRollbackOnly();
109: } catch (IllegalStateException e) {
110: log(MDBBeanManagedContext.class, ON_MESSAGE,
111: MDBBeanManagedContext.class, SET_ROLLBACK_ONLY);
112: }
113:
114: } else if (isEqual(TIMER, op)) {
115: try {
116: checkInstance(ctx.getTimerService());
117: log(MDBBeanManagedContext.class, ON_MESSAGE,
118: MDBBeanManagedContext.class, TIMER);
119: } catch (Exception e) {
120: logger.debug("Unexpected error.", e);
121: }
122:
123: } else if (isEqual(USER_TRANSACTION, op)) {
124: try {
125: checkInstance(ctx.getTimerService());
126: log(MDBBeanManagedContext.class, ON_MESSAGE,
127: MDBBeanManagedContext.class, USER_TRANSACTION);
128: } catch (Exception e) {
129: logger.debug("Unexpected error.", e);
130: }
131: }
132: }
133: }
|