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.txpropiiop.ejb.a;
023:
024: import java.rmi.RemoteException;
025: import java.util.Properties;
026: import javax.ejb.CreateException;
027: import javax.ejb.EJBException;
028: import javax.ejb.SessionBean;
029: import javax.ejb.SessionContext;
030: import javax.naming.Context;
031: import javax.naming.InitialContext;
032: import javax.rmi.PortableRemoteObject;
033:
034: import org.jboss.logging.Logger;
035: import org.jboss.test.txpropiiop.interfaces.b.SessionB;
036: import org.jboss.test.txpropiiop.interfaces.b.SessionBHome;
037: import org.jboss.tm.iiop.TxClientInterceptor;
038: import org.omg.CORBA.ORB;
039: import org.omg.CosTransactions.PropagationContext;
040: import org.omg.CosTransactions.TransIdentity;
041: import org.omg.CosTransactions.otid_t;
042:
043: /**
044: * A SessionA.
045: *
046: * @author <a href="adrian@jboss.com">Adrian Brock</a>
047: * @version $Revision: 57211 $
048: */
049: public class SessionAEJB implements SessionBean {
050: private static final Logger log = Logger
051: .getLogger(SessionAEJB.class);
052:
053: private static SessionB sessionB;
054:
055: public void invokeSessionB() {
056: try {
057: SessionB session = getSessionB();
058:
059: String hello = "Hello";
060: String result = session.sayHello(hello);
061: validate(hello, result);
062: } catch (Exception e) {
063: throw new EJBException(e);
064: }
065: }
066:
067: public void testTxToNotSupported() {
068: setTransaction();
069: try {
070: SessionB session = getSessionB();
071:
072: String hello = "Hello";
073: String result = session.testNotSupported(hello);
074: validate(hello, result);
075: } catch (Exception e) {
076: throw new EJBException(e);
077: } finally {
078: unsetTransaction();
079: }
080: }
081:
082: public void testTxToRequired() {
083: setTransaction();
084: try {
085: SessionB session = getSessionB();
086:
087: String hello = "Hello";
088: String result = session.testRequired(hello);
089: throw new EJBException("Expected RemoteException");
090: } catch (EJBException e) {
091: throw e;
092: } catch (RemoteException expected) {
093: } catch (Exception e) {
094: throw new EJBException(e);
095: } finally {
096: unsetTransaction();
097: }
098: }
099:
100: public void testTxToSupports() {
101: setTransaction();
102: try {
103: SessionB session = getSessionB();
104:
105: String hello = "Hello";
106: String result = session.testSupports(hello);
107: throw new EJBException("Expected RemoteException");
108: } catch (EJBException e) {
109: throw e;
110: } catch (RemoteException expected) {
111: } catch (Exception e) {
112: throw new EJBException(e);
113: } finally {
114: unsetTransaction();
115: }
116: }
117:
118: public void testTxToRequiresNew() {
119: setTransaction();
120: try {
121: SessionB session = getSessionB();
122:
123: String hello = "Hello";
124: String result = session.testRequiresNew(hello);
125: validate(hello, result);
126: } catch (Exception e) {
127: throw new EJBException(e);
128: } finally {
129: unsetTransaction();
130: }
131: }
132:
133: public void testTxToMandatory() {
134: setTransaction();
135: try {
136: SessionB session = getSessionB();
137:
138: String hello = "Hello";
139: String result = session.testMandatory(hello);
140: throw new EJBException("Expected RemoteException");
141: } catch (EJBException e) {
142: throw e;
143: } catch (RemoteException expected) {
144: } catch (Exception e) {
145: throw new EJBException(e);
146: } finally {
147: unsetTransaction();
148: }
149: }
150:
151: public void testTxToNever() {
152: setTransaction();
153: try {
154: SessionB session = getSessionB();
155:
156: String hello = "Hello";
157: String result = session.testNever(hello);
158: throw new EJBException("Expected RemoteException");
159: } catch (EJBException e) {
160: throw e;
161: } catch (RemoteException expected) {
162: } catch (Exception e) {
163: throw new EJBException(e);
164: } finally {
165: unsetTransaction();
166: }
167: }
168:
169: public void testNoTxToNotSupported() {
170: try {
171: SessionB session = getSessionB();
172:
173: String hello = "Hello";
174: String result = session.testNotSupported(hello);
175: validate(hello, result);
176: } catch (Exception e) {
177: throw new EJBException(e);
178: }
179: }
180:
181: public void testNoTxToRequired() {
182: try {
183: SessionB session = getSessionB();
184:
185: String hello = "Hello";
186: String result = session.testRequired(hello);
187: validate(hello, result);
188: } catch (Exception e) {
189: throw new EJBException(e);
190: }
191: }
192:
193: public void testNoTxToSupports() {
194: try {
195: SessionB session = getSessionB();
196:
197: String hello = "Hello";
198: String result = session.testSupports(hello);
199: validate(hello, result);
200: } catch (Exception e) {
201: throw new EJBException(e);
202: }
203: }
204:
205: public void testNoTxToRequiresNew() {
206: try {
207: SessionB session = getSessionB();
208:
209: String hello = "Hello";
210: String result = session.testRequiresNew(hello);
211: validate(hello, result);
212: } catch (Exception e) {
213: throw new EJBException(e);
214: }
215: }
216:
217: public void testNoTxToMandatory() {
218: try {
219: SessionB session = getSessionB();
220:
221: String hello = "Hello";
222: String result = session.testMandatory(hello);
223: throw new EJBException("Expected RemoteException");
224: } catch (EJBException e) {
225: throw e;
226: } catch (RemoteException expected) {
227: } catch (Exception e) {
228: throw new EJBException(e);
229: }
230: }
231:
232: public void testNoTxToNever() {
233: try {
234: SessionB session = getSessionB();
235:
236: String hello = "Hello";
237: String result = session.testNever(hello);
238: validate(hello, result);
239: } catch (Exception e) {
240: throw new EJBException(e);
241: }
242: }
243:
244: public void ejbCreate() throws CreateException {
245: }
246:
247: public void ejbActivate() {
248: }
249:
250: public void ejbPassivate() {
251: }
252:
253: public void ejbRemove() {
254: }
255:
256: public void setSessionContext(SessionContext ctx) {
257: }
258:
259: protected void setTransaction() {
260: PropagationContext pc = new PropagationContext();
261: pc.parents = new TransIdentity[0];
262: pc.current = new TransIdentity();
263: pc.current.otid = new otid_t();
264: pc.current.otid.formatID = 666;
265: pc.current.otid.bqual_length = 1;
266: pc.current.otid.tid = new byte[] { (byte) 1 };
267: pc.implementation_specific_data = getORB().create_any();
268: pc.implementation_specific_data.insert_long(1);
269: TxClientInterceptor.setOutgoingPropagationContext(pc);
270: }
271:
272: protected void unsetTransaction() {
273: TxClientInterceptor.unsetOutgoingPropagationContext();
274: }
275:
276: private static void validate(String hello, String result)
277: throws Exception {
278: if (hello == result)
279: throw new EJBException("Should be pass by value");
280: if (hello.equals(result) == false)
281: throw new EJBException("Did not get expected 'Hello'");
282: }
283:
284: private static SessionB getSessionB() throws Exception {
285: if (sessionB == null) {
286: SessionBHome home = (SessionBHome) lookup("SessionB",
287: SessionBHome.class);
288: sessionB = home.create();
289: }
290: return sessionB;
291: }
292:
293: private static Object lookup(String name, Class clazz)
294: throws Exception {
295: Properties jndiProps = new Properties();
296: jndiProps.put(Context.INITIAL_CONTEXT_FACTORY,
297: "com.sun.jndi.cosnaming.CNCtxFactory");
298: jndiProps.put(Context.PROVIDER_URL, "corbaloc::"
299: + System.getProperty("jboss.bind.address", "localhost")
300: + ":3528/JBoss/Naming/root");
301: Context ctx = new InitialContext(jndiProps);
302: Object obj = ctx.lookup(name);
303: Object result = PortableRemoteObject.narrow(obj, clazz);
304: return result;
305: }
306:
307: private static ORB getORB() {
308: try {
309: return (ORB) new InitialContext().lookup("java:comp/ORB");
310: } catch (Exception e) {
311: throw new EJBException(e);
312: }
313: }
314: }
|