KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > examples > statefulbean > ClientStateful


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ClientStateful.java 1078 2006-08-10 09:28:57Z sauthieg $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.examples.statefulbean;
27
28 import java.util.Hashtable JavaDoc;
29
30 import javax.naming.Context JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33 import javax.transaction.UserTransaction JavaDoc;
34
35 /**
36  * Simple client of the stateless.
37  * @author Florent Benoit
38  */

39 public final class ClientStateful {
40
41     /**
42      * First amount to buy.
43      */

44     private static final int FIRST_BUY_AMOUNT = 10;
45
46     /**
47      * Second amount to buy.
48      */

49     private static final int SECOND_BUY_AMOUNT = 20;
50
51     /**
52      * Third amount to buy (will be rollback).
53      */

54     private static final int THIRD_BUY_AMOUNT = 50;
55
56     /**
57      * Default InitialContextFactory to use.
58      */

59     private static final String JavaDoc DEFAULT_INITIAL_CONTEXT_FACTORY = "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory";
60
61     /**
62      * Client class, no public constructor.
63      */

64     private ClientStateful() {
65
66     }
67
68     /**
69      * Main method.
70      * @param args the arguments (not required)
71      */

72     public static void main(final String JavaDoc[] args) {
73
74         Context JavaDoc initialContext = null;
75
76         try {
77             initialContext = getInitialContext();
78         } catch (NamingException JavaDoc e) {
79             System.err.println("Cannot get InitialContext: " + e);
80             System.exit(2);
81         }
82         StatefulRemote statefulBean = null;
83         try {
84             statefulBean = (StatefulRemote) initialContext
85                     .lookup("org.objectweb.easybeans.examples.statefulbean.StatefulBean" + "_"
86                             + StatefulRemote.class.getName() + "@Remote");
87         } catch (NamingException JavaDoc e) {
88             System.err.println("Cannot get statefulBean: " + e);
89             System.exit(2);
90         }
91
92         // We want to start transactions from client: get UserTransaction
93
UserTransaction JavaDoc utx = null;
94         try {
95             utx = (UserTransaction JavaDoc) initialContext.lookup("javax.transaction.UserTransaction");
96         } catch (NamingException JavaDoc e) {
97             System.err.println("Cannot lookup UserTransaction: " + e);
98             System.exit(2);
99         }
100
101         // First transaction (committed)
102
try {
103             System.out.println("Start a first transaction");
104             utx.begin();
105             System.out.println("First request on the new bean");
106             statefulBean.buy(FIRST_BUY_AMOUNT);
107             System.out.println("Second request on the bean");
108             statefulBean.buy(SECOND_BUY_AMOUNT);
109             System.out.println("Commit the transaction");
110             utx.commit();
111         } catch (Exception JavaDoc e) {
112             System.err.println("exception during 1st Tx: " + e);
113             System.exit(2);
114         }
115
116         // Start another transaction (rolled back)
117
try {
118             System.out.println("Start a second transaction");
119             utx.begin();
120             System.out.println("Buy " + THIRD_BUY_AMOUNT + " amount.");
121             statefulBean.buy(THIRD_BUY_AMOUNT);
122             System.out.println("Rollback the transaction");
123             utx.rollback();
124         } catch (Exception JavaDoc e) {
125             System.err.println("exception during 2nd Tx: " + e);
126             System.exit(2);
127         }
128
129         System.out.println("after rollback, value = " + statefulBean.read());
130
131         // Get the total bought, outside the transaction
132
int val = 0;
133         try {
134             System.out.println("Request outside any transaction");
135             val = statefulBean.read();
136         } catch (Exception JavaDoc e) {
137             System.err.println("Cannot read value on t1 : " + e);
138             System.exit(2);
139         }
140
141         System.out.println("Check that value = " + (FIRST_BUY_AMOUNT + SECOND_BUY_AMOUNT));
142         if (val != FIRST_BUY_AMOUNT + SECOND_BUY_AMOUNT) {
143             System.err.println("Bad value read: " + val);
144             System.exit(2);
145         }
146
147         System.out.println("ClientStateful OK. Exiting.");
148     }
149
150     /**
151      * @return Returns the InitialContext.
152      * @throws NamingException If the Context cannot be created.
153      */

154     private static Context JavaDoc getInitialContext() throws NamingException JavaDoc {
155
156         // if user don't use jclient/client container
157
// we can specify the InitialContextFactory to use
158
// But this is *not recommended*.
159
Hashtable JavaDoc<String JavaDoc, Object JavaDoc> env = new Hashtable JavaDoc<String JavaDoc, Object JavaDoc>();
160         env.put(Context.INITIAL_CONTEXT_FACTORY, getInitialContextFactory());
161
162         // Usually a simple new InitialContext() without any parameters is sufficent.
163
// return new InitialContext();
164

165         return new InitialContext JavaDoc(env);
166     }
167
168     /**
169      * Returns a configurable InitialContextFactory classname.<br/>
170      * Can be configured with the <code>easybeans.client.initial-context-factory</code> System property.
171      * @return Returns a configurable InitialContextFactory classname.
172      */

173     private static String JavaDoc getInitialContextFactory() {
174         String JavaDoc prop = System.getProperty("easybeans.client.initial-context-factory");
175         // If not found, use the default
176
if (prop == null) {
177             prop = DEFAULT_INITIAL_CONTEXT_FACTORY;
178         }
179         return prop;
180     }
181
182 }
183
Popular Tags