KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jaasclient > ClientJAASOp


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: ClientJAASOp.java,v 1.3 2004/04/19 06:39:29 benoitf Exp $
25  * --------------------------------------------------------------------------
26  */

27
28 package jaasclient;
29
30 import jaasclient.beans.secusb.JAASOp;
31 import jaasclient.beans.secusb.JAASOpHome;
32
33 import javax.naming.Context;
34 import javax.naming.InitialContext;
35 import javax.rmi.PortableRemoteObject;
36 import javax.security.auth.callback.CallbackHandler;
37 import javax.security.auth.login.LoginContext;
38 import javax.security.auth.login.LoginException;
39 import javax.transaction.UserTransaction;
40
41 import org.objectweb.jonas.security.auth.callback.DialogCallbackHandler;
42 import org.objectweb.jonas.security.auth.callback.LoginCallbackHandler;
43 import org.objectweb.jonas.security.auth.callback.NoInputCallbackHandler;
44
45 /**
46  * Sample for Session Bean. Usage: jclient jaasclient.ClientJAASOp <text |
47  * dialog | noprompt>
48  * @author jonas team
49  * @author Florent Benoit : use a JAAS login module configuration for the
50  * authentication
51  */

52 public class ClientJAASOp {
53
54     /**
55      * First amount to buy
56      */

57     private static final int FIRST_BUY_AMOUNT = 10;
58
59     /**
60      * Second amount to buy
61      */

62     private static final int SECOND_BUY_AMOUNT = 20;
63
64     /**
65      * Third amount to buy (will be rollback)
66      */

67     private static final int THIRD_BUY_AMOUNT = 50;
68
69     /**
70      * Constructor. Hide constructor as it is an utility class
71      */

72     private ClientJAASOp() {
73
74     }
75
76     /**
77      * Main method
78      * @param args the arguments
79      */

80     public static void main(String[] args) {
81
82         //Check if there are valid args
83
if (args.length != 1) {
84             System.err.println("Syntax is : jclient jaasclient.ClientJAASOp <text | dialog | noprompt>");
85             System.err.println(" - text : Prompt the user to enter login/password by command line");
86             System.err.println(" - dialog : Prompt the user to enter login/password by a window dialog");
87             System.err.println(" - noprompt : No prompt is asked to the user. A default login/password is used");
88             System.exit(2);
89         }
90
91         // Which handler use ?
92
CallbackHandler handler = null;
93
94         if (args[0].equalsIgnoreCase("text")) {
95             handler = new LoginCallbackHandler();
96         } else if (args[0].equalsIgnoreCase("dialog")) {
97             handler = new DialogCallbackHandler();
98         } else if (args[0].equalsIgnoreCase("noprompt")) {
99             handler = new NoInputCallbackHandler("jonas", "jonas");
100         } else {
101             System.err.println("Invalid type '" + args[0]
102                     + "', valid syntax is : jclient jaasclient.ClientJAASOp <text | dialog | noprompt>");
103             System.exit(2);
104         }
105
106         Context initialContext = null;
107         try {
108             initialContext = new InitialContext();
109         } catch (Exception e) {
110             System.err.println("Cannot get initial context for JNDI: " + e);
111             System.exit(2);
112         }
113
114         // Obtain a LoginContext
115
LoginContext lc = null;
116         try {
117             lc = new LoginContext("jaasclient", handler);
118         } catch (LoginException le) {
119             System.err.println("Cannot create LoginContext: " + le.getMessage());
120             System.exit(2);
121         } catch (SecurityException se) {
122             System.err.println("Cannot create LoginContext: " + se.getMessage());
123             System.exit(2);
124         }
125
126         System.out
127                 .println("Use the l/p jonas/jonas to authenticate with the right role.\nYou can change this by editing the file JONAS_ROOT/conf/jonas-realm.xml.");
128
129         // Login
130
try {
131             lc.login();
132         } catch (LoginException le) {
133             System.err.println("Authentication failed : " + le.getMessage());
134             System.exit(2);
135         }
136
137         // Authentication is ok
138
System.out.println("Authentication succeeded");
139
140         // We want to start transactions from client: get UserTransaction
141
UserTransaction utx = null;
142         try {
143
144             // Comment the following lines if you want to use a David Client:
145
utx = (UserTransaction) initialContext.lookup("javax.transaction.UserTransaction");
146         } catch (Exception e) {
147             System.err.println("Cannot lookup UserTransaction: " + e);
148             System.exit(2);
149         }
150
151         // Connecting to JAASOpHome thru JNDI
152
JAASOpHome home = null;
153         try {
154             home = (JAASOpHome) PortableRemoteObject.narrow(initialContext.lookup("JAASOpHome"), JAASOpHome.class);
155         } catch (Exception e) {
156             System.err.println("Cannot lookup JAASOpHome: " + e
157                     + ". Maybe you haven't do the 'jonas admin -a jaasop.jar'");
158             System.exit(2);
159         }
160
161         // JAASOpBean creation
162
JAASOp t1 = null;
163         try {
164             System.out.println("Create a bean");
165             t1 = home.create("User1");
166         } catch (Exception e) {
167             System.err.println("Cannot create JAASOpBean: " + e);
168             System.exit(2);
169         }
170
171         // First transaction (committed)
172
try {
173             System.out.println("Start a first transaction");
174             utx.begin();
175             System.out.println("First request on the new bean");
176             t1.buy(FIRST_BUY_AMOUNT);
177             System.out.println("Second request on the bean");
178             t1.buy(SECOND_BUY_AMOUNT);
179             System.out.println("Commit the transaction");
180             utx.commit();
181         } catch (Exception e) {
182             System.err.println("exception during 1st Tx: " + e);
183             System.exit(2);
184         }
185         // Start another transaction (rolled back)
186
try {
187             System.out.println("Start a second transaction");
188             utx.begin();
189             t1.buy(THIRD_BUY_AMOUNT);
190             System.out.println("Rollback the transaction");
191             utx.rollback();
192         } catch (Exception e) {
193             System.err.println("exception during 2nd Tx: " + e);
194             System.exit(2);
195         }
196
197         // Get the total bought, outside the transaction
198
int val = 0;
199         try {
200             System.out.println("Request outside any transaction");
201             val = t1.read();
202         } catch (Exception e) {
203             System.err.println("Cannot read value on t1 : " + e);
204             System.exit(2);
205         }
206         if (val != FIRST_BUY_AMOUNT + SECOND_BUY_AMOUNT) {
207             System.err.println("Bad value read: " + val);
208             System.exit(2);
209         }
210
211         // Remove Session bean
212
try {
213             t1.remove();
214         } catch (Exception e) {
215             System.out.println("Exception on buy: " + e);
216             System.exit(2);
217         }
218         System.out.println("ClientJAASOp OK. Exiting.");
219         System.exit(0);
220     }
221 }
Popular Tags