KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jaasclient > ClientJAASOpContClient


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): jonas team
22  *
23  * --------------------------------------------------------------------------
24  * $Id: ClientJAASOpContClient.java,v 1.2 2004/04/19 06:39:29 benoitf Exp $
25  * --------------------------------------------------------------------------
26  */

27
28 package jaasclient;
29
30 // import javax
31
import jaasclient.beans.secusb.JAASOp;
32 import jaasclient.beans.secusb.JAASOpHome;
33
34 import javax.naming.Context;
35 import javax.naming.InitialContext;
36 import javax.rmi.PortableRemoteObject;
37 import javax.transaction.UserTransaction;
38
39 /**
40  * Sample for Session Bean.
41  * Usage:
42  * jclient jaasclient.ClientJAASOpContClient
43  * @author jonas team
44  * @author Florent Benoit
45  */

46 public class ClientJAASOpContClient {
47
48     /**
49      * First amount to buy
50      */

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

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

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

66     private ClientJAASOpContClient() {
67
68     }
69
70     /**
71      * Main method
72      * @param args the arguments
73      */

74     public static void main(String[] args) {
75
76         Context initialContext = null;
77         try {
78             initialContext = new InitialContext();
79         } catch (Exception e) {
80             System.err.println("Cannot get initial context for JNDI: " + e);
81             System.exit(2);
82         }
83
84         // We want to start transactions from client: get UserTransaction
85
UserTransaction utx = null;
86         try {
87             utx = (UserTransaction) initialContext.lookup("java:comp/UserTransaction");
88         } catch (Exception e) {
89             System.err.println("Cannot lookup java:comp/UserTransaction: " + e);
90             System.exit(2);
91         }
92
93         // Connecting to JAASOpHome thru JNDI
94
JAASOpHome home = null;
95         try {
96             home = (JAASOpHome) PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/JAASOp"), JAASOpHome.class);
97         } catch (Exception e) {
98             System.err.println("Cannot lookup java:comp/env/ejb/JAASOp: " + e + ". Maybe you haven't do the 'jonas admin -a jaasop.jar'");
99             System.exit(2);
100         }
101
102         // JAASOpBean creation
103
JAASOp t1 = null;
104         try {
105             System.out.println("Create a bean");
106             t1 = home.create("User1");
107         } catch (Exception e) {
108             System.err.println("Cannot create JAASOpBean: " + e);
109             System.exit(2);
110         }
111
112         // First transaction (committed)
113
try {
114             System.out.println("Start a first transaction");
115             utx.begin();
116             System.out.println("First request on the new bean");
117             t1.buy(FIRST_BUY_AMOUNT);
118             System.out.println("Second request on the bean");
119             t1.buy(SECOND_BUY_AMOUNT);
120             System.out.println("Commit the transaction");
121             utx.commit();
122         } catch (Exception e) {
123             System.err.println("exception during 1st Tx: " + e);
124             System.exit(2);
125         }
126         // Start another transaction (rolled back)
127
try {
128             System.out.println("Start a second transaction");
129             utx.begin();
130             t1.buy(THIRD_BUY_AMOUNT);
131             System.out.println("Rollback the transaction");
132             utx.rollback();
133         } catch (Exception e) {
134             System.err.println("exception during 2nd Tx: " + e);
135             System.exit(2);
136         }
137
138         // Get the total bought, outside the transaction
139
int val = 0;
140         try {
141             System.out.println("Request outside any transaction");
142             val = t1.read();
143         } catch (Exception e) {
144             System.err.println("Cannot read value on t1 : " + e);
145             System.exit(2);
146         }
147         if (val != FIRST_BUY_AMOUNT + SECOND_BUY_AMOUNT) {
148             System.err.println("Bad value read: " + val);
149             System.exit(2);
150         }
151
152         // Remove Session bean
153
try {
154             t1.remove();
155         } catch (Exception e) {
156             System.out.println("Exception on buy: " + e);
157             System.exit(2);
158         }
159         System.out.println("ClientJAASOpContClient OK. Exiting.");
160         System.exit(0);
161     }
162 }
163
Popular Tags