001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ClientStateful.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.examples.statefulbean;
025:
026: import java.util.Hashtable;
027:
028: import javax.naming.Context;
029: import javax.naming.InitialContext;
030: import javax.naming.NamingException;
031: import javax.transaction.UserTransaction;
032:
033: /**
034: * Simple client of the stateless.
035: * @author Florent Benoit
036: */
037: public final class ClientStateful {
038:
039: /**
040: * First amount to buy.
041: */
042: private static final int FIRST_BUY_AMOUNT = 10;
043:
044: /**
045: * Second amount to buy.
046: */
047: private static final int SECOND_BUY_AMOUNT = 20;
048:
049: /**
050: * Third amount to buy (will be rollback).
051: */
052: private static final int THIRD_BUY_AMOUNT = 50;
053:
054: /**
055: * Default InitialContextFactory to use.
056: */
057: private static final String DEFAULT_INITIAL_CONTEXT_FACTORY = "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory";
058:
059: /**
060: * Client class, no public constructor.
061: */
062: private ClientStateful() {
063:
064: }
065:
066: /**
067: * Main method.
068: * @param args the arguments (not required)
069: */
070: public static void main(final String[] args) {
071:
072: Context initialContext = null;
073:
074: try {
075: initialContext = getInitialContext();
076: } catch (NamingException e) {
077: System.err.println("Cannot get InitialContext: " + e);
078: System.exit(2);
079: }
080: StatefulRemote statefulBean = null;
081: try {
082: statefulBean = (StatefulRemote) initialContext
083: .lookup("org.ow2.easybeans.examples.statefulbean.StatefulBean"
084: + "_"
085: + StatefulRemote.class.getName()
086: + "@Remote");
087: } catch (NamingException e) {
088: System.err.println("Cannot get statefulBean: " + e);
089: System.exit(2);
090: }
091:
092: // We want to start transactions from client: get UserTransaction
093: UserTransaction utx = null;
094: try {
095: utx = (UserTransaction) initialContext
096: .lookup("javax.transaction.UserTransaction");
097: } catch (NamingException e) {
098: System.err.println("Cannot lookup UserTransaction: " + e);
099: System.exit(2);
100: }
101:
102: // First transaction (committed)
103: try {
104: System.out.println("Start a first transaction");
105: utx.begin();
106: System.out.println("First request on the new bean");
107: statefulBean.buy(FIRST_BUY_AMOUNT);
108: System.out.println("Second request on the bean");
109: statefulBean.buy(SECOND_BUY_AMOUNT);
110: System.out.println("Commit the transaction");
111: utx.commit();
112: } catch (Exception e) {
113: System.err.println("exception during 1st Tx: " + e);
114: System.exit(2);
115: }
116:
117: // Start another transaction (rolled back)
118: try {
119: System.out.println("Start a second transaction");
120: utx.begin();
121: System.out.println("Buy " + THIRD_BUY_AMOUNT + " amount.");
122: statefulBean.buy(THIRD_BUY_AMOUNT);
123: System.out.println("Rollback the transaction");
124: utx.rollback();
125: } catch (Exception e) {
126: System.err.println("exception during 2nd Tx: " + e);
127: System.exit(2);
128: }
129:
130: System.out.println("after rollback, value = "
131: + statefulBean.read());
132:
133: // Get the total bought, outside the transaction
134: int val = 0;
135: try {
136: System.out.println("Request outside any transaction");
137: val = statefulBean.read();
138: } catch (Exception e) {
139: System.err.println("Cannot read value on t1 : " + e);
140: System.exit(2);
141: }
142:
143: System.out.println("Check that value = "
144: + (FIRST_BUY_AMOUNT + SECOND_BUY_AMOUNT));
145: if (val != FIRST_BUY_AMOUNT + SECOND_BUY_AMOUNT) {
146: System.err.println("Bad value read: " + val);
147: System.exit(2);
148: }
149:
150: System.out.println("ClientStateful OK. Exiting.");
151: }
152:
153: /**
154: * @return Returns the InitialContext.
155: * @throws NamingException If the Context cannot be created.
156: */
157: private static Context getInitialContext() throws NamingException {
158:
159: // if user don't use jclient/client container
160: // we can specify the InitialContextFactory to use
161: // But this is *not recommended*.
162: Hashtable<String, Object> env = new Hashtable<String, Object>();
163: env.put(Context.INITIAL_CONTEXT_FACTORY,
164: getInitialContextFactory());
165:
166: // Usually a simple new InitialContext() without any parameters is sufficent.
167: // return new InitialContext();
168:
169: return new InitialContext(env);
170: }
171:
172: /**
173: * Returns a configurable InitialContextFactory classname.<br/>
174: * Can be configured with the <code>easybeans.client.initial-context-factory</code> System property.
175: * @return Returns a configurable InitialContextFactory classname.
176: */
177: private static String getInitialContextFactory() {
178: String prop = System
179: .getProperty("easybeans.client.initial-context-factory");
180: // If not found, use the default
181: if (prop == null) {
182: prop = DEFAULT_INITIAL_CONTEXT_FACTORY;
183: }
184: return prop;
185: }
186:
187: }
|