1 25 26 package org.objectweb.easybeans.examples.statefulbean; 27 28 import java.util.Hashtable ; 29 30 import javax.naming.Context ; 31 import javax.naming.InitialContext ; 32 import javax.naming.NamingException ; 33 import javax.transaction.UserTransaction ; 34 35 39 public final class ClientStateful { 40 41 44 private static final int FIRST_BUY_AMOUNT = 10; 45 46 49 private static final int SECOND_BUY_AMOUNT = 20; 50 51 54 private static final int THIRD_BUY_AMOUNT = 50; 55 56 59 private static final String DEFAULT_INITIAL_CONTEXT_FACTORY = "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory"; 60 61 64 private ClientStateful() { 65 66 } 67 68 72 public static void main(final String [] args) { 73 74 Context initialContext = null; 75 76 try { 77 initialContext = getInitialContext(); 78 } catch (NamingException e) { 79 System.err.println("Cannot get InitialContext: " + e); 80 System.exit(2); 81 } 82 StatefulRemote statefulBean = null; 83 try { 84 statefulBean = (StatefulRemote) initialContext 85 .lookup("org.objectweb.easybeans.examples.statefulbean.StatefulBean" + "_" 86 + StatefulRemote.class.getName() + "@Remote"); 87 } catch (NamingException e) { 88 System.err.println("Cannot get statefulBean: " + e); 89 System.exit(2); 90 } 91 92 UserTransaction utx = null; 94 try { 95 utx = (UserTransaction ) initialContext.lookup("javax.transaction.UserTransaction"); 96 } catch (NamingException e) { 97 System.err.println("Cannot lookup UserTransaction: " + e); 98 System.exit(2); 99 } 100 101 try { 103 System.out.println("Start a first transaction"); 104 utx.begin(); 105 System.out.println("First request on the new bean"); 106 statefulBean.buy(FIRST_BUY_AMOUNT); 107 System.out.println("Second request on the bean"); 108 statefulBean.buy(SECOND_BUY_AMOUNT); 109 System.out.println("Commit the transaction"); 110 utx.commit(); 111 } catch (Exception e) { 112 System.err.println("exception during 1st Tx: " + e); 113 System.exit(2); 114 } 115 116 try { 118 System.out.println("Start a second transaction"); 119 utx.begin(); 120 System.out.println("Buy " + THIRD_BUY_AMOUNT + " amount."); 121 statefulBean.buy(THIRD_BUY_AMOUNT); 122 System.out.println("Rollback the transaction"); 123 utx.rollback(); 124 } catch (Exception e) { 125 System.err.println("exception during 2nd Tx: " + e); 126 System.exit(2); 127 } 128 129 System.out.println("after rollback, value = " + statefulBean.read()); 130 131 int val = 0; 133 try { 134 System.out.println("Request outside any transaction"); 135 val = statefulBean.read(); 136 } catch (Exception e) { 137 System.err.println("Cannot read value on t1 : " + e); 138 System.exit(2); 139 } 140 141 System.out.println("Check that value = " + (FIRST_BUY_AMOUNT + SECOND_BUY_AMOUNT)); 142 if (val != FIRST_BUY_AMOUNT + SECOND_BUY_AMOUNT) { 143 System.err.println("Bad value read: " + val); 144 System.exit(2); 145 } 146 147 System.out.println("ClientStateful OK. Exiting."); 148 } 149 150 154 private static Context getInitialContext() throws NamingException { 155 156 Hashtable <String , Object > env = new Hashtable <String , Object >(); 160 env.put(Context.INITIAL_CONTEXT_FACTORY, getInitialContextFactory()); 161 162 165 return new InitialContext (env); 166 } 167 168 173 private static String getInitialContextFactory() { 174 String prop = System.getProperty("easybeans.client.initial-context-factory"); 175 if (prop == null) { 177 prop = DEFAULT_INITIAL_CONTEXT_FACTORY; 178 } 179 return prop; 180 } 181 182 } 183 | Popular Tags |