1 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 52 public class ClientJAASOp { 53 54 57 private static final int FIRST_BUY_AMOUNT = 10; 58 59 62 private static final int SECOND_BUY_AMOUNT = 20; 63 64 67 private static final int THIRD_BUY_AMOUNT = 50; 68 69 72 private ClientJAASOp() { 73 74 } 75 76 80 public static void main(String[] args) { 81 82 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 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 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 try { 131 lc.login(); 132 } catch (LoginException le) { 133 System.err.println("Authentication failed : " + le.getMessage()); 134 System.exit(2); 135 } 136 137 System.out.println("Authentication succeeded"); 139 140 UserTransaction utx = null; 142 try { 143 144 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 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 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 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 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 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 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 |