1 25 26 package org.objectweb.jonas.jtests.beans.bank; 27 28 import java.rmi.NoSuchObjectException ; 29 import java.rmi.RemoteException ; 30 import java.util.Collection ; 31 import java.util.Iterator ; 32 33 import javax.ejb.CreateException ; 34 import javax.ejb.EJBException ; 35 import javax.ejb.FinderException ; 36 import javax.ejb.NoSuchObjectLocalException ; 37 import javax.ejb.RemoveException ; 38 import javax.ejb.SessionBean ; 39 import javax.ejb.SessionContext ; 40 import javax.ejb.SessionSynchronization ; 41 import javax.ejb.TransactionRolledbackLocalException ; 42 import javax.naming.Context ; 43 import javax.naming.InitialContext ; 44 import javax.naming.NamingException ; 45 import javax.rmi.PortableRemoteObject ; 46 47 import org.objectweb.jonas.common.Log; 48 import org.objectweb.util.monolog.api.BasicLevel; 49 import org.objectweb.util.monolog.api.Logger; 50 51 55 public class ManagerSF implements SessionBean , SessionSynchronization { 56 57 protected static Logger history = null; 58 SessionContext ejbContext; 59 AccountLocalHome accountLocalHome = null; 60 AccountLocal last = null; 61 int initialValue; 62 63 67 78 public void setSessionContext(SessionContext ctx) { 79 if (history == null) { 80 history = Log.getLogger("org.objectweb.jonas_tests.history"); 81 } 82 history.log(BasicLevel.DEBUG, ""); 83 ejbContext = ctx; 84 } 85 86 95 public void ejbRemove() { 96 history.log(BasicLevel.DEBUG, ""); 97 } 98 99 104 public void ejbCreate(int ival) throws CreateException { 105 history.log(BasicLevel.DEBUG, ""); 106 107 try { 109 Context ictx = new InitialContext (); 110 accountLocalHome = (AccountLocalHome) ictx.lookup("java:comp/env/ejb/bank"); 111 } catch (NamingException e) { 112 history.log(BasicLevel.ERROR, "Cannot get AccountLocalHome:" + e); 113 throw new CreateException ("Cannot get AccountLocalHome"); 114 } 115 116 initialValue = ival; 117 } 118 119 124 public void ejbCreate(int ival, boolean prefetch) throws CreateException { 125 history.log(BasicLevel.DEBUG, ""); 126 127 try { 129 Context ictx = new InitialContext (); 130 String ejblink = prefetch ? "java:comp/env/ejb/bankpf" : "java:comp/env/ejb/bank"; 131 accountLocalHome = (AccountLocalHome) ictx.lookup(ejblink); 132 } catch (NamingException e) { 133 history.log(BasicLevel.ERROR, "Cannot get AccountLocalHome:" + e); 134 throw new CreateException ("Cannot get AccountLocalHome"); 135 } 136 137 initialValue = ival; 138 } 139 140 144 public void ejbPassivate() { 145 history.log(BasicLevel.DEBUG, ""); 146 } 147 148 153 public void ejbActivate() { 154 history.log(BasicLevel.DEBUG, ""); 155 } 156 157 161 public void afterBegin() { 162 history.log(BasicLevel.DEBUG, ""); 163 } 164 165 public void beforeCompletion() { 166 history.log(BasicLevel.DEBUG, ""); 167 } 168 169 public void afterCompletion(boolean committed) { 170 if (committed) { 171 history.log(BasicLevel.DEBUG, "TX committed"); 172 } else { 173 history.log(BasicLevel.DEBUG, "TX rolled back"); 174 } 175 } 176 177 181 185 public void createAll(int nb) throws RemoteException { 186 try { 188 accountLocalHome.findByNum(nb - 1); 189 } catch (Exception e) { 190 for (int i = 0; i < nb; i++) { 192 newAccount(i); 193 } 194 } 195 } 196 197 200 public void reinitAll() throws RemoteException { 201 try { 202 Collection coll = accountLocalHome.findAll(); 203 for (Iterator it = coll.iterator(); it.hasNext();) { 204 AccountLocal a = (AccountLocal) it.next(); 205 a.setBalance(initialValue); 206 } 207 } catch (Exception e) { 208 history.log(BasicLevel.ERROR, "reinitAll:" + e); 209 } 210 } 211 212 213 217 public void delAccount(int d1) throws RemoteException , RemoveException { 218 try { 219 AccountLocal deb1 = accountLocalHome.findByNum(d1); 220 deb1.remove(); 221 history.log(BasicLevel.DEBUG, d1 + "\tREMOVED"); 222 } catch (FinderException e) { 223 history.log(BasicLevel.INFO, d1 + "\tNot Found for remove"); 224 } 225 } 226 227 231 public boolean checkAll() throws RemoteException { 232 int count = 0; 233 int total = 0; 234 try { 235 Collection coll = accountLocalHome.findAll(); 236 for (Iterator it = coll.iterator(); it.hasNext();) { 237 count++; 238 AccountLocal a = (AccountLocal) it.next(); 239 int balance = a.getBalance(); 240 if (balance < 0) { 241 history.log(BasicLevel.ERROR, "checkAllAccounts: bad balance: " + balance); 242 return false; 243 } 244 String name = a.getName(); 245 history.log(BasicLevel.DEBUG, name + " : FINAL BALANCE=" + balance); 246 total += balance; 247 } 248 } catch (Exception e) { 249 history.log(BasicLevel.ERROR, "checkAllAccounts:" + e); 250 return false; 251 } 252 int exp = initialValue * count; 253 if (total != exp) { 254 history.log(BasicLevel.ERROR, "checkAllAccounts: bad total: " + total + " (expected: " + exp + ")"); 255 return false; 256 } 257 history.log(BasicLevel.DEBUG, "CheckAll OK"); 258 return true; 259 } 260 261 266 public boolean checkAccount(int a) throws RemoteException { 267 boolean ret = false; 268 AccountLocal m = null; 269 270 Exception exc = null; 273 int retry; 274 for (retry = 0; retry < 20; retry++) { 275 try { 276 history.log(BasicLevel.DEBUG, "\ta_" + a + "\tCHECKED try #" + retry); 277 m = accountLocalHome.findByNum(a); 278 int b = m.getBalance(); 279 if (b >= 0) { 280 ret = true; 281 } else { 282 history.log(BasicLevel.WARN, "bad balance=" + b); 283 } 284 return ret; 285 } catch (Exception e) { 286 exc = e; 287 history.log(BasicLevel.DEBUG, "retrying " + retry); 288 sleep(retry + 1); 289 } 290 } 291 history.log(BasicLevel.WARN, "cannot check account: " + exc); 292 return ret; 293 } 294 295 300 public int readBalanceTx(int a) throws RemoteException { 301 return readBalance(a); 302 } 303 304 309 public int readBalance(int a) throws RemoteException { 310 int ret; 311 try { 312 ret = getAccount(a).getBalance(); 313 } catch (Exception e) { 314 history.log(BasicLevel.ERROR, "Cannot read balance for " + a + ": " + e); 315 throw new RemoteException ("Cannot read balance for " + a); 316 } 317 history.log(BasicLevel.DEBUG, "READ " + a + " = " + ret); 318 return ret; 319 } 320 321 328 public void move(int d, int c, int v, int delay) throws RemoteException { 329 history.log(BasicLevel.DEBUG, "MOVE " + v + " from " + d + " to " + c); 330 try { 331 AccountLocal cred = getAccount(c); 332 AccountLocal deb = getAccount(d); 333 cred.credit(v); 334 sleep(delay); 335 deb.debit(v); 336 } catch (TransactionRolledbackLocalException e) { 337 history.log(BasicLevel.WARN, "move: Rollback transaction"); 338 return; 339 } catch (EJBException e) { 340 history.log(BasicLevel.ERROR, "Cannot move:" + e); 341 return; 342 } 343 } 344 345 348 public int readBalance() throws RemoteException { 349 int ret; 350 try { 351 ret = last.getBalance(); 352 } catch (NoSuchObjectLocalException e) { 353 throw new NoSuchObjectException ("Account destroyed"); 354 } catch (Exception e) { 355 throw new RemoteException ("Cannot read last balance"); 356 } 357 return ret; 358 } 359 360 364 370 private AccountLocal newAccount(int i) throws RemoteException { 371 AccountLocal ml = null; 372 try { 373 ml = accountLocalHome.create(i, initialValue); 374 history.log(BasicLevel.DEBUG, "New Account\t" + i); 375 } catch (CreateException e) { 376 try { 378 ml = accountLocalHome.findByNum(i); 379 history.log(BasicLevel.WARN, "Account was created already : " + i); 380 } catch (FinderException e2) { 381 history.log(BasicLevel.ERROR, "newAccount:" + e); 382 throw new RemoteException ("Cannot create Account", e); 383 } 384 } 385 return ml; 386 } 387 388 392 private AccountLocal getAccount(int c1) throws RemoteException { 393 try { 394 last = accountLocalHome.findByNum(c1); 395 } catch (FinderException e) { 396 last = newAccount(c1); 397 } 398 return last; 399 } 400 401 405 private void sleep(int n) { 406 try { 407 Thread.sleep(1000 * n); 408 } catch (InterruptedException e) { 409 } 410 } 411 412 } 413 | Popular Tags |