1 22 package org.jboss.ejb3.test.bank; 23 24 import java.io.Serializable ; 25 import java.rmi.RemoteException ; 26 import java.sql.Connection ; 27 import javax.annotation.Resource; 28 import javax.ejb.EJBException ; 29 import javax.ejb.Init ; 30 import javax.ejb.Remove ; 31 import javax.naming.InitialContext ; 32 import javax.naming.NamingException ; 33 import javax.sql.DataSource ; 34 import javax.transaction.UserTransaction ; 35 36 import org.jboss.logging.Logger; 37 import org.jboss.ejb3.Container; 38 39 44 public class BankBean implements Serializable , javax.ejb.SessionSynchronization 45 { 46 private static final Logger log = Logger.getLogger(BankBean.class); 47 48 transient public DataSource customerDb; 49 50 static final String ID = Container.ENC_CTX_NAME + "/env/org.jboss.ejb3.test.bank/id"; 51 52 @Resource String id; 53 54 String customerId = "defaultId"; 55 56 static long nextAccountId = System.currentTimeMillis(); 57 58 static long nextCustomerId = System.currentTimeMillis(); 59 60 String initialized = ""; 61 62 private String activated = ""; 63 64 private String transactionState = "failed"; 65 private String rollbackState; 66 private boolean beforeCalled = false; 67 68 public String getId() 69 { 70 return id; 71 } 72 73 public String getEnvEntryId() throws RemoteException , NamingException 74 { 75 InitialContext jndiContext = new InitialContext (); 76 String value = (String )jndiContext.lookup(ID); 77 return value; 78 } 79 80 public String createAccountId(Customer customer) throws RemoteException 81 { 82 return getId() + "." + customer.getName() + "." + (nextAccountId++); 83 } 84 85 public String createCustomerId() 86 { 87 return getId() + "." + (nextCustomerId++); 88 } 89 90 public void storeCustomerId(String customerId) 91 { 92 this.customerId = customerId; 93 } 94 95 public String retrieveCustomerId() 96 { 97 return customerId; 98 } 99 100 public String interceptCustomerId(String customerId) 101 { 102 return customerId; 103 } 104 105 public void testResource() throws Exception 106 { 107 if (customerDb == null) throw new Exception ("customerDb resource not set"); 108 Connection connection = customerDb.getConnection(); 109 connection.close(); 110 } 111 112 public void remove() 113 { 114 } 115 116 @Init 117 public void annotatedInit() 118 { 119 initialized += "YES"; 120 } 121 122 public void init() 123 { 124 initialized += "YES"; 125 } 126 127 public String isInitialized() 128 { 129 return initialized; 130 } 131 132 public String isActivated() 133 { 134 return activated; 135 } 136 137 public String getTransactionState() 138 { 139 return transactionState; 140 } 141 142 public void testTransactionTimeout() 143 { 144 try 145 { 146 Thread.sleep(2000); 147 transactionState = "ok"; 148 } 149 catch (Exception e) 150 { 151 e.printStackTrace(); 152 } 153 } 154 155 public void afterBegin() throws EJBException , RemoteException 156 { 157 rollbackState = transactionState; 158 } 159 160 public void beforeCompletion() throws EJBException , RemoteException 161 { 162 beforeCalled = true; 163 } 164 165 public void afterCompletion(boolean committed) throws EJBException , RemoteException 166 { 167 if (!committed) 168 transactionState = rollbackState; 169 } 170 } 171 172 231 | Popular Tags |