001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.ejb.message;
031:
032: import com.caucho.ejb.AbstractContext;
033: import com.caucho.ejb.AbstractServer;
034: import com.caucho.ejb.xa.TransactionContext;
035: import com.caucho.transaction.*;
036: import com.caucho.util.L10N;
037:
038: import javax.ejb.*;
039: import javax.transaction.*;
040: import javax.transaction.xa.*;
041: import java.util.logging.Logger;
042:
043: /**
044: * Server container for a message bean.
045: */
046: public class MessageDrivenContextImpl extends AbstractContext implements
047: MessageDrivenContext {
048: protected static final L10N L = new L10N(
049: MessageDrivenContextImpl.class);
050: protected static final Logger log = Logger
051: .getLogger(MessageDrivenContextImpl.class.getName());
052:
053: private MessageServer _server;
054: private UserTransaction _ut;
055: private boolean _isRollbackOnly;
056:
057: MessageDrivenContextImpl(MessageServer server, UserTransaction ut) {
058: _server = server;
059: _ut = ut;
060: }
061:
062: public AbstractServer getServer() {
063: return _server;
064: }
065:
066: public boolean isCMT() {
067: return getServer().isContainerTransaction();
068: }
069:
070: /**
071: * Returns the EJBHome stub for the container.
072: */
073: public EJBHome getEJBHome() {
074: throw new IllegalStateException(L
075: .l("Message-driven beans may not use getEJBHome()"));
076: }
077:
078: /**
079: * Returns the current UserTransaction. Only Session beans with
080: * bean-managed transactions may use this.
081: */
082: public UserTransaction getUserTransaction()
083: throws IllegalStateException {
084: if (isCMT())
085: throw new IllegalStateException(
086: L
087: .l("Container-managed message-driven beans may not use getUserTransaction()"));
088:
089: return _ut;
090: }
091:
092: /**
093: * Forces a rollback of the current transaction.
094: */
095: void clearRollbackOnly() {
096: _isRollbackOnly = false;
097: }
098:
099: /**
100: * Forces a rollback of the current transaction.
101: */
102: public void setRollbackOnly() throws IllegalStateException {
103: if (!isCMT())
104: throw new IllegalStateException(
105: "setRollbackOnly may not be called from a bean-managed transaction");
106:
107: try {
108: TransactionManagerImpl tm = TransactionManagerImpl
109: .getLocal();
110:
111: Transaction trans = tm.getTransaction();
112:
113: if (trans != null)
114: trans.setRollbackOnly();
115: else
116: throw new IllegalStateException(
117: L
118: .l("setRollbackOnly called with no active transaction"));
119: } catch (RuntimeException e) {
120: throw e;
121: } catch (Exception e) {
122: throw new EJBException(e);
123: }
124: }
125:
126: /**
127: * Forces a rollback of the current transaction.
128: */
129: public boolean getRollbackOnly() throws IllegalStateException {
130: if (!isCMT())
131: throw new IllegalStateException(
132: "getRollbackOnly may not be called from a bean-managed transaction");
133:
134: try {
135: TransactionManagerImpl tm = TransactionManagerImpl
136: .getLocal();
137:
138: Transaction trans = tm.getTransaction();
139:
140: if (trans != null)
141: return trans.getStatus() == Status.STATUS_MARKED_ROLLBACK;
142: else
143: throw new IllegalStateException(
144: "getRollbackOnly requires a valid container-managed transaction");
145: } catch (RuntimeException e) {
146: throw e;
147: } catch (Exception e) {
148: throw new EJBException(e);
149: }
150: }
151: }
|