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.TxSession;
029:
030: public class TxSessionBean implements SessionBean {
031: org.apache.log4j.Category log = org.apache.log4j.Category
032: .getInstance(getClass());
033: private SessionContext sessionContext;
034:
035: public void ejbCreate() throws RemoteException, CreateException {
036: log.debug("TxSessionBean.ejbCreate() called");
037: }
038:
039: public void ejbActivate() throws RemoteException {
040: log.debug("TxSessionBean.ejbActivate() called");
041: }
042:
043: public void ejbPassivate() throws RemoteException {
044: log.debug("TxSessionBean.ejbPassivate() called");
045: }
046:
047: public void ejbRemove() throws RemoteException {
048: log.debug("TxSessionBean.ejbRemove() called");
049: }
050:
051: public void setSessionContext(SessionContext context)
052: throws RemoteException {
053: sessionContext = context;
054: //Exception e = new Exception("in set Session context");
055: //log.debug("failed", e);
056: }
057:
058: /*
059: * This method is defined with "Required"
060: */
061: public String txRequired() {
062:
063: log.debug("TxSessionBean.txRequired() called");
064:
065: try {
066: Object tx = getDaTransaction();
067: if (tx == null)
068: throw new Error("Required sees no transaction");
069: else
070: return ("required sees a transaction " + tx.hashCode());
071: } catch (Exception e) {
072: log.debug("failed", e);
073: return e.getMessage();
074: }
075: }
076:
077: /*
078: * This method is defined with "Requires_new"
079: */
080: public String txRequiresNew() {
081:
082: log.debug("TxSessionBean.txRequiresNew() called");
083:
084: try {
085: Object tx = getDaTransaction();
086: if (tx == null)
087: throw new Error("RequiresNew sees no transaction");
088: else
089: return ("requiresNew sees a transaction " + tx
090: .hashCode());
091: } catch (Exception e) {
092: log.debug("failed", e);
093: return e.getMessage();
094: }
095: }
096:
097: /*
098: * testSupports is defined with Supports
099: */
100:
101: public String txSupports() {
102:
103: log.debug("TxSessionBean.txSupports() called");
104:
105: try {
106:
107: Object tx = getDaTransaction();
108:
109: if (tx == null)
110: return "supports sees no transaction";
111: else
112: return "supports sees a transaction " + tx.hashCode();
113: } catch (Exception e) {
114: log.debug("failed", e);
115: return e.getMessage();
116: }
117: }
118:
119: /*
120: * This method is defined with "Mandatory"
121: */
122: public String txMandatory() {
123:
124: log.debug("TxSessionBean.txMandatory() called");
125:
126: try {
127: Object tx = getDaTransaction();
128: if (tx == null)
129: throw new Error("Mandatory sees no transaction");
130: else
131: return ("mandatory sees a transaction " + tx.hashCode());
132: } catch (Exception e) {
133: log.debug("failed", e);
134: return e.getMessage();
135: }
136: }
137:
138: /*
139: * This method is defined with "Never"
140: */
141: public String txNever() {
142:
143: log.debug("TxSessionBean.txNever() called");
144:
145: try {
146: Object tx = getDaTransaction();
147: if (tx == null)
148: return "never sees no transaction";
149: else
150: throw new Error("txNever sees a transaction");
151: } catch (Exception e) {
152: log.debug("failed", e);
153: return e.getMessage();
154: }
155: }
156:
157: /*
158: * This method is defined with "TxNotSupported"
159: */
160:
161: public String txNotSupported() {
162:
163: log.debug("TxSessionBean.txNotSupported() called");
164:
165: try {
166: Object tx = getDaTransaction();
167: if (tx == null)
168: return "notSupported sees no transaction";
169: else
170: throw new Error("txNotSupported sees a transaction");
171: } catch (Exception e) {
172: log.debug("failed", e);
173: return e.getMessage();
174: }
175: }
176:
177: /*
178: * This method is defined with "Required" and it passes it to a Supports Tx
179: */
180: public String requiredToSupports() throws RemoteException {
181:
182: log.debug("TxSessionBean.requiredToSupports() called");
183:
184: String message;
185: Object tx = getDaTransaction();
186: if (tx == null)
187: throw new Error("Required doesn't see a transaction");
188: else
189: message = "Required sees a transaction " + tx.hashCode()
190: + " Supports should see the same ";
191:
192: message = message
193: + ((TxSession) sessionContext.getEJBObject())
194: .txSupports();
195:
196: // And after invocation we should have the same transaction
197: tx = getDaTransaction();
198: if (tx == null)
199: throw new Error(
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:
208: /*
209: * This method is defined with "Required" and it passes it to a NotSupported Tx
210: */
211: public String requiredToNotSupported() throws RemoteException {
212:
213: log.debug("TxSessionBean.requiredToNotSupported() called");
214:
215: String message;
216: Object tx = getDaTransaction();
217: if (tx == null)
218: throw new Error("Required doesn't see a transaction");
219: else
220: message = "Required sees a transaction " + tx.hashCode()
221: + " NotSupported should see the same ";
222:
223: message = message
224: + ((TxSession) sessionContext.getEJBObject())
225: .txNotSupported();
226:
227: // And after invocation we should have the same transaction
228: tx = getDaTransaction();
229: if (tx == null)
230: throw new Error(
231: "Required doesn't see a transaction COMING BACK");
232: else
233: return message
234: + " on coming back Required sees a transaction "
235: + tx.hashCode();
236: }
237:
238: public String requiredToRequiresNew() throws RemoteException {
239:
240: log.debug("TxSessionBean.requiredToRequiresNew() called");
241:
242: String message;
243: Object tx = getDaTransaction();
244: if (tx == null)
245: throw new Error("Required doesn't see a transaction");
246: else
247: message = "Required sees a transaction " + tx.hashCode()
248: + " Requires new should see a new transaction ";
249:
250: message = message
251: + ((TxSession) sessionContext.getEJBObject())
252: .txRequiresNew();
253:
254: // And after invocation we should have the same transaction
255: tx = getDaTransaction();
256: if (tx == null)
257: throw new Error(
258: "Required doesn't see a transaction COMING BACK");
259: else
260: return message
261: + " on coming back Required sees a transaction "
262: + tx.hashCode();
263: }
264:
265: private Object getDaTransaction() {
266:
267: try {
268:
269: return ((javax.transaction.TransactionManager) new InitialContext()
270: .lookup("java:/TransactionManager"))
271: .getTransaction();
272: } catch (Exception e) {
273: return null;
274: }
275: }
276:
277: }
|