KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sb > ClientOp


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(s): ____________________________________.
22  * Contributor(s): ______________________________________.
23  *
24  * --------------------------------------------------------------------------
25  * $Id: ClientOp.java,v 1.10 2004/04/19 06:39:29 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28
29 package sb;
30
31 import javax.naming.Context;
32 import javax.naming.InitialContext;
33 import javax.rmi.PortableRemoteObject;
34 import javax.transaction.UserTransaction;
35
36 /**
37  * Sample for Session Bean. Usage: java sb.ClientOp
38  * Heavy client doing some operations on a bean
39  * @author jonas team
40  */

41 public class ClientOp {
42     /**
43      * First amount to buy
44      */

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

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

55     private static final int THIRD_BUY_AMOUNT = 50;
56
57     /**
58      * Constructor. Hide constructor as it is an utility class
59      */

60     private ClientOp() {
61
62     }
63
64     /**
65      * Main method
66      * @param args arguments of the client
67      */

68     public static void main(String[] args) {
69
70         Context initialContext = null;
71         try {
72             initialContext = new InitialContext();
73         } catch (Exception e) {
74             System.err.println("Cannot get initial context for JNDI: " + e);
75             System.exit(2);
76         }
77
78         // We want to start transactions from client: get UserTransaction
79
UserTransaction utx = null;
80         try {
81
82             // Comment the following lines if you want to use a David Client:
83
utx = (UserTransaction) initialContext.lookup("javax.transaction.UserTransaction");
84
85             // Uncomment the following lines if you want to use a David Client:
86
/*
87              * Properties prop = new Properties();
88              * prop.put("java.naming.factory.initial",
89              * "com.sun.jndi.rmi.registry.RegistryContextFactory");
90              * prop.put("java.naming.provider.url", "rmi://localhost:1099"); //
91              * update the port if necessary InitialContext ictx = new
92              * InitialContext(prop); utx = (UserTransaction)
93              * ictx.lookup("javax.transaction.UserTransaction");
94              */

95
96         } catch (Exception e) {
97             System.err.println("Cannot lookup UserTransaction: " + e);
98             System.exit(2);
99         }
100
101         // Connecting to OpHome thru JNDI
102
OpHome home = null;
103         try {
104             home = (OpHome) PortableRemoteObject.narrow(initialContext.lookup("OpHome"), OpHome.class);
105         } catch (Exception e) {
106             System.err.println("Cannot lookup OpHome: " + e);
107             System.exit(2);
108         }
109
110         // OpBean creation
111
Op t1 = null;
112         try {
113             System.out.println("Create a bean");
114             t1 = home.create("User1");
115         } catch (Exception e) {
116             System.err.println("Cannot create OpBean: " + e);
117             System.exit(2);
118         }
119
120         // First transaction (committed)
121
try {
122             System.out.println("Start a first transaction");
123             utx.begin();
124             System.out.println("First request on the new bean");
125             t1.buy(FIRST_BUY_AMOUNT);
126             System.out.println("Second request on the bean");
127             t1.buy(SECOND_BUY_AMOUNT);
128             System.out.println("Commit the transaction");
129             utx.commit();
130         } catch (Exception e) {
131             System.err.println("exception during 1st Tx: " + e);
132             System.exit(2);
133         }
134         // Start another transaction (rolled back)
135
try {
136             System.out.println("Start a second transaction");
137             utx.begin();
138             t1.buy(THIRD_BUY_AMOUNT);
139             System.out.println("Rollback the transaction");
140             utx.rollback();
141         } catch (Exception e) {
142             System.err.println("exception during 2nd Tx: " + e);
143             System.exit(2);
144         }
145
146         // Get the total bought, outside the transaction
147
int val = 0;
148         try {
149             System.out.println("Request outside any transaction");
150             val = t1.read();
151         } catch (Exception e) {
152             System.err.println("Cannot read value on t1 : " + e);
153             System.exit(2);
154         }
155         if (val != FIRST_BUY_AMOUNT + SECOND_BUY_AMOUNT) {
156             System.err.println("Bad value read: " + val);
157             System.exit(2);
158         }
159
160         // Remove Session bean
161
try {
162             t1.remove();
163         } catch (Exception e) {
164             System.out.println("Exception on buy: " + e);
165             System.exit(2);
166         }
167         System.out.println("ClientOp OK. Exiting.");
168     }
169 }
170
171
Popular Tags