001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.testbean.bean;
023:
024: import java.rmi.*;
025: import javax.ejb.*;
026: import javax.naming.InitialContext;
027: import javax.naming.Context;
028: import org.jboss.test.testbean.interfaces.BMTStateful;
029: import javax.transaction.UserTransaction;
030: import javax.transaction.Status;
031:
032: public class BMTStatelessBean implements SessionBean {
033: org.apache.log4j.Category log = org.apache.log4j.Category
034: .getInstance(getClass());
035:
036: private SessionContext sessionContext;
037:
038: public void ejbCreate() throws RemoteException, CreateException {
039: log.debug("BMTStatelessBean.ejbCreate() called");
040: }
041:
042: public void ejbActivate() throws RemoteException {
043: log.debug("BMTStatelessBean.ejbActivate() called");
044: }
045:
046: public void ejbPassivate() throws RemoteException {
047: log.debug("BMTStatelessBean.ejbPassivate() called");
048: }
049:
050: public void ejbRemove() throws RemoteException {
051: log.debug("BMTStatelessBean.ejbRemove() called");
052: }
053:
054: public void setSessionContext(SessionContext context)
055: throws RemoteException {
056: sessionContext = context;
057: }
058:
059: public String txExists() throws RemoteException {
060: String result = "";
061: try {
062: UserTransaction ut1 = sessionContext.getUserTransaction();
063: result += "Got UserTransaction via sessionContext.getUserTransaction()\n";
064:
065: UserTransaction ut2 = (UserTransaction) new InitialContext()
066: .lookup("java:comp/UserTransaction");
067: result += "Got UserTransaction via lookup(java:comp/UserTransaction)\n";
068: return result;
069: } catch (Exception e) {
070: throw new RemoteException(e.getMessage());
071: }
072: }
073:
074: public String txCommit() throws RemoteException {
075: try {
076: UserTransaction tx = sessionContext.getUserTransaction();
077:
078: String result = "Got transaction : "
079: + statusName(tx.getStatus()) + "\n";
080: tx.begin();
081: result += "tx.begin(): " + statusName(tx.getStatus())
082: + "\n";
083: tx.commit();
084: result += "tx.commit(): " + statusName(tx.getStatus())
085: + "\n";
086:
087: return result;
088:
089: } catch (Exception e) {
090: log.debug("failed", e);
091: throw new RemoteException(e.getMessage());
092: }
093:
094: }
095:
096: public String txRollback() throws RemoteException {
097: try {
098: UserTransaction tx = sessionContext.getUserTransaction();
099:
100: String result = "Got transaction : "
101: + statusName(tx.getStatus()) + "\n";
102: tx.begin();
103: result += "tx.begin(): " + statusName(tx.getStatus())
104: + "\n";
105: tx.rollback();
106: result += "tx.rollback(): " + statusName(tx.getStatus())
107: + "\n";
108:
109: return result;
110:
111: } catch (Exception e) {
112: throw new RemoteException(e.getMessage());
113: }
114: }
115:
116: // this should not be allowed by the container
117: public String txBegin() throws RemoteException {
118: try {
119: UserTransaction tx = sessionContext.getUserTransaction();
120:
121: tx.begin();
122: return "status: " + statusName(tx.getStatus());
123: } catch (Exception e) {
124: throw new RemoteException(e.getMessage());
125: }
126:
127: }
128:
129: private String statusName(int s) {
130: switch (s) {
131: case Status.STATUS_ACTIVE:
132: return "STATUS_ACTIVE";
133: case Status.STATUS_COMMITTED:
134: return "STATUS_COMMITED";
135: case Status.STATUS_COMMITTING:
136: return "STATUS_COMMITTING";
137: case Status.STATUS_MARKED_ROLLBACK:
138: return "STATUS_MARKED_ROLLBACK";
139: case Status.STATUS_NO_TRANSACTION:
140: return "STATUS_NO_TRANSACTION";
141: case Status.STATUS_PREPARED:
142: return "STATUS_PREPARED";
143: case Status.STATUS_PREPARING:
144: return "STATUS_PREPARING";
145: case Status.STATUS_ROLLEDBACK:
146: return "STATUS_ROLLEDBACK";
147: case Status.STATUS_ROLLING_BACK:
148: return "STATUS_ROLLING_BACK";
149: case Status.STATUS_UNKNOWN:
150: return "STATUS_UNKNOWN";
151: }
152: return "REALLY_UNKNOWN";
153: }
154:
155: }
|