1 27 28 package org.objectweb.earsample.clients; 29 30 import java.net.URL ; 31 32 import javax.naming.Context ; 33 import javax.naming.InitialContext ; 34 import javax.transaction.UserTransaction ; 35 import javax.rmi.PortableRemoteObject ; 36 37 import org.objectweb.earsample.beans.secusb.Op; 38 import org.objectweb.earsample.beans.secusb.OpHome; 39 40 44 public class Client { 45 46 49 private static final int FIRST_BUY_AMOUNT = 10; 50 51 54 private static final int SECOND_BUY_AMOUNT = 20; 55 56 59 private static final int THIRD_BUY_AMOUNT = 50; 60 61 64 private Client() { 65 66 } 67 68 72 public static void main(String [] args) { 73 74 Context initialContext = null; 75 System.out.print("Building a new InitialContext..."); 76 try { 77 initialContext = new InitialContext (); 78 } catch (Exception e) { 79 System.err.println("Cannot get initial context for JNDI: " + e); 80 System.exit(2); 81 } 82 System.out.println("done !"); 83 84 System.out.print("Looking up java:comp/UserTransaction ..."); 85 UserTransaction utx = null; 87 try { 88 utx = (UserTransaction ) initialContext.lookup("java:comp/UserTransaction"); 89 } catch (Exception e) { 90 System.err.println("Cannot lookup UserTransaction: " + e); 91 System.exit(2); 92 } 93 System.out.println("done !"); 94 95 String envEntry = null; 96 System.out.print("Looking up java:comp/env/envEntryString ..."); 97 try { 98 envEntry = (String ) initialContext.lookup("java:comp/env/envEntryString"); 99 } catch (Exception e) { 100 System.err.println("Cannot get env-entry on JNDI " + e); 101 System.exit(2); 102 } 103 System.out.println("done !"); 104 System.out.println("Env entry is : " + envEntry); 105 106 URL url = null; 107 System.out.print("Looking up java:comp/env/url/jonas ..."); 108 try { 109 url = (URL ) initialContext.lookup("java:comp/env/url/jonas"); 110 } catch (Exception e) { 111 System.err.println("Cannot get URL on JNDI " + e); 112 System.exit(2); 113 } 114 System.out.println("done !"); 115 System.out.println("Web site of jonas is at :" + url); 116 117 System.out.print("Looking up java:comp/env/ejb/Op ..."); 118 OpHome home = null; 120 try { 121 home = (OpHome) PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Op"), OpHome.class); 122 System.out.println("done !"); 123 } catch (Exception e) { 124 e.printStackTrace(); 125 System.err.println("Cannot lookup OpHome: " + e); 126 System.exit(2); 127 } 128 129 Op t1 = null; 131 try { 132 System.out.println("Create a bean"); 133 t1 = home.create("User1"); 134 } catch (Exception e) { 135 System.err.println("Cannot create OpBean: " + e); 136 System.exit(2); 137 } 138 139 try { 141 System.out.println("Start a first transaction"); 142 utx.begin(); 143 System.out.println("First request on the new bean"); 144 t1.buy(FIRST_BUY_AMOUNT); 145 System.out.println("Second request on the bean"); 146 t1.buy(SECOND_BUY_AMOUNT); 147 System.out.println("Commit the transaction"); 148 utx.commit(); 149 } catch (Exception e) { 150 System.err.println("exception during 1st Tx: " + e); 151 System.exit(2); 152 } 153 try { 155 System.out.println("Start a second transaction"); 156 utx.begin(); 157 t1.buy(THIRD_BUY_AMOUNT); 158 System.out.println("Rollback the transaction"); 159 utx.rollback(); 160 } catch (Exception e) { 161 System.err.println("exception during 2nd Tx: " + e); 162 System.exit(2); 163 } 164 165 int val = 0; 167 try { 168 System.out.println("Request outside any transaction"); 169 val = t1.read(); 170 } catch (Exception e) { 171 System.err.println("Cannot read value on t1 : " + e); 172 System.exit(2); 173 } 174 if (val != FIRST_BUY_AMOUNT + SECOND_BUY_AMOUNT) { 175 System.err.println("Bad value read: " + val); 176 System.exit(2); 177 } 178 179 try { 181 t1.remove(); 182 } catch (Exception e) { 183 System.out.println("Exception on buy: " + e); 184 System.exit(2); 185 } 186 System.out.println("ClientOp OK. Exiting."); 187 } 188 } 189 190 | Popular Tags |