KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > earsample > clients > Client


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@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  * Initial developer: Florent Benoit
22  *
23  * --------------------------------------------------------------------------
24  * $Id: Client.java,v 1.3 2004/04/19 06:39:30 benoitf Exp $
25  * --------------------------------------------------------------------------
26  */

27
28 package org.objectweb.earsample.clients;
29
30 import java.net.URL JavaDoc;
31
32 import javax.naming.Context JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.transaction.UserTransaction JavaDoc;
35 import javax.rmi.PortableRemoteObject JavaDoc;
36
37 import org.objectweb.earsample.beans.secusb.Op;
38 import org.objectweb.earsample.beans.secusb.OpHome;
39
40 /**
41  * Heavy client doing some operations on a bean
42  * @author jonas team
43  */

44 public class Client {
45
46     /**
47      * First amount to buy
48      */

49     private static final int FIRST_BUY_AMOUNT = 10;
50
51     /**
52      * Second amount to buy
53      */

54     private static final int SECOND_BUY_AMOUNT = 20;
55
56     /**
57      * Third amount to buy (will be rollback)
58      */

59     private static final int THIRD_BUY_AMOUNT = 50;
60
61     /**
62      * Constructor. Hide constructor as it is an utility class
63      */

64     private Client() {
65
66     }
67
68     /**
69      * Main method
70      * @param args arguments of the client
71      */

72     public static void main(String JavaDoc[] args) {
73
74         Context JavaDoc initialContext = null;
75         System.out.print("Building a new InitialContext...");
76         try {
77             initialContext = new InitialContext JavaDoc();
78         } catch (Exception JavaDoc e) {
79             System.err.println("Cannot get initial context for JNDI: " + e);
80             System.exit(2);
81         }
82         System.out.println("done !");
83
84         System.out.print("Looking up java:comp/UserTransaction ...");
85         // We want to start transactions from client: get UserTransaction
86
UserTransaction JavaDoc utx = null;
87         try {
88             utx = (UserTransaction JavaDoc) initialContext.lookup("java:comp/UserTransaction");
89         } catch (Exception JavaDoc e) {
90             System.err.println("Cannot lookup UserTransaction: " + e);
91             System.exit(2);
92         }
93         System.out.println("done !");
94
95         String JavaDoc envEntry = null;
96         System.out.print("Looking up java:comp/env/envEntryString ...");
97         try {
98             envEntry = (String JavaDoc) initialContext.lookup("java:comp/env/envEntryString");
99         } catch (Exception JavaDoc e) {
100             System.err.println("Cannot get env-entry on JNDI " + e);
101             System.exit(2);
102         }
103         System.out.println("done !");
104         System.out.println("Env entry is : " + envEntry);
105
106         URL JavaDoc url = null;
107         System.out.print("Looking up java:comp/env/url/jonas ...");
108         try {
109             url = (URL JavaDoc) initialContext.lookup("java:comp/env/url/jonas");
110         } catch (Exception JavaDoc e) {
111             System.err.println("Cannot get URL on JNDI " + e);
112             System.exit(2);
113         }
114         System.out.println("done !");
115         System.out.println("Web site of jonas is at :" + url);
116
117         System.out.print("Looking up java:comp/env/ejb/Op ...");
118         // Connecting to OpHome thru JNDI
119
OpHome home = null;
120         try {
121             home = (OpHome) PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Op"), OpHome.class);
122             System.out.println("done !");
123         } catch (Exception JavaDoc e) {
124             e.printStackTrace();
125             System.err.println("Cannot lookup OpHome: " + e);
126             System.exit(2);
127         }
128
129         // OpBean creation
130
Op t1 = null;
131         try {
132             System.out.println("Create a bean");
133             t1 = home.create("User1");
134         } catch (Exception JavaDoc e) {
135             System.err.println("Cannot create OpBean: " + e);
136             System.exit(2);
137         }
138
139         // First transaction (committed)
140
try {
141             System.out.println("Start a first transaction");
142             utx.begin();
143             System.out.println("First request on the new bean");
144             t1.buy(FIRST_BUY_AMOUNT);
145             System.out.println("Second request on the bean");
146             t1.buy(SECOND_BUY_AMOUNT);
147             System.out.println("Commit the transaction");
148             utx.commit();
149         } catch (Exception JavaDoc e) {
150             System.err.println("exception during 1st Tx: " + e);
151             System.exit(2);
152         }
153         // Start another transaction (rolled back)
154
try {
155             System.out.println("Start a second transaction");
156             utx.begin();
157             t1.buy(THIRD_BUY_AMOUNT);
158             System.out.println("Rollback the transaction");
159             utx.rollback();
160         } catch (Exception JavaDoc e) {
161             System.err.println("exception during 2nd Tx: " + e);
162             System.exit(2);
163         }
164
165         // Get the total bought, outside the transaction
166
int val = 0;
167         try {
168             System.out.println("Request outside any transaction");
169             val = t1.read();
170         } catch (Exception JavaDoc e) {
171             System.err.println("Cannot read value on t1 : " + e);
172             System.exit(2);
173         }
174         if (val != FIRST_BUY_AMOUNT + SECOND_BUY_AMOUNT) {
175             System.err.println("Bad value read: " + val);
176             System.exit(2);
177         }
178
179         // Remove Session bean
180
try {
181             t1.remove();
182         } catch (Exception JavaDoc e) {
183             System.out.println("Exception on buy: " + e);
184             System.exit(2);
185         }
186         System.out.println("ClientOp OK. Exiting.");
187     }
188 }
189
190
Popular Tags