1 25 26 package org.objectweb.jonas.stests.bank; 27 28 import java.rmi.RemoteException ; 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 32 import javax.ejb.CreateException ; 33 import javax.ejb.EJBException ; 34 import javax.ejb.FinderException ; 35 import javax.ejb.RemoveException ; 36 import javax.ejb.SessionBean ; 37 import javax.ejb.SessionContext ; 38 import javax.ejb.SessionSynchronization ; 39 import javax.ejb.TransactionRolledbackLocalException ; 40 import javax.naming.Context ; 41 import javax.naming.InitialContext ; 42 import javax.naming.NamingException ; 43 import javax.rmi.PortableRemoteObject ; 44 45 import org.objectweb.jonas.common.Log; 46 import org.objectweb.util.monolog.api.BasicLevel; 47 import org.objectweb.util.monolog.api.Logger; 48 49 53 public class ManagerSF implements SessionBean , SessionSynchronization { 54 55 protected static Logger history = null; 56 SessionContext ejbContext; 57 AccountLocalHome accountLocalHome = null; 58 int initialValue; 59 60 64 75 public void setSessionContext(SessionContext ctx) { 76 if (history == null) { 77 history = Log.getLogger("org.objectweb.jonas_tests.history"); 78 } 79 history.log(BasicLevel.DEBUG, ""); 80 ejbContext = ctx; 81 } 82 83 92 public void ejbRemove() { 93 history.log(BasicLevel.DEBUG, ""); 94 } 95 96 101 public void ejbCreate(int ival) throws CreateException { 102 history.log(BasicLevel.DEBUG, ""); 103 104 try { 106 Context ictx = new InitialContext (); 107 accountLocalHome = (AccountLocalHome) ictx.lookup("java:comp/env/ejb/bank"); 108 } catch (NamingException e) { 109 history.log(BasicLevel.ERROR, "Cannot get AccountLocalHome:" + e); 110 throw new CreateException ("Cannot get AccountLocalHome"); 111 } 112 113 initialValue = ival; 114 } 115 116 121 public void ejbCreate(int ival, boolean prefetch) throws CreateException { 122 history.log(BasicLevel.DEBUG, ""); 123 124 try { 126 Context ictx = new InitialContext (); 127 String ejblink = prefetch ? "java:comp/env/ejb/bankpf" : "java:comp/env/ejb/bank"; 128 accountLocalHome = (AccountLocalHome) ictx.lookup(ejblink); 129 } catch (NamingException e) { 130 history.log(BasicLevel.ERROR, "Cannot get AccountLocalHome:" + e); 131 throw new CreateException ("Cannot get AccountLocalHome"); 132 } 133 134 initialValue = ival; 135 } 136 137 141 public void ejbPassivate() { 142 history.log(BasicLevel.DEBUG, ""); 143 } 144 145 150 public void ejbActivate() { 151 history.log(BasicLevel.DEBUG, ""); 152 } 153 154 158 public void afterBegin() { 159 history.log(BasicLevel.DEBUG, ""); 160 } 161 162 public void beforeCompletion() { 163 history.log(BasicLevel.DEBUG, ""); 164 } 165 166 public void afterCompletion(boolean committed) { 167 if (committed) { 168 history.log(BasicLevel.DEBUG, "TX committed"); 169 } else { 170 history.log(BasicLevel.DEBUG, "TX rolled back"); 171 } 172 } 173 174 178 182 public void createAll(int nb) throws RemoteException { 183 try { 185 accountLocalHome.findByNum(nb - 1); 186 } catch (Exception e) { 187 for (int i = 0; i < nb; i++) { 189 newAccount(i); 190 } 191 } 192 } 193 194 197 public void reinitAll() throws RemoteException { 198 try { 199 Collection coll = accountLocalHome.findAll(); 200 for (Iterator it = coll.iterator(); it.hasNext();) { 201 AccountLocal a = (AccountLocal) it.next(); 202 a.setBalance(initialValue); 203 } 204 } catch (Exception e) { 205 history.log(BasicLevel.ERROR, "reinitAll:" + e); 206 } 207 } 208 209 210 214 public void delAccount(int d1) throws RemoteException , RemoveException { 215 try { 216 AccountLocal deb1 = accountLocalHome.findByNum(d1); 217 deb1.remove(); 218 history.log(BasicLevel.DEBUG, d1 + "\tREMOVED"); 219 } catch (FinderException e) { 220 history.log(BasicLevel.INFO, d1 + "\tNot Found for remove"); 221 } 222 } 223 224 228 public boolean checkAll() throws RemoteException { 229 int count = 0; 230 int total = 0; 231 try { 232 Collection coll = accountLocalHome.findAll(); 233 for (Iterator it = coll.iterator(); it.hasNext();) { 234 count++; 235 AccountLocal a = (AccountLocal) it.next(); 236 int balance = a.getBalance(); 237 if (balance < 0) { 238 history.log(BasicLevel.ERROR, "checkAllAccounts: bad balance: " + balance); 239 return false; 240 } 241 String name = a.getName(); 242 history.log(BasicLevel.DEBUG, name + " : FINAL BALANCE=" + balance); 243 total += balance; 244 } 245 } catch (Exception e) { 246 history.log(BasicLevel.ERROR, "checkAllAccounts:" + e); 247 return false; 248 } 249 int exp = initialValue * count; 250 if (total != exp) { 251 history.log(BasicLevel.ERROR, "checkAllAccounts: bad total: " + total + " (expected: " + exp + ")"); 252 return false; 253 } 254 history.log(BasicLevel.DEBUG, "CheckAll OK"); 255 return true; 256 } 257 258 263 public boolean checkAccount(int a) throws RemoteException { 264 boolean ret = false; 265 AccountLocal m = null; 266 267 Exception exc = null; 270 int retry; 271 for (retry = 0; retry < 20; retry++) { 272 try { 273 history.log(BasicLevel.DEBUG, "\ta_" + a + "\tCHECKED try #" + retry); 274 m = accountLocalHome.findByNum(a); 275 int b = m.getBalance(); 276 if (b >= 0) { 277 ret = true; 278 } else { 279 history.log(BasicLevel.WARN, "bad balance=" + b); 280 } 281 return ret; 282 } catch (Exception e) { 283 exc = e; 284 history.log(BasicLevel.DEBUG, "retrying " + retry); 285 sleep(retry + 1); 286 } 287 } 288 history.log(BasicLevel.WARN, "cannot check account: " + exc); 289 return ret; 290 } 291 292 297 public int readBalanceTx(int a) throws RemoteException { 298 return readBalance(a); 299 } 300 301 306 public int readBalance(int a) throws RemoteException { 307 int ret; 308 try { 309 ret = getAccount(a).getBalance(); 310 } catch (Exception e) { 311 history.log(BasicLevel.ERROR, "Cannot read balance for " + a + ": " + e); 312 throw new RemoteException ("Cannot read balance for " + a); 313 } 314 history.log(BasicLevel.DEBUG, "READ " + a + " = " + ret); 315 return ret; 316 } 317 318 325 public void move(int d, int c, int v, int delay) throws RemoteException { 326 history.log(BasicLevel.DEBUG, "MOVE " + v + " from " + d + " to " + c); 327 try { 328 AccountLocal cred = getAccount(c); 329 AccountLocal deb = getAccount(d); 330 cred.credit(v); 331 sleep(delay); 332 deb.debit(v); 333 } catch (TransactionRolledbackLocalException e) { 334 history.log(BasicLevel.WARN, "move: Rollback transaction"); 335 return; 336 } catch (EJBException e) { 337 history.log(BasicLevel.ERROR, "Cannot move:" + e); 338 return; 339 } 340 } 341 342 346 352 private AccountLocal newAccount(int i) throws RemoteException { 353 AccountLocal ml = null; 354 try { 355 ml = accountLocalHome.create(i, initialValue); 356 history.log(BasicLevel.DEBUG, "New Account\t" + i); 357 } catch (CreateException e) { 358 try { 360 ml = accountLocalHome.findByNum(i); 361 history.log(BasicLevel.WARN, "Account was created already : " + i); 362 } catch (FinderException e2) { 363 history.log(BasicLevel.ERROR, "newAccount:" + e); 364 throw new RemoteException ("Cannot create Account", e); 365 } 366 } 367 return ml; 368 } 369 370 374 private AccountLocal getAccount(int c1) throws RemoteException { 375 AccountLocal cred1; 376 try { 377 cred1 = accountLocalHome.findByNum(c1); 378 } catch (FinderException e) { 379 cred1 = newAccount(c1); 380 } 381 return cred1; 382 } 383 384 388 private void sleep(int n) { 389 try { 390 Thread.sleep(1000 * n); 391 } catch (InterruptedException e) { 392 } 393 } 394 395 } 396 | Popular Tags |