1 27 28 package jaasclient; 29 30 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 46 public class ClientJAASOpContClient { 47 48 51 private static final int FIRST_BUY_AMOUNT = 10; 52 53 56 private static final int SECOND_BUY_AMOUNT = 20; 57 58 61 private static final int THIRD_BUY_AMOUNT = 50; 62 63 66 private ClientJAASOpContClient() { 67 68 } 69 70 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 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 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 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 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 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 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 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 |