001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer(s): jonas team
022: *
023: * --------------------------------------------------------------------------
024: * $Id: ClientJAASOp.java 4618 2004-04-19 06:39:30Z benoitf $
025: * --------------------------------------------------------------------------
026: */package jaasclient;
027:
028: import jaasclient.beans.secusb.JAASOp;
029: import jaasclient.beans.secusb.JAASOpHome;
030:
031: import javax.naming.Context;
032: import javax.naming.InitialContext;
033: import javax.rmi.PortableRemoteObject;
034: import javax.security.auth.callback.CallbackHandler;
035: import javax.security.auth.login.LoginContext;
036: import javax.security.auth.login.LoginException;
037: import javax.transaction.UserTransaction;
038:
039: import org.objectweb.jonas.security.auth.callback.DialogCallbackHandler;
040: import org.objectweb.jonas.security.auth.callback.LoginCallbackHandler;
041: import org.objectweb.jonas.security.auth.callback.NoInputCallbackHandler;
042:
043: /**
044: * Sample for Session Bean. Usage: jclient jaasclient.ClientJAASOp <text |
045: * dialog | noprompt>
046: * @author jonas team
047: * @author Florent Benoit : use a JAAS login module configuration for the
048: * authentication
049: */
050: public class ClientJAASOp {
051:
052: /**
053: * First amount to buy
054: */
055: private static final int FIRST_BUY_AMOUNT = 10;
056:
057: /**
058: * Second amount to buy
059: */
060: private static final int SECOND_BUY_AMOUNT = 20;
061:
062: /**
063: * Third amount to buy (will be rollback)
064: */
065: private static final int THIRD_BUY_AMOUNT = 50;
066:
067: /**
068: * Constructor. Hide constructor as it is an utility class
069: */
070: private ClientJAASOp() {
071:
072: }
073:
074: /**
075: * Main method
076: * @param args the arguments
077: */
078: public static void main(String[] args) {
079:
080: //Check if there are valid args
081: if (args.length != 1) {
082: System.err
083: .println("Syntax is : jclient jaasclient.ClientJAASOp <text | dialog | noprompt>");
084: System.err
085: .println(" - text : Prompt the user to enter login/password by command line");
086: System.err
087: .println(" - dialog : Prompt the user to enter login/password by a window dialog");
088: System.err
089: .println(" - noprompt : No prompt is asked to the user. A default login/password is used");
090: System.exit(2);
091: }
092:
093: // Which handler use ?
094: CallbackHandler handler = null;
095:
096: if (args[0].equalsIgnoreCase("text")) {
097: handler = new LoginCallbackHandler();
098: } else if (args[0].equalsIgnoreCase("dialog")) {
099: handler = new DialogCallbackHandler();
100: } else if (args[0].equalsIgnoreCase("noprompt")) {
101: handler = new NoInputCallbackHandler("jonas", "jonas");
102: } else {
103: System.err
104: .println("Invalid type '"
105: + args[0]
106: + "', valid syntax is : jclient jaasclient.ClientJAASOp <text | dialog | noprompt>");
107: System.exit(2);
108: }
109:
110: Context initialContext = null;
111: try {
112: initialContext = new InitialContext();
113: } catch (Exception e) {
114: System.err.println("Cannot get initial context for JNDI: "
115: + e);
116: System.exit(2);
117: }
118:
119: // Obtain a LoginContext
120: LoginContext lc = null;
121: try {
122: lc = new LoginContext("jaasclient", handler);
123: } catch (LoginException le) {
124: System.err.println("Cannot create LoginContext: "
125: + le.getMessage());
126: System.exit(2);
127: } catch (SecurityException se) {
128: System.err.println("Cannot create LoginContext: "
129: + se.getMessage());
130: System.exit(2);
131: }
132:
133: System.out
134: .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.");
135:
136: // Login
137: try {
138: lc.login();
139: } catch (LoginException le) {
140: System.err.println("Authentication failed : "
141: + le.getMessage());
142: System.exit(2);
143: }
144:
145: // Authentication is ok
146: System.out.println("Authentication succeeded");
147:
148: // We want to start transactions from client: get UserTransaction
149: UserTransaction utx = null;
150: try {
151:
152: // Comment the following lines if you want to use a David Client:
153: utx = (UserTransaction) initialContext
154: .lookup("javax.transaction.UserTransaction");
155: } catch (Exception e) {
156: System.err.println("Cannot lookup UserTransaction: " + e);
157: System.exit(2);
158: }
159:
160: // Connecting to JAASOpHome thru JNDI
161: JAASOpHome home = null;
162: try {
163: home = (JAASOpHome) PortableRemoteObject.narrow(
164: initialContext.lookup("JAASOpHome"),
165: JAASOpHome.class);
166: } catch (Exception e) {
167: System.err
168: .println("Cannot lookup JAASOpHome: "
169: + e
170: + ". Maybe you haven't do the 'jonas admin -a jaasop.jar'");
171: System.exit(2);
172: }
173:
174: // JAASOpBean creation
175: JAASOp t1 = null;
176: try {
177: System.out.println("Create a bean");
178: t1 = home.create("User1");
179: } catch (Exception e) {
180: System.err.println("Cannot create JAASOpBean: " + e);
181: System.exit(2);
182: }
183:
184: // First transaction (committed)
185: try {
186: System.out.println("Start a first transaction");
187: utx.begin();
188: System.out.println("First request on the new bean");
189: t1.buy(FIRST_BUY_AMOUNT);
190: System.out.println("Second request on the bean");
191: t1.buy(SECOND_BUY_AMOUNT);
192: System.out.println("Commit the transaction");
193: utx.commit();
194: } catch (Exception e) {
195: System.err.println("exception during 1st Tx: " + e);
196: System.exit(2);
197: }
198: // Start another transaction (rolled back)
199: try {
200: System.out.println("Start a second transaction");
201: utx.begin();
202: t1.buy(THIRD_BUY_AMOUNT);
203: System.out.println("Rollback the transaction");
204: utx.rollback();
205: } catch (Exception e) {
206: System.err.println("exception during 2nd Tx: " + e);
207: System.exit(2);
208: }
209:
210: // Get the total bought, outside the transaction
211: int val = 0;
212: try {
213: System.out.println("Request outside any transaction");
214: val = t1.read();
215: } catch (Exception e) {
216: System.err.println("Cannot read value on t1 : " + e);
217: System.exit(2);
218: }
219: if (val != FIRST_BUY_AMOUNT + SECOND_BUY_AMOUNT) {
220: System.err.println("Bad value read: " + val);
221: System.exit(2);
222: }
223:
224: // Remove Session bean
225: try {
226: t1.remove();
227: } catch (Exception e) {
228: System.out.println("Exception on buy: " + e);
229: System.exit(2);
230: }
231: System.out.println("ClientJAASOp OK. Exiting.");
232: System.exit(0);
233: }
234: }
|