1 25 26 package org.objectweb.jonas_ejb.container; 27 28 import java.rmi.RemoteException ; 29 30 import javax.ejb.EJBException ; 31 import javax.ejb.EJBLocalObject ; 32 import javax.ejb.EJBObject ; 33 import javax.ejb.EntityBean ; 34 import javax.ejb.EntityContext ; 35 import javax.ejb.NoSuchObjectLocalException ; 36 import javax.ejb.RemoveException ; 37 import javax.ejb.TimerService ; 38 import javax.transaction.Status ; 39 import javax.transaction.Synchronization ; 40 import javax.transaction.SystemException ; 41 import javax.transaction.Transaction ; 42 43 import org.objectweb.jonas_ejb.deployment.api.EntityDesc; 44 45 import org.objectweb.util.monolog.api.BasicLevel; 46 47 55 public class JEntityContext extends JContext implements EntityContext , Synchronization { 56 57 60 private boolean dirty = false; 61 62 66 private boolean initialized = false; 67 68 71 Transaction beanCoord = null; 72 73 private boolean mustnotifywriting = false; 74 75 private JEntitySwitch bs = null; 76 77 80 boolean ismarkedremoved; 81 82 85 boolean isnewinstance = false; 86 87 91 96 public JEntityContext(JEntityFactory bf, EntityBean eb) { 97 super(bf, eb); 98 } 99 100 104 110 public TimerService getTimerService() throws IllegalStateException { 111 int mystate = getState(); 112 if (TraceEjb.isDebugIc()) { 113 TraceEjb.interp.log(BasicLevel.DEBUG, "" + mystate); 114 } 115 switch (mystate) { 116 case 0: 117 TraceEjb.logger.log(BasicLevel.ERROR, "not allowed here"); 118 throw new IllegalStateException ("getTimerService not allowed here"); 119 case 4: 120 TraceEjb.logger.log(BasicLevel.ERROR, "not allowed here"); 121 throw new IllegalStateException ("getTimerService not allowed here"); 122 case 1: 123 return bf.getTimerService(); 127 default: 128 return bs == null ? bf.getTimerService() : bs.getEntityTimerService(); 129 } 130 } 131 132 136 144 public EJBObject getEJBObject() throws IllegalStateException { 145 if (TraceEjb.isDebugIc()) { 146 TraceEjb.interp.log(BasicLevel.DEBUG, ""); 147 } 148 if (ismarkedremoved) { 149 TraceEjb.logger.log(BasicLevel.ERROR, "marked removed"); 150 throw new IllegalStateException ("EJB is removed"); 151 } 152 if (bs == null) { 153 TraceEjb.logger.log(BasicLevel.ERROR, "no EntitySwitch"); 154 throw new IllegalStateException ("no EntitySwitch"); 155 } 156 EJBObject ejbobject = bs.getRemote(); 157 if (ejbobject == null) { 158 throw new IllegalStateException ("No Remote Interface for this bean"); 159 } 160 return ejbobject; 161 } 162 163 173 private EJBLocalObject getEJBLocalObject(boolean check) throws IllegalStateException { 174 if (TraceEjb.isDebugIc()) { 175 TraceEjb.interp.log(BasicLevel.DEBUG, ""); 176 } 177 if (check) { 178 if (!bf.dd.hasDefinedLocalInterface()) { 179 TraceEjb.logger.log(BasicLevel.ERROR, "No Local Interface declared for this bean"); 180 throw new IllegalStateException ("No Local Interface declared for this bean"); 181 } 182 } 183 if (ismarkedremoved) { 184 TraceEjb.logger.log(BasicLevel.ERROR, "marked removed"); 185 throw new IllegalStateException ("EJB is removed"); 186 } 187 if (bs == null) { 188 TraceEjb.logger.log(BasicLevel.ERROR, "no EntitySwitch"); 189 throw new IllegalStateException ("no EntitySwitch"); 190 } 191 EJBLocalObject ejblocalobject = bs.getLocal(); 192 if (ejblocalobject == null) { 193 throw new IllegalStateException ("No Local Object for this bean"); 194 } 195 return ejblocalobject; 196 } 197 198 207 public EJBLocalObject getEJBLocalObject() throws IllegalStateException { 208 return getEJBLocalObject(true); 209 } 210 211 221 public EJBLocalObject get2EJBLocalObject() throws IllegalStateException { 222 return getEJBLocalObject(false); 223 } 224 225 233 public Object getPrimaryKey() throws IllegalStateException { 234 if (TraceEjb.isDebugIc()) { 235 TraceEjb.interp.log(BasicLevel.DEBUG, ""); 236 } 237 if (ismarkedremoved) { 238 TraceEjb.logger.log(BasicLevel.ERROR, "marked removed"); 239 throw new IllegalStateException ("EJB is removed"); 240 } 241 if (bs == null) { 242 TraceEjb.logger.log(BasicLevel.ERROR, "no EntitySwitch"); 243 throw new IllegalStateException ("no EntitySwitch"); 244 } 245 return bs.getPrimaryKey(); 246 } 247 248 252 255 public void beforeCompletion() { 256 if (TraceEjb.isDebugContext()) { 257 TraceEjb.context.log(BasicLevel.DEBUG, ""); 258 } 259 260 if (ismarkedremoved) { 262 if (TraceEjb.isDebugContext()) { 263 TraceEjb.context.log(BasicLevel.DEBUG, "ismarkedremoved -> no ejbStore"); 264 } 265 return; 266 } 267 268 if (beanCoord == null) { 269 TraceEjb.logger.log(BasicLevel.ERROR, "no tx"); 271 } 272 273 if (isnewinstance) { 276 try { 277 if (getPrimaryKey() == null) { 278 return; 283 } 284 } catch (IllegalStateException e) { 285 return; 286 } 287 } 288 289 try { 290 storeIfModified(); 291 } catch (EJBException e) { 292 abortTransaction(); 294 } 295 } 296 297 301 public void afterCompletion(int status) { 302 303 boolean committed = (status == Status.STATUS_COMMITTED); 304 if (TraceEjb.isDebugContext()) { 305 if (committed) { 306 TraceEjb.context.log(BasicLevel.DEBUG, "committed"); 307 } else { 308 TraceEjb.context.log(BasicLevel.DEBUG, "rolledback"); 309 } 310 } 311 312 if (beanCoord == null) { 314 TraceEjb.logger.log(BasicLevel.ERROR, "no tx for " + this); 316 return; 317 } 318 if (bs == null) { 320 TraceEjb.logger.log(BasicLevel.ERROR, "Context without EntitySwitch reference"); 322 throw new EJBException ("Context with no Entity Switch"); 323 } 324 325 bs.txCompleted(beanCoord, committed); 329 } 330 331 335 338 public void razEntityContext() { 339 if (TraceEjb.isDebugContext()) { 340 TraceEjb.context.log(BasicLevel.DEBUG, ""); 341 } 342 bs = null; 343 beanCoord = null; 344 ismarkedremoved = false; 345 isnewinstance = false; 346 initialized = false; 347 } 348 349 352 public void detachTx() { 353 if (TraceEjb.isDebugContext()) { 354 TraceEjb.context.log(BasicLevel.DEBUG, ""); 355 } 356 beanCoord = null; 357 ismarkedremoved = false; 358 isnewinstance = false; 359 } 360 361 365 public void initEntityContext(JEntitySwitch bs) { 366 if (TraceEjb.isDebugContext()) { 367 TraceEjb.context.log(BasicLevel.DEBUG, ""); 368 } 369 this.bs = bs; 370 initialized = true; 371 } 372 373 public void setRunningTx(Transaction tx) { 374 if (TraceEjb.isDebugContext()) { 375 TraceEjb.context.log(BasicLevel.DEBUG, ""); 376 } 377 beanCoord = tx; 378 } 379 380 385 public void reuseEntityContext(boolean newtrans) { 386 if (TraceEjb.isDebugContext()) { 387 TraceEjb.context.log(BasicLevel.DEBUG, ""); 388 } 389 390 if (ismarkedremoved) { 392 TraceEjb.context.log(BasicLevel.WARN, "Try to access a deleted object"); 393 throw new NoSuchObjectLocalException ("Instance has been removed"); 394 } 395 if (newtrans) { 396 isnewinstance = false; 397 } 398 if (bs == null) { 399 TraceEjb.logger.log(BasicLevel.ERROR, "reuse Entity Context with no Entity Switch reference"); 400 } 401 } 402 403 406 public void setNewInstance() { 407 isnewinstance = true; 408 } 409 410 416 public void setRemoved() throws RemoteException , RemoveException { 417 if (TraceEjb.isDebugContext()) { 418 TraceEjb.context.log(BasicLevel.DEBUG, ""); 419 } 420 EntityBean eb = (EntityBean ) instance; 421 422 if (instance == null) { 423 TraceEjb.logger.log(BasicLevel.ERROR, "null instance!"); 424 return; 425 } 426 eb.ejbRemove(); 428 ismarkedremoved = true; 431 } 432 433 437 public boolean isMarkedRemoved() { 438 return ismarkedremoved; 439 } 440 441 445 public boolean isNewInstance() { 446 return isnewinstance; 447 } 448 449 455 public EntityBean getInstance() throws RemoteException { 457 if (instance == null) { 458 TraceEjb.logger.log(BasicLevel.ERROR, "null!"); 459 throw new RemoteException ("No instance available"); 460 } 461 return (EntityBean ) instance; 462 } 463 464 468 public JEntityFactory getEntityFactory() { 469 return (JEntityFactory) bf; 470 } 471 472 476 public JEntitySwitch getEntitySwitch() { 477 return (JEntitySwitch) bs; 478 } 479 480 484 public void setEntitySwitch(JEntitySwitch bs) { 485 this.bs = bs; 486 ismarkedremoved = false; 487 mustnotifywriting = (bs.getPolicy() == EntityDesc.LOCK_CONTAINER_READ_UNCOMMITTED || bs.getPolicy() == EntityDesc.LOCK_DATABASE); 488 } 489 490 493 public boolean isDirty() { 494 return dirty; 495 } 496 497 500 public void setDirty(boolean d) { 501 if (TraceEjb.isDebugContext() && d) { 502 TraceEjb.context.log(BasicLevel.DEBUG, "mustnotifywriting=" + mustnotifywriting); 503 TraceEjb.context.log(BasicLevel.DEBUG, "dirty=" + dirty); 504 } 505 if (mustnotifywriting && d && !dirty && initialized) { 507 Transaction tx = null; 509 try { 510 tx = tm.getTransaction(); 511 } catch (SystemException e) { 512 TraceEjb.context.log(BasicLevel.ERROR, "getTransaction failed", e); 513 } 514 if (tx == null) { 515 if (TraceEjb.isDebugContext()) { 516 TraceEjb.context.log(BasicLevel.DEBUG, "Try to modify bean outside any transaction"); 517 } 518 } else { 519 bs.notifyWriting(tx, this); 520 } 521 } 522 dirty = d; 523 } 524 525 528 public void storeIfModified() { 529 530 EntityBean eb = (EntityBean ) instance; 531 532 if (ismarkedremoved) { 533 if (TraceEjb.isDebugContext()) { 535 TraceEjb.context.log(BasicLevel.DEBUG, "marked removed"); 536 } 537 return; 538 } 539 if (TraceEjb.isDebugContext()) { 540 TraceEjb.context.log(BasicLevel.DEBUG, ""); 541 } 542 543 try { 544 eb.ejbStore(); 548 } catch (RemoteException e) { 549 throw new EJBException ("Exception while storing data", e); 550 } catch (EJBException e) { 551 TraceEjb.logger.log(BasicLevel.ERROR, "raised EJBException ", e); 552 throw e; 553 } catch (RuntimeException e) { 554 TraceEjb.logger.log(BasicLevel.ERROR, "runtime exception: ", e); 555 throw new EJBException ("Exception while storing data", e); 556 } catch (Error e) { 557 TraceEjb.logger.log(BasicLevel.ERROR, "error: ", e); 558 throw new EJBException ("Error while storing data"); 559 } 560 } 561 562 565 public void passivate() { 566 567 if (TraceEjb.isDebugContext()) { 568 TraceEjb.context.log(BasicLevel.DEBUG, ""); 569 } 570 571 EntityBean eb = (EntityBean ) instance; 572 setState(1); 573 574 try { 575 eb.ejbPassivate(); 576 } catch (Exception e) { 577 TraceEjb.logger.log(BasicLevel.ERROR, "ejbPassivate failed", e); 578 } catch (Error e) { 579 TraceEjb.logger.log(BasicLevel.ERROR, "ejbPassivate error", e); 580 } 581 } 582 583 587 public void activate(boolean doactivate) { 588 if (TraceEjb.isDebugContext()) { 589 TraceEjb.context.log(BasicLevel.DEBUG, ""); 590 } 591 592 EntityBean eb = (EntityBean ) instance; 593 594 try { 595 if (doactivate) { 596 setState(1); 597 eb.ejbActivate(); 599 } 600 setState(2); 601 eb.ejbLoad(); 603 } catch (RemoteException e) { 604 TraceEjb.logger.log(BasicLevel.ERROR, "remote exception: ", e); 605 throw new EJBException ("Cannot activate bean", e); 606 } catch (RuntimeException e) { 607 TraceEjb.logger.log(BasicLevel.ERROR, "runtime exception: ", e); 608 throw new EJBException ("Cannot activate bean", e); 609 } catch (Error e) { 610 TraceEjb.logger.log(BasicLevel.ERROR, "error: ", e); 611 throw new EJBException ("Cannot activate bean"); 612 } 613 } 614 615 619 623 private void abortTransaction() { 624 if (TraceEjb.isDebugContext()) { 625 TraceEjb.context.log(BasicLevel.DEBUG, ""); 626 } 627 628 if (beanCoord != null) { 630 try { 631 beanCoord.setRollbackOnly(); 632 } catch (SystemException e) { 633 TraceEjb.logger.log(BasicLevel.ERROR, "cannot setRollbackOnly", e); 634 } 635 } 636 } 637 638 } 639 | Popular Tags |