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.perf.ejb;
023:
024: import java.rmi.RemoteException;
025: import javax.ejb.CreateException;
026: import javax.ejb.EJBException;
027: import javax.ejb.SessionBean;
028: import javax.ejb.SessionContext;
029: import javax.naming.Context;
030: import javax.naming.InitialContext;
031: import javax.naming.NamingException;
032: import javax.transaction.Transaction;
033: import javax.transaction.TransactionManager;
034:
035: import org.jboss.test.perf.interfaces.TxSession;
036:
037: public class TxSessionBean implements SessionBean {
038: private SessionContext sessionContext;
039: private InitialContext iniCtx;
040:
041: public void ejbCreate() throws CreateException {
042: }
043:
044: public void ejbActivate() {
045: }
046:
047: public void ejbPassivate() {
048: }
049:
050: public void ejbRemove() {
051: }
052:
053: public void setSessionContext(SessionContext context) {
054: sessionContext = context;
055: try {
056: iniCtx = new InitialContext();
057: } catch (NamingException e) {
058: throw new EJBException(e);
059: }
060: }
061:
062: /*
063: * This method is defined with "Required"
064: */
065: public String txRequired() {
066: Transaction tx = getDaTransaction();
067: if (tx == null)
068: throw new EJBException("Required sees no transaction");
069: else
070: return ("required sees a transaction " + tx.hashCode());
071: }
072:
073: /*
074: * This method is defined with "Requires_new"
075: */
076: public String txRequiresNew() {
077: Object tx = getDaTransaction();
078: if (tx == null)
079: throw new EJBException("RequiresNew sees no transaction");
080: else
081: return ("requiresNew sees a transaction " + tx.hashCode());
082: }
083:
084: /*
085: * testSupports is defined with Supports
086: */
087: public String txSupports() {
088: Object tx = getDaTransaction();
089: if (tx == null)
090: return "supports sees no transaction";
091: else
092: return "supports sees a transaction " + tx.hashCode();
093: }
094:
095: /*
096: * This method is defined with "Mandatory"
097: */
098: public String txMandatory() {
099: Object tx = getDaTransaction();
100: if (tx == null)
101: throw new EJBException("Mandatory sees no transaction");
102: else
103: return ("mandatory sees a transaction " + tx.hashCode());
104: }
105:
106: /*
107: * This method is defined with "Never"
108: */
109: public String txNever() {
110: Object tx = getDaTransaction();
111: if (tx == null)
112: return "never sees no transaction";
113: else
114: throw new EJBException("txNever sees a transaction");
115: }
116:
117: /*
118: * This method is defined with "TxNotSupported"
119: */
120: public String txNotSupported() {
121: Object tx = getDaTransaction();
122: if (tx == null)
123: return "notSupported sees no transaction";
124: else
125: throw new EJBException("txNotSupported sees a transaction");
126: }
127:
128: /*
129: * This method is defined with "Required" and it passes it to a Supports Tx
130: */
131: public String requiredToSupports() throws RemoteException {
132: String message;
133: Object tx = getDaTransaction();
134: if (tx == null)
135: throw new EJBException("Required doesn't see a transaction");
136: else
137: message = "Required sees a transaction " + tx.hashCode()
138: + " Supports should see the same ";
139:
140: message = message
141: + ((TxSession) sessionContext.getEJBObject())
142: .txSupports();
143:
144: // And after invocation we should have the same transaction
145: tx = getDaTransaction();
146: if (tx == null)
147: throw new EJBException(
148: "Required doesn't see a transaction COMING BACK");
149: else
150: return message
151: + " on coming back Required sees a transaction "
152: + tx.hashCode();
153:
154: }
155:
156: /*
157: * This method is defined with "Required" and it passes it to a Mandatory Tx
158: */
159: public String requiredToMandatory() throws RemoteException {
160: String message;
161: Object tx = getDaTransaction();
162: if (tx == null)
163: throw new EJBException("Required doesn't see a transaction");
164: else
165: message = "Required sees a transaction " + tx.hashCode()
166: + " NotSupported should see the same ";
167:
168: message = message
169: + ((TxSession) sessionContext.getEJBObject())
170: .txMandatory();
171:
172: // And after invocation we should have the same transaction
173: tx = getDaTransaction();
174: if (tx == null)
175: throw new EJBException(
176: "Required doesn't see a transaction COMING BACK");
177: else
178: return message
179: + " on coming back Required sees a transaction "
180: + tx.hashCode();
181: }
182:
183: public String requiredToRequiresNew() throws RemoteException {
184: String message;
185: Object tx = getDaTransaction();
186: if (tx == null)
187: throw new EJBException("Required doesn't see a transaction");
188: else
189: message = "Required sees a transaction " + tx.hashCode()
190: + " Requires new should see a new transaction ";
191:
192: message = message
193: + ((TxSession) sessionContext.getEJBObject())
194: .txRequiresNew();
195:
196: // And after invocation we should have the same transaction
197: tx = getDaTransaction();
198: if (tx == null)
199: throw new EJBException(
200: "Required doesn't see a transaction COMING BACK");
201: else
202: return message
203: + " on coming back Required sees a transaction "
204: + tx.hashCode();
205: }
206:
207: private Transaction getDaTransaction() {
208: Transaction t = null;
209: try {
210: TransactionManager tm = (TransactionManager) iniCtx
211: .lookup("java:/TransactionManager");
212: t = tm.getTransaction();
213: } catch (Exception e) {
214: }
215: return t;
216: }
217:
218: }
|