1 27 28 package jaasclient; 29 30 import jaasclient.beans.secusb.JAASOp; 31 import jaasclient.beans.secusb.JAASOpHome; 32 33 import java.awt.event.ActionEvent; 34 35 import javax.naming.Context; 36 import javax.naming.InitialContext; 37 import javax.rmi.PortableRemoteObject; 38 import javax.swing.AbstractAction; 39 import javax.swing.Action; 40 import javax.swing.JButton; 41 import javax.swing.JFrame; 42 import javax.swing.WindowConstants; 43 import javax.transaction.UserTransaction; 44 45 51 public class ClientJAASOpContClientSwing { 52 53 56 private static final int FIRST_BUY_AMOUNT = 10; 57 58 61 private static final int SECOND_BUY_AMOUNT = 20; 62 63 66 private static final int THIRD_BUY_AMOUNT = 50; 67 68 71 private ClientJAASOpContClientSwing() { 72 73 } 74 75 79 public static void main(String[] args) { 80 81 82 final JFrame jf = new JFrame("Test Application"); 83 jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 84 final JButton jb = new JButton(); 85 final Action a = new AbstractAction("Click here to test!") { 86 public final void actionPerformed(final ActionEvent v) { 87 88 89 Context initialContext = null; 90 try { 91 initialContext = new InitialContext(); 92 } catch (Exception e) { 93 System.err.println("Cannot get initial context for JNDI: " + e); 94 System.exit(2); 95 } 96 97 UserTransaction utx = null; 99 try { 100 utx = (UserTransaction) initialContext.lookup("java:comp/UserTransaction"); 101 } catch (Exception e) { 102 System.err.println("Cannot lookup java:comp/UserTransaction: " + e); 103 System.exit(2); 104 } 105 106 JAASOpHome home = null; 108 try { 109 home = (JAASOpHome) PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/JAASOp"), JAASOpHome.class); 110 } catch (Exception e) { 111 System.err.println("Cannot lookup java:comp/env/ejb/JAASOp: " + e + ". Maybe you haven't do the 'jonas admin -a jaasop.jar'"); 112 System.exit(2); 113 } 114 115 JAASOp t1 = null; 117 try { 118 System.out.println("Create a bean"); 119 t1 = home.create("User1"); 120 } catch (Exception e) { 121 System.err.println("Cannot create JAASOpBean: " + e); 122 System.exit(2); 123 } 124 125 try { 127 System.out.println("Start a first transaction"); 128 utx.begin(); 129 System.out.println("First request on the new bean"); 130 t1.buy(FIRST_BUY_AMOUNT); 131 System.out.println("Second request on the bean"); 132 t1.buy(SECOND_BUY_AMOUNT); 133 System.out.println("Commit the transaction"); 134 utx.commit(); 135 } catch (Exception e) { 136 System.err.println("exception during 1st Tx: " + e); 137 System.exit(2); 138 } 139 try { 141 System.out.println("Start a second transaction"); 142 utx.begin(); 143 t1.buy(THIRD_BUY_AMOUNT); 144 System.out.println("Rollback the transaction"); 145 utx.rollback(); 146 } catch (Exception e) { 147 System.err.println("exception during 2nd Tx: " + e); 148 System.exit(2); 149 } 150 151 int val = 0; 153 try { 154 System.out.println("Request outside any transaction"); 155 val = t1.read(); 156 } catch (Exception e) { 157 System.err.println("Cannot read value on t1 : " + e); 158 System.exit(2); 159 } 160 if (val != FIRST_BUY_AMOUNT + SECOND_BUY_AMOUNT) { 161 System.err.println("Bad value read: " + val); 162 System.exit(2); 163 } 164 165 try { 167 t1.remove(); 168 } catch (Exception e) { 169 System.out.println("Exception on buy: " + e); 170 System.exit(2); 171 } 172 System.out.println("ClientJAASOpContClientSwing OK. Exiting."); 173 System.exit(0); 174 175 176 177 } 178 }; 179 jb.setAction(a); 180 jf.getContentPane().add(jb); 181 jf.pack(); 182 jf.setVisible(true); 183 } 184 } 185 | Popular Tags |