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