1 23 package com.sun.ejb.containers; 24 25 import java.lang.reflect.Method ; 26 import java.io.*; 27 import java.rmi.RemoteException ; 28 29 import java.util.*; 30 import java.util.logging.*; 31 import java.lang.reflect.Proxy ; 32 import java.lang.reflect.InvocationHandler ; 33 34 import javax.ejb.*; 35 import javax.transaction.*; 36 37 import com.sun.appserv.util.cache.Constants; 38 import com.sun.appserv.util.cache.Cache; 39 import com.sun.appserv.util.cache.BaseCache; 40 import com.sun.appserv.util.cache.LruCache; 41 import com.sun.appserv.util.cache.CacheListener; 42 43 import com.sun.ejb.*; 44 import com.sun.ejb.portable.ObjrefEnumeration; 45 import com.sun.ejb.containers.util.pool.*; 46 import com.sun.ejb.containers.util.cache.EJBObjectCache; 47 import com.sun.ejb.containers.util.cache.EJBObjectCacheListener; 48 import com.sun.ejb.containers.util.cache.FIFOEJBObjectCache; 49 import com.sun.ejb.containers.util.cache.UnboundedEJBObjectCache; 50 import com.sun.ejb.containers.util.ContainerWorkPool; 51 52 import com.sun.enterprise.*; 53 import com.sun.enterprise.deployment.*; 54 import com.sun.enterprise.deployment.runtime.IASEjbExtraDescriptors; 55 import com.sun.enterprise.deployment.runtime.BeanCacheDescriptor; 56 import com.sun.enterprise.deployment.runtime.BeanPoolDescriptor; 57 import com.sun.enterprise.util.LocalStringManagerImpl; 58 import com.sun.enterprise.log.Log; 59 import com.sun.enterprise.appverification.factory.AppVerification; 60 import com.sun.enterprise.admin.monitor.callflow.ComponentType; 61 62 import com.sun.logging.*; 63 64 import com.sun.enterprise.admin.monitor.*; 65 import com.sun.enterprise.config.ConfigException; 66 import com.sun.enterprise.config.serverbeans.*; 67 import com.sun.enterprise.server.ServerContext; 68 import com.sun.enterprise.server.ApplicationServer; 69 import com.sun.enterprise.util.io.FileUtils; 70 71 import com.sun.ejb.spi.stats.EntityBeanStatsProvider; 72 73 import com.sun.ejb.spi.container.BeanStateSynchronization; 74 75 import com.sun.enterprise.distributedtx.J2EETransaction; 76 77 144 145 public class EntityContainer 146 extends BaseContainer 147 implements CacheListener, EntityBeanStatsProvider 148 { 149 protected static Logger _logger; 150 static { 151 _logger=LogDomains.getLogger(LogDomains.EJB_LOGGER); 152 153 } 154 155 private ThreadLocal ejbServant = new ThreadLocal () { 156 protected Object initialValue() { 157 return null; 158 } 159 }; 160 protected static LocalStringManagerImpl localStrings = 161 new LocalStringManagerImpl(EntityContainer.class); 162 163 static final int POOLED=1, READY=2, INVOKING=3, 164 INCOMPLETE_TX=4, DESTROYED=5; 165 protected static final int HIGH_WATER_MARK=100; 166 167 private static final int DEFAULT_TX_CACHE_BUCKETS = 16; 168 169 protected EJBObjectCache ejbObjectStore; 172 173 protected EJBObjectCache ejbLocalObjectStore; 176 177 protected Stack passivationCandidates = new Stack(); 179 180 protected Cache readyStore; 182 183 protected AbstractPool entityCtxPool; 185 186 protected boolean isReentrant; 187 protected boolean isContainerManagedPers; 188 protected Hashtable txBeanTable; 189 190 protected final float DEFAULT_LOAD_FACTOR = 0.75f; 191 protected final int DEFAULT_CACHE_SIZE = 8192; 192 protected int _maxBuckets = 8; 193 194 protected IASEjbExtraDescriptors iased = null; 195 protected BeanCacheDescriptor beanCacheDes = null; 196 protected BeanPoolDescriptor beanPoolDes = null; 197 protected Server svr = null; 198 protected Config cfg = null; 199 protected EjbContainer ejbContainer = null; 200 boolean largeCache = false; 201 202 CacheProperties cacheProp = null; 203 PoolProperties poolProp = null; 204 Object asyncTaskSemaphore = new Object (); 205 boolean addedASyncTask = false; 206 207 protected IdleBeansPassivator idleEJBObjectPassivator; 209 protected IdleBeansPassivator idleLocalEJBObjectPassivator; 210 protected boolean defaultCacheEJBO = true; 211 212 IdleBeansPassivator idleBeansPassivator; 213 boolean timerValid = true; 214 long idleTimeout; 215 216 217 protected int ejboRemoved; 218 219 protected int totalPassivations; 220 protected int totalPassivationErrors; 221 222 private EntityCacheStatsProvider cacheStatsProvider; 223 224 static { 225 _logger.log(Level.FINE," Loading Entitycontainer..."); 226 } 227 228 232 protected EntityContainer(EjbDescriptor desc, ClassLoader loader) 233 throws Exception 234 { 235 super(desc, loader); 236 EjbEntityDescriptor ed = (EjbEntityDescriptor)desc; 237 isReentrant = ed.isReentrant(); 238 if ( ed.getPersistenceType().equals( 239 EjbEntityDescriptor.BEAN_PERSISTENCE) ) { 240 isContainerManagedPers = false; 241 } else { 242 isContainerManagedPers = true; 243 } 244 245 iased = ed.getIASEjbExtraDescriptors(); 246 if( iased != null) { 247 beanCacheDes = iased.getBeanCache(); 248 beanPoolDes = iased.getBeanPool(); 249 } 250 try { 251 ServerContext sc = ApplicationServer.getServerContext(); 252 cfg = ServerBeansFactory.getConfigBean(sc.getConfigContext()); 255 } catch (ConfigException ex) { 256 _logger.log(Level.WARNING, "ejb.entitycontainer_exception", ex); 257 } 258 ejbContainer = cfg.getEjbContainer(); 261 262 super.setMonitorOn(ejbContainer.isMonitoringEnabled()); 263 264 createCaches(); 265 266 super.createCallFlowAgent( 267 isContainerManagedPers ? ComponentType.CMP : ComponentType.BMP); 268 _logger.log(Level.FINE,"[EntityContainer] Created EntityContainer: " 269 + logParams[0]); 270 } 271 272 273 278 public IdleBeansPassivator setupIdleBeansPassivator(Cache cache) 279 throws Exception { 280 281 IdleBeansPassivator idleBeansPassivator = 282 new IdleBeansPassivator(cache); 283 284 ContainerFactoryImpl.getTimer(). 285 scheduleAtFixedRate(idleBeansPassivator, idleTimeout, idleTimeout); 286 287 return idleBeansPassivator; 288 } 289 290 293 public void cancelTimerTasks() { 294 timerValid = false; 295 if (idleBeansPassivator != null) { 296 try { 297 idleBeansPassivator.cancel(); 298 idleBeansPassivator.cache = null; 299 } catch (Exception e) { 300 _logger.log(Level.FINE, "[EntityContainer] cancelTimerTask: " + 301 e); 302 } 303 } 304 305 if (idleEJBObjectPassivator != null) { 306 try { 307 idleEJBObjectPassivator.cancel(); 308 idleEJBObjectPassivator.cache = null; 309 } catch (Exception e) { 310 _logger.log(Level.FINE, "[EntityContainer] cancelTimerTask: " + 311 e); 312 } 313 } 314 315 if (idleLocalEJBObjectPassivator != null) { 316 try { 317 idleLocalEJBObjectPassivator.cancel(); 318 idleLocalEJBObjectPassivator.cache = null; 319 } catch (Exception e) { 320 _logger.log(Level.FINE, "[EntityContainer] cancelTimerTask: " + 321 e); 322 } 323 } 324 325 this.idleEJBObjectPassivator = null; 326 this.idleLocalEJBObjectPassivator = null; 327 this.idleBeansPassivator = null; 328 } 329 330 void setTxBeanTable(Hashtable t) { 331 txBeanTable = t; 332 } 333 334 337 protected void initializeHome() 338 throws Exception 339 { 340 ObjectFactory entityCtxFactory = new EntityContextFactory(this); 341 342 int steadyPoolSize = 0; 343 int resizeQuantity = 10; 344 int idleTimeoutInSeconds = Integer.MAX_VALUE-1; 345 poolProp = new PoolProperties(); 346 347 super.initializeHome(); 348 349 entityCtxPool = new NonBlockingPool(ejbDescriptor.getName(), 350 entityCtxFactory, poolProp.steadyPoolSize, 351 poolProp.poolResizeQuantity, poolProp.maxPoolSize, 352 poolProp.poolIdleTimeoutInSeconds, loader); 353 354 355 registerMonitorableComponents(); 356 } 357 358 protected void registerMonitorableComponents() { 359 registryMediator.registerProvider(this); 360 registryMediator.registerProvider(entityCtxPool); 361 if (readyStore != null) { 362 int confMaxCacheSize = cacheProp.maxCacheSize; 363 if (confMaxCacheSize <= 0) { 364 confMaxCacheSize = Integer.MAX_VALUE; 365 } 366 this.cacheStatsProvider = new EntityCacheStatsProvider( 367 (BaseCache) readyStore, confMaxCacheSize); 368 registryMediator.registerProvider(cacheStatsProvider); 369 } 370 super.registerMonitorableComponents(); 371 super.populateMethodMonitorMap(); 372 _logger.log(Level.FINE, "[Entity Container] registered monitorable"); 373 } 374 375 public void onReady() { 376 } 377 378 public String getMonitorAttributeValues() { 379 StringBuffer sbuf = new StringBuffer (); 380 appendStats(sbuf); 381 return sbuf.toString(); 382 } 383 384 public void appendStats(StringBuffer sbuf) { 385 sbuf.append("\nEntityContainer: ") 386 .append("CreateCount=").append(statCreateCount).append("; ") 387 .append("RemoveCount=").append(statRemoveCount).append("; ") 388 .append("PassQSize=") 389 .append(passivationCandidates.size()).append("]"); 390 Map stats = null; 391 if (readyStore != null) { 392 stats = readyStore.getStats(); 393 } 394 appendStat(sbuf, "ReadyStore", stats); 395 396 appendStat(sbuf, "EJBObjectStore", ejbObjectStore.getStats()); 397 appendStat(sbuf, "EJBLocalObjectStore",ejbLocalObjectStore.getStats()); 398 } 399 400 401 403 public int getMaxCacheSize() { 404 int maxSize = 0; 405 if (readyStore != null) { 406 maxSize = (cacheProp.maxCacheSize <= 0) 407 ? Integer.MAX_VALUE 408 : cacheProp.maxCacheSize; 409 } 410 411 return maxSize; 412 } 413 414 public int getSteadyPoolSize() { 415 return entityCtxPool.getSteadyPoolSize(); 416 } 417 418 public int getMaxPoolSize() { 419 return entityCtxPool.getMaxPoolSize(); 420 } 421 422 public long getPooledCount() { 423 return entityCtxPool.getSize(); 424 } 425 426 public long getReadyCount() { 427 return (readyStore == null) 428 ? 0 429 : readyStore.getEntryCount(); 430 } 431 432 433 434 private int getEjbObjectStoreSize() { 435 return ejbObjectStore.getEntryCount(); 436 } 437 438 441 EJBObjectImpl createEJBObjectImpl() 442 throws CreateException, RemoteException 443 { 444 throw new EJBException( 445 "INTERNAL ERROR: EntityContainer.createEJBObject() called"); 446 } 447 448 EJBLocalObjectImpl createEJBLocalObjectImpl() 449 throws CreateException 450 { 451 throw new EJBException( 452 "INTERNAL ERROR: EntityContainer.createEJBLocalObjectImpl() called"); 453 } 454 455 456 460 EJBObjectImpl getEJBObjectImpl(byte[] streamKey) { 461 Object primaryKey; 463 try { 464 primaryKey = EJBUtils.deserializeObject(streamKey, loader, false); 465 } catch ( Exception ex ) { 466 throw new EJBException(ex); 467 } 468 469 return internalGetEJBObjectImpl(primaryKey, streamKey); 470 } 471 472 473 477 EJBLocalObjectImpl getEJBLocalObjectImpl(Object key) { 478 return internalGetEJBLocalObjectImpl(key); 479 } 480 481 485 protected ComponentContext _getContext(Invocation inv) { 486 String name = inv.method.getName(); 487 488 if ( inv.invocationInfo.isCreateHomeFinder ) { 489 494 EntityContextImpl context = getPooledEJB(); 496 497 context.setState(INVOKING); 500 501 if ( inv.invocationInfo.startsWithCreate ) 502 preCreate(inv, context); 503 else if ( inv.invocationInfo.startsWithFind ) 504 preFind(inv, context); 505 506 507 context.setLastTransactionStatus(-1); 508 context.incrementCalls(); 509 510 return context; 511 } 512 513 516 EntityContextImpl context = null; 519 if ( willInvokeWithClientTx(inv) ) 520 context = getEJBWithIncompleteTx(inv); 521 if ( context == null ) 522 context = getReadyEJB(inv); 523 524 synchronized ( context ) { 525 if ( context.getState() == INVOKING && !isReentrant ) 526 throw new EJBException( 527 "EJB is already executing another request"); 528 if (context.getState() == POOLED || 529 context.getState() == DESTROYED) { 530 throw new EJBException("Internal error: unknown EJB state"); 533 } 534 535 context.setState(INVOKING); 536 } 537 538 context.setLastTransactionStatus(-1); 539 context.incrementCalls(); 540 context.setDirty(true); 542 543 return context; 544 } 545 546 protected boolean willInvokeWithClientTx(Invocation inv) { 547 int status = Status.STATUS_UNKNOWN; 548 try { 549 status = transactionManager.getStatus(); 550 } catch ( SystemException ex ) { 551 throw new EJBException(ex); 552 } 553 if ( status != Status.STATUS_NO_TRANSACTION ) { 554 int txAttr = inv.invocationInfo.txAttr; 555 switch (txAttr) { 556 case TX_SUPPORTS: 557 case TX_REQUIRED: 558 case TX_MANDATORY: 559 return true; 560 } 561 } 562 return false; 563 } 564 565 566 567 571 public void releaseContext(Invocation inv) { 572 EntityContextImpl context = (EntityContextImpl)inv.context; 573 boolean decrementedCalls = false; 575 if ( context.getState()==DESTROYED ) 576 return; 577 578 try { 579 if ( context.hasReentrantCall() ) { 580 if ( inv.ejbObject.isRemoved() ) { 583 removeIncompleteTxEJB(context, true); 587 588 if ( context.getEJBObjectImpl() != null ) { 591 context.getEJBObjectImpl().setRemoved(false); 593 context.setEJBObjectImpl(null); 594 context.setEJBStub(null); 595 } 596 if ( context.getEJBLocalObjectImpl() != null ) { 597 context.getEJBLocalObjectImpl().setRemoved(false); 599 context.setEJBLocalObjectImpl(null); 600 } 601 } else { 602 if ( context.getState() == INVOKING ) { 603 doFlush( inv ); 604 } 605 } 606 607 } else if ( context.getEJBObjectImpl()==null 609 && context.getEJBLocalObjectImpl()==null ) { 610 decrementedCalls = true; 618 context.decrementCalls(); 619 if (!(inv.invocationInfo.startsWithCreate)) { 620 context.setTransaction(null); 621 addPooledEJB(context); 622 }else if(context.getTransaction() == null) { 623 addPooledEJB(context); 624 } else { 625 context.setState(INCOMPLETE_TX); 629 } 630 } else if ( inv.ejbObject.isRemoved() ) { 631 removeIncompleteTxEJB(context, true); 635 if ( context.getEJBObjectImpl() != null ) 638 context.getEJBObjectImpl().setRemoved(false); 639 if ( context.getEJBLocalObjectImpl() != null ) 640 context.getEJBLocalObjectImpl().setRemoved(false); 641 642 decrementedCalls = true; 643 context.decrementCalls(); 644 if(context.getTransaction() == null) { 645 addPooledEJB(context); 646 } else { 647 context.setState(INCOMPLETE_TX); 651 } 652 653 } else if ( context.getTransaction() == null ) { 654 659 int status = context.getLastTransactionStatus(); 662 decrementedCalls = true; 663 context.decrementCalls(); 664 context.setLastTransactionStatus(-1); 665 if ( status == -1 || status == Status.STATUS_COMMITTED 666 || status == Status.STATUS_NO_TRANSACTION ) 667 addReadyEJB(context); 668 else 669 passivateAndPoolEJB(context); 670 } else { 671 context.setState(INCOMPLETE_TX); 675 676 doFlush( inv ); 677 } 678 } catch ( Exception ex ) { 679 _logger.log(Level.FINE, "ejb.release_context_exception", 680 logParams); 681 _logger.log(Level.FINE, "",ex); 682 throw new EJBException(ex); 683 } finally { 684 if (decrementedCalls == false) { 685 context.decrementCalls(); 686 } 687 context.touch(); 688 } 689 } 690 691 692 695 protected void preCreate(Invocation inv, EntityContextImpl context) { 696 statCreateCount++; 697 } 698 699 700 706 public void postCreate(Invocation inv, Object primaryKey) 707 throws CreateException 708 { 709 if ( primaryKey == null ) 710 throw new EJBException( 711 "Null primary key returned by ejbCreate method"); 712 713 EntityContextImpl context = (EntityContextImpl)inv.context; 714 EJBObjectImpl ejbObjImpl = null; 715 EJBLocalObjectImpl localObjImpl = null; 716 717 if ( (isRemote) && (!inv.isLocal) ) { 718 ejbObjImpl = internalGetEJBObjectImpl(primaryKey, null, true); 720 721 context.setEJBObjectImpl(ejbObjImpl); 723 context.setEJBStub((EJBObject)ejbObjImpl.getStub()); 724 } 725 726 if ( isLocal ) { 727 localObjImpl = internalGetEJBLocalObjectImpl(primaryKey, true); 731 732 context.setEJBLocalObjectImpl(localObjImpl); 734 } 735 736 if ( inv.isLocal ) 737 inv.ejbObject = localObjImpl; 738 else 739 inv.ejbObject = ejbObjImpl; 740 741 if ( context.getTransaction() != null ) { 742 addIncompleteTxEJB(context); 745 } 746 747 context.setDirty(true); } 749 750 protected Object invokeFindByPrimaryKey(Method method, 753 Invocation inv, Object [] args) 754 throws Throwable 755 { 756 Object pKeys = super.invokeTargetBeanMethod(method, 757 inv, inv.ejb, args, null); 758 return postFind(inv, pKeys, null); 759 } 760 761 764 protected void preFind(Invocation inv, EntityContextImpl context) { 765 if ( willInvokeWithClientTx(inv) && 770 !inv.method.getName().equals("findByPrimaryKey") ) { 771 Transaction tx = null; 772 try { 773 tx = transactionManager.getTransaction(); 774 } catch ( SystemException ex ) { 775 throw new EJBException(ex); 776 } 777 778 storeAllBeansInTx( tx ); 779 } 780 781 } 782 783 786 public void preSelect() 787 throws javax.ejb.EJBException { 788 _logger.fine(" inside preSelect..."); 793 Transaction tx = null; 794 try { 795 _logger.fine("PRESELECT : getting transaction..."); 796 tx = transactionManager.getTransaction(); 797 } catch ( SystemException ex ) { 798 throw new EJBException(ex); 799 } 800 _logger.fine("PRESELECT : calling storeAllBeansInTx()..."); 801 storeAllBeansInTx( tx ); 802 } 803 804 815 public Object postFind(Invocation inv, Object primaryKeys, 816 Object [] findParams) 817 throws FinderException 818 { 819 820 if ( primaryKeys instanceof Enumeration ) { 821 Enumeration e = (Enumeration)primaryKeys; 823 ObjrefEnumeration objrefs = new ObjrefEnumeration(); 825 while ( e.hasMoreElements() ) { 826 Object primaryKey = e.nextElement(); 827 Object ref; 828 if( primaryKey != null ) { 829 if ( inv.isLocal ) 830 ref = getEJBLocalObjectForPrimaryKey(primaryKey); 831 else 832 ref = getEJBObjectStub(primaryKey, null); 833 objrefs.add(ref); 834 } else { 835 objrefs.add(null); 836 } 837 } 838 return objrefs; 839 } else if ( primaryKeys instanceof Collection ) { 840 Collection c = (Collection)primaryKeys; 842 Iterator it = c.iterator(); 843 ArrayList objrefs = new ArrayList(); while ( it.hasNext() ) { 845 Object primaryKey = it.next(); 846 Object ref; 847 if( primaryKey != null ) { 848 if ( inv.isLocal ) 849 ref = getEJBLocalObjectForPrimaryKey(primaryKey); 850 else 851 ref = getEJBObjectStub(primaryKey, null); 852 objrefs.add(ref); 853 } else { 854 objrefs.add(null); 855 } 856 } 857 return objrefs; 858 } else { 859 if( primaryKeys != null ) { 860 if ( inv.isLocal ) 861 return getEJBLocalObjectForPrimaryKey(primaryKeys); 862 else 863 return getEJBObjectStub(primaryKeys, null); 864 } else { 865 return null; 866 } 867 } 868 } 869 870 877 public EJBObject getEJBObjectForPrimaryKey(Object pkey) { 878 return getEJBObjectStub(pkey, null); 880 } 881 882 923 public EJBLocalObject getEJBLocalObjectForPrimaryKey 924 (Object pkey, EJBContext ctx) { 925 926 EntityContextImpl context = (EntityContextImpl) ctx; 927 EJBLocalObjectImpl ejbLocalObjectImpl = 928 internalGetEJBLocalObjectImpl(pkey); 929 930 if (context.isCascadeDeleteBeforeEJBRemove()) { 931 J2EETransaction current = null; 932 try { 933 current = (J2EETransaction) transactionManager.getTransaction(); 934 } catch ( SystemException ex ) { 935 throw new EJBException(ex); 936 } 937 ActiveTxCache activeTxCache = (current == null) ? null : 938 (ActiveTxCache) (current.getActiveTxCache()); 939 if (activeTxCache != null) { 940 EntityContextImpl ctx2 = (EntityContextImpl) 941 activeTxCache.get(this, pkey); 942 if ((ctx2 != null) && 943 (ctx2.isCascadeDeleteAfterSuperEJBRemove())) { 944 return null; 945 } 946 } 947 return (EJBLocalObject) ejbLocalObjectImpl.getClientObject(); 948 } 949 950 return (EJBLocalObject) ejbLocalObjectImpl.getClientObject(); 951 } 952 953 960 public EJBLocalObject getEJBLocalObjectForPrimaryKey(Object pkey) { 961 EJBLocalObjectImpl localObjectImpl = 962 internalGetEJBLocalObjectImpl(pkey); 963 return (localObjectImpl != null) ? 964 (EJBLocalObject) localObjectImpl.getClientObject() : null; 965 } 966 967 protected void removeBean(Object primaryKey, Method removeMethod, 970 boolean local) 971 throws RemoveException, EJBException, RemoteException 972 { 973 EJBLocalRemoteObject ejbo; 974 if ( local ) { 975 ejbo = internalGetEJBLocalObjectImpl(primaryKey, false, true); 976 } 977 else { ejbo = internalGetEJBObjectImpl(primaryKey, null, false, true); 979 } 980 removeBean(ejbo, removeMethod, local); 981 } 982 983 protected void removeBean(EJBLocalRemoteObject ejbo, Method removeMethod, 986 boolean local) 987 throws RemoveException, EJBException, RemoteException 988 { 989 Invocation i = new Invocation(); 990 i.ejbObject = ejbo; 991 i.isLocal = local; 992 i.method = removeMethod; 993 994 Class declaringClass = removeMethod.getDeclaringClass(); 998 i.isHome = ( (declaringClass == javax.ejb.EJBHome .class) || 999 (declaringClass == javax.ejb.EJBLocalHome .class) ); 1000 1001 try { 1002 preInvoke(i); 1003 removeBean(i); 1004 } catch(Exception e) { 1005 _logger.log(Level.SEVERE,"ejb.preinvoke_exception",logParams); 1006 _logger.log(Level.SEVERE,"",e); 1007 i.exception = e; 1008 } finally { 1009 postInvoke(i); 1010 } 1011 1012 if(i.exception != null) { 1013 if(i.exception instanceof RemoveException) { 1014 throw (RemoveException)i.exception; 1015 } 1016 else if(i.exception instanceof RuntimeException ) { 1017 throw (RuntimeException )i.exception; 1018 } 1019 else if(i.exception instanceof Exception ) { 1020 throw new EJBException((Exception )i.exception); 1021 } 1022 else { 1023 EJBException ejbEx = new EJBException(); 1024 ejbEx.initCause(i.exception); 1025 throw ejbEx; 1026 } 1027 } 1028 } 1029 1030 1031 1036 protected void removeBean(Invocation inv) 1037 throws RemoveException 1038 { 1039 try { 1040 statRemoveCount++; 1041 1045 EntityBean ejb = (EntityBean)inv.ejb; 1048 EntityContextImpl context = (EntityContextImpl)inv.context; 1049 callEJBRemove(ejb, context); 1050 1051 Object primaryKey = inv.ejbObject.getKey(); 1053 if ( isRemote ) { 1054 removeEJBObjectFromStore(primaryKey); 1055 1056 if ( context.getEJBObjectImpl() != null ) { 1058 context.getEJBObjectImpl().setRemoved(true); 1059 } 1060 } 1061 1062 if ( isLocal ) { 1063 ejbLocalObjectStore.remove(primaryKey); 1065 if ( context.getEJBLocalObjectImpl() != null ) { 1067 context.getEJBLocalObjectImpl().setRemoved(true); 1068 } 1069 } 1070 1071 EJBTimerService ejbTimerService = 1073 containerFactory.getEJBTimerService(); 1074 if( (ejbTimerService != null) && isTimedObject() ) { 1075 ejbTimerService.cancelEntityBeanTimers(getContainerId(), 1076 primaryKey); 1077 } 1078 1079 } catch ( RemoveException ex ) { 1080 if(_logger.isLoggable(Level.FINE)) { 1081 _logger.log(Level.FINE,"ejb.local_remove_exception",logParams); 1082 _logger.log(Level.FINE,"",ex); 1083 } 1084 throw ex; 1085 } 1086 catch ( Exception ex ) { 1087 if(_logger.isLoggable(Level.FINE)) { 1088 _logger.log(Level.FINE,"ejb.remove_bean_exception",logParams); 1089 _logger.log(Level.FINE,"",ex); 1090 } 1091 throw new EJBException(ex); 1092 } 1093 } 1094 1095 private void removeEJBObjectFromStore(Object primaryKey) { 1096 removeEJBObjectFromStore(primaryKey, true); 1097 } 1098 1099 private void removeEJBObjectFromStore(Object primaryKey, boolean decrementRefCount) { 1100 EJBObjectImpl ejbObjImpl = 1103 (EJBObjectImpl)ejbObjectStore.remove(primaryKey, decrementRefCount); 1104 1105 if ( ejbObjImpl != null ) { 1106 synchronized ( ejbObjImpl ) { 1107 remoteHomeRefFactory.destroyReference(ejbObjImpl.getStub(), 1110 ejbObjImpl.getEJBObject()); 1111 } 1112 } 1113 } 1114 1115 1119 public void removeBeanUnchecked(EJBLocalObject localObj) { 1120 EJBLocalObjectImpl localObjectImpl = 1122 EJBLocalObjectImpl.toEJBLocalObjectImpl(localObj); 1123 internalRemoveBeanUnchecked(localObjectImpl, true); 1124 } 1125 1126 1127 1131 public void removeBeanUnchecked(Object primaryKey) { 1132 EJBLocalRemoteObject ejbo; 1133 if ( isLocal ) { 1134 ejbo = internalGetEJBLocalObjectImpl(primaryKey); 1135 internalRemoveBeanUnchecked(ejbo, true); 1136 } 1137 else { ejbo = internalGetEJBObjectImpl(primaryKey, null); 1139 internalRemoveBeanUnchecked(ejbo, false); 1140 } 1141 } 1142 1143 1147 private void internalRemoveBeanUnchecked( 1148 EJBLocalRemoteObject localRemoteObj, boolean local) { 1149 Invocation inv = new Invocation(); 1150 inv.ejbObject = localRemoteObj; 1151 inv.isLocal = local; 1152 Method method=null; 1153 try { 1154 method = EJBLocalObject.class.getMethod("remove", NO_PARAMS); 1155 } catch ( NoSuchMethodException e ) { 1156 _logger.log(Level.FINE, 1157 "Exception in internalRemoveBeanUnchecked()", e); 1158 } 1159 inv.method = method; 1160 1161 inv.invocationInfo = (InvocationInfo) invocationInfoMap.get(method); 1162 1163 try { 1164 EntityContextImpl context = getEJBWithIncompleteTx(inv); 1170 if ( context == null ) { 1171 context = getReadyEJB(inv); 1172 } 1173 1174 synchronized ( context ) { 1175 if ( context.getState() == INVOKING && !isReentrant ) { 1176 throw new EJBException( 1177 "EJB is already executing another request"); 1178 } 1179 if (context.getState() == POOLED || 1180 context.getState() == DESTROYED) { 1181 throw new EJBException("Internal error: unknown EJB state"); 1184 } 1185 1186 context.setState(INVOKING); 1187 } 1188 inv.context = context; 1189 context.setLastTransactionStatus(-1); 1190 context.incrementCalls(); 1191 1192 inv.instance = inv.ejb = context.getEJB(); 1193 inv.container = this; 1194 invocationManager.preInvoke(inv); 1195 1196 useClientTx(context.getTransaction(), inv); 1198 1199 try { 1200 context.setCascadeDeleteBeforeEJBRemove(true); 1201 removeBean(inv); 1202 } catch ( Exception ex ) { 1203 _logger.log(Level.FINE, 1204 "Exception in internalRemoveBeanUnchecked()", ex); 1205 inv.exception = checkExceptionClientTx(context, ex); 1207 } 1208 if ( inv.exception != null ) { 1209 throw inv.exception; 1210 } 1211 } 1212 catch ( RuntimeException ex ) { 1213 throw ex; 1214 } 1215 catch ( Exception ex ) { 1216 throw new EJBException(ex); 1217 } 1218 catch ( Throwable ex ) { 1219 EJBException ejbEx = new EJBException(); 1220 ejbEx.initCause(ex); 1221 throw ejbEx; 1222 } 1223 finally { 1224 invocationManager.postInvoke(inv); 1225 releaseContext(inv); 1226 } 1227 } 1228 1229 1235 void forceDestroyBean(EJBContextImpl ctx) { 1236 1241 if ( ctx.getState() == DESTROYED ) { 1242 entityCtxPool.destroyObject(null); 1243 return; 1244 } 1245 1246 EntityContextImpl context = (EntityContextImpl)ctx; 1247 EntityBean ejb = (EntityBean)context.getEJB(); 1248 synchronized ( context ) { 1250 try { 1251 Object primaryKey = context.getPrimaryKey(); 1252 if ( primaryKey != null ) { 1253 if ( context.getTransaction() != null ) { 1254 Transaction txCurrent = context.getTransaction(); 1255 ActiveTxCache activeTxCache = (ActiveTxCache) 1256 (((J2EETransaction) txCurrent).getActiveTxCache()); 1257 if (activeTxCache != null) { 1258 activeTxCache.remove(this, primaryKey); 1260 } 1261 } 1262 1263 removeContextFromReadyStore(primaryKey, context); 1265 1266 if (context.getEJBObjectImpl() != null) { 1267 removeEJBObjectFromStore(primaryKey); 1268 } 1269 if (context.getEJBLocalObjectImpl() != null) { 1270 ejbLocalObjectStore.remove(primaryKey); 1271 } 1272 1273 } 1274 } catch ( Exception ex ) { 1275 _logger.log(Level.FINE, "Exception in forceDestroyBean()", ex); 1276 } finally { 1277 try { 1278 context.setState(DESTROYED); 1281 entityCtxPool.destroyObject(context); 1282 } catch (Exception ex) { 1283 _logger.log(Level.FINE, "Exception in forceDestroyBean()", 1284 ex); 1285 } 1286 } 1287 } 1288 } 1290 1291 1292 protected void checkUnfinishedTx(Transaction prevTx, Invocation inv) { 1295 1296 try { 1297 if ( (prevTx != null) && 1298 prevTx.getStatus() != Status.STATUS_NO_TRANSACTION ) { 1299 throw new IllegalStateException ( 1302 "Bean is associated with a different unfinished transaction"); 1303 } 1304 } catch (SystemException ex) { 1305 throw new EJBException(ex); 1306 } 1307 } 1308 1309 1310 1315 void checkExists(EJBLocalRemoteObject ejbObj) { 1316 } 1320 1321 void afterBegin(EJBContextImpl ctx) { 1323 if ( ctx.getState() == DESTROYED ) 1325 return; 1326 1327 EntityContextImpl context = (EntityContextImpl)ctx; 1328 1329 if ( context.getEJBObjectImpl() != null 1330 || context.getEJBLocalObjectImpl() != null ) { 1331 1333 if ( context.getTransaction() != null ) { 1336 addIncompleteTxEJB(context); 1337 } 1338 1339 EntityBean e = (EntityBean)context.getEJB(); 1342 try { 1343 callEJBLoad(e, context); 1344 } catch ( NoSuchEntityException ex ) { 1345 _logger.log(Level.FINE, "Exception in afterBegin()", ex); 1346 forceDestroyBean(context); 1348 1349 throw new NoSuchObjectLocalException( 1350 "NoSuchEntityException thrown by ejbLoad, EJB instance discarded", ex); 1351 } catch ( Exception ex ) { 1352 forceDestroyBean(context); 1354 1355 throw new EJBException(ex); 1356 } 1357 1358 context.setNewlyActivated(false); 1359 } 1360 } 1361 1362 void beforeCompletion(EJBContextImpl ctx) { 1364 if ( ctx.getState() == DESTROYED ) { 1365 return; 1366 } 1367 1368 EntityContextImpl context = (EntityContextImpl)ctx; 1369 EJBLocalRemoteObject ejbObjImpl = context.getEJBObjectImpl(); 1370 EJBLocalRemoteObject ejbLocalObjImpl = context.getEJBLocalObjectImpl(); 1371 1372 if ( ((ejbObjImpl != null) && !ejbObjImpl.isRemoved()) 1375 || ((ejbLocalObjImpl != null) && !ejbLocalObjImpl.isRemoved()) ) { 1376 if ( context.isDirty() ) { 1377 enlistResourcesAndStore(context); 1378 } 1379 } 1380 } 1381 1382 1383 private void enlistResourcesAndStore(EntityContextImpl context) { 1385 EntityBean e = (EntityBean)context.getEJB(); 1386 Invocation inv = new Invocation(e, this); 1391 inv.context = context; 1392 invocationManager.preInvoke(inv); 1393 1394 try { 1395 transactionManager.enlistComponentResources(); 1396 1397 callEJBStore(e, context); 1398 1399 } catch ( NoSuchEntityException ex ) { 1400 forceDestroyBean(context); 1402 1403 throw new NoSuchObjectLocalException( 1404 "NoSuchEntityException thrown by ejbStore, EJB instance discarded", ex); 1405 } catch ( Exception ex ) { 1406 forceDestroyBean(context); 1408 throw new EJBException(ex); 1409 } finally { 1410 invocationManager.postInvoke(inv); 1411 } 1412 } 1413 1414 1415 void afterCompletion(EJBContextImpl ctx, int status) { 1426 if ( ctx.getState() == DESTROYED ) { 1427 return; 1428 } 1429 1430 if (super.isUndeployed()) { 1431 transactionManager.ejbDestroyed(ctx); 1432 return; 1433 } 1434 1435 EntityContextImpl context = (EntityContextImpl)ctx; 1436 EJBLocalRemoteObject ejbObjImpl = context.getEJBObjectImpl(); 1437 EJBLocalRemoteObject ejbLocalObjImpl = context.getEJBLocalObjectImpl(); 1438 1439 if ( ((ejbObjImpl != null) && !ejbObjImpl.isRemoved()) 1441 || ((ejbLocalObjImpl != null) && !ejbLocalObjImpl.isRemoved()) ) { 1442 1446 1448 context.setTransaction(null); 1449 context.setLastTransactionStatus(status); 1450 1451 context.setCascadeDeleteAfterSuperEJBRemove(false); 1452 context.setCascadeDeleteBeforeEJBRemove(false); 1453 1454 if ( context.getState() != INVOKING ) { 1456 if ( (status == Status.STATUS_COMMITTED) 1457 || (status == Status.STATUS_NO_TRANSACTION) ) { 1458 addReadyEJB(context); 1459 } else { 1460 passivateAndPoolEJB(context); 1461 } 1462 } 1463 } else if ((ejbObjImpl == null) && (ejbLocalObjImpl == null)) { 1464 1469 context.setTransaction(null); 1470 context.setLastTransactionStatus(status); 1471 1472 context.setCascadeDeleteAfterSuperEJBRemove(false); 1473 context.setCascadeDeleteBeforeEJBRemove(false); 1474 1475 if ( context.getState() != INVOKING ) { 1476 addPooledEJB(context); 1477 } 1478 } else if ( ((ejbObjImpl != null) && ejbObjImpl.isRemoved()) 1479 || ((ejbLocalObjImpl != null) && ejbLocalObjImpl.isRemoved()) ) 1480 { 1481 context.setTransaction(null); 1483 context.setLastTransactionStatus(status); 1484 1485 if (context.getState() == INCOMPLETE_TX) { 1486 addPooledEJB(context); 1487 } 1488 } 1489 } 1490 1491 1492 void preInvokeNoTx(Invocation inv) { 1496 EntityContextImpl context = (EntityContextImpl)inv.context; 1497 1498 if ( context.getState() == DESTROYED ) { 1499 return; 1500 } 1501 1502 if ( context.isNewlyActivated() && 1503 !inv.invocationInfo.isCreateHomeFinder ) { 1504 EntityBean e = (EntityBean)context.getEJB(); 1506 try { 1507 callEJBLoad(e, context); 1508 } catch ( NoSuchEntityException ex ) { 1509 forceDestroyBean(context); 1511 1512 throw new NoSuchObjectLocalException( 1513 "NoSuchEntityException thrown by ejbLoad, EJB instance discarded", ex); 1514 } catch ( Exception ex ) { 1515 forceDestroyBean(context); 1517 1518 throw new EJBException(ex); 1519 } 1520 1521 context.setNewlyActivated(false); 1522 } 1523 } 1524 1525 void postInvokeNoTx(Invocation inv) { 1528 beforeCompletion((EJBContextImpl)inv.context); 1532 } 1533 1534 1535 1536 public void trimEvent(Object primaryKey, Object context) { 1538 boolean addTask = false; 1539 synchronized (asyncTaskSemaphore) { 1540 passivationCandidates.add(context); 1541 if (addedASyncTask == true) { 1542 return; 1543 } 1544 addTask = addedASyncTask = true; 1545 } 1546 1547 try { 1548 ASyncPassivator work = new ASyncPassivator(); 1549 ContainerWorkPool.addLast(work); 1550 } catch (Exception ex) { 1551 addedASyncTask = false; 1552 _logger.log(Level.WARNING, "ejb.add_cleanup_task_error",ex); 1553 } 1554 } 1555 1556 private class ASyncPassivator 1557 implements com.sun.enterprise.util.threadpool.Servicable 1558 { 1559 1560 public void prolog() { } 1561 1562 public void epilog() { } 1563 1564 public void service() { run(); } 1565 1566 public void run() { 1567 final Thread currentThread = Thread.currentThread(); 1568 final ClassLoader previousClassLoader = 1569 currentThread.getContextClassLoader(); 1570 final ClassLoader myClassLoader = loader; 1571 1572 try { 1573 java.security.AccessController.doPrivileged( 1576 new java.security.PrivilegedAction () { 1577 public java.lang.Object run() { 1578 currentThread.setContextClassLoader(loader); 1579 return null; 1580 } 1581 } 1582 ); 1583 1584 ComponentContext ctx = null; 1585 do { 1586 synchronized (asyncTaskSemaphore) { 1587 int sz = passivationCandidates.size() - 1; 1588 if (sz > 0) { 1589 ctx = 1590 (ComponentContext) passivationCandidates.remove(sz-1); 1591 } else { 1592 return; 1593 } 1594 } 1595 1596 if (ctx != null) { 1597 passivateEJB(ctx); 1598 totalPassivations++; 1599 } 1600 } while (ctx != null); 1601 } catch (Throwable th) { 1602 totalPassivationErrors++; 1603 th.printStackTrace(); 1604 } finally { 1605 synchronized (asyncTaskSemaphore) { 1606 addedASyncTask = false; 1607 } 1608 java.security.AccessController.doPrivileged( 1609 new java.security.PrivilegedAction () { 1610 public java.lang.Object run() { 1611 currentThread.setContextClassLoader(previousClassLoader); 1612 return null; 1613 } 1614 } 1615 ); 1616 } 1617 } 1618 } 1619 1620 boolean passivateEJB(ComponentContext ctx) { 1622 if (containerState != CONTAINER_STARTED) { 1623 return false; 1624 } 1625 1626 EntityContextImpl context = (EntityContextImpl)ctx; 1627 1628 if (context.getState() != READY) { 1629 return false; 1630 } 1631 1632 _logger.log(Level.FINEST,"EntityContainer.passivateEJB(): context = (" + 1633 ctx + ")"); 1634 EntityBean ejb = (EntityBean)context.getEJB(); 1635 1636 Invocation inv = new Invocation(ejb, this, context); 1637 inv.method = ejbPassivateMethod; 1638 1639 Object pkey = context.getPrimaryKey(); 1640 boolean wasPassivated = false; 1641 1642 if ( context.getState() != READY ) 1644 return false; 1645 try { 1646 invocationManager.preInvoke(inv); 1647 1648 removeContextFromReadyStore(pkey, context); 1650 1651 ejb.ejbPassivate(); 1653 1654 wasPassivated = true; 1655 } catch ( Exception ex ) { 1656 _logger.log(Level.FINE, "Exception in passivateEJB()", ex); 1657 forceDestroyBean(context); 1659 return false; 1660 } finally { 1661 invocationManager.postInvoke(inv); 1662 } 1663 1664 if ( isRemote ) { 1667 removeEJBObjectFromStore(pkey); 1668 } 1669 if ( isLocal ) { 1670 ejbLocalObjectStore.remove(pkey); 1671 } 1672 1673 synchronized (context) { 1677 addPooledEJB(context); 1678 } 1679 1680 return wasPassivated; 1681 } 1682 1683 1684 1685 1689 1690 1691 protected EJBLocalObjectImpl internalGetEJBLocalObjectImpl 1694 (Object primaryKey) { 1695 return internalGetEJBLocalObjectImpl(primaryKey, false, 1696 defaultCacheEJBO); 1697 } 1698 1699 protected EJBLocalObjectImpl internalGetEJBLocalObjectImpl 1700 (Object primaryKey, boolean incrementRefCount) 1701 { 1702 return internalGetEJBLocalObjectImpl(primaryKey, incrementRefCount, 1703 defaultCacheEJBO); 1704 } 1705 1706 protected EJBLocalObjectImpl internalGetEJBLocalObjectImpl 1707 (Object primaryKey, boolean incrementRefCount, boolean cacheEJBO) { 1708 try { 1710 EJBLocalObjectImpl localObjImpl = (EJBLocalObjectImpl) 1711 ejbLocalObjectStore.get(primaryKey, incrementRefCount); 1712 if ( localObjImpl == null ) { 1713 1714 localObjImpl = instantiateEJBLocalObjectImpl(); 1715 1716 localObjImpl.setKey(primaryKey); 1718 1719 if (incrementRefCount || cacheEJBO) { 1721 ejbLocalObjectStore.put(primaryKey, localObjImpl, 1722 incrementRefCount); 1723 } 1724 } 1725 return localObjImpl; 1726 } catch ( Exception ex ) { 1727 _logger.log(Level.SEVERE,"ejb.get_ejb_local_object_exception", 1728 logParams); 1729 _logger.log(Level.SEVERE,"",ex); 1730 throw new EJBException(ex); 1731 } 1732 } 1733 1734 EJBObject getEJBObjectStub(Object primaryKey, byte[] streamKey) { 1737 1739 try { 1741 EJBObjectImpl ejbObjImpl = 1742 (EJBObjectImpl) ejbObjectStore.get(primaryKey); 1743 if ( (ejbObjImpl != null) && (ejbObjImpl.getStub() != null) ) { 1744 return (EJBObject) ejbObjImpl.getStub(); 1745 } 1746 1747 if ( streamKey == null ) { 1749 streamKey = EJBUtils.serializeObject(primaryKey, false); 1750 } 1751 EJBObject ejbStub = (EJBObject) 1752 remoteHomeRefFactory.createRemoteReference(streamKey); 1753 1754 return ejbStub; 1755 } catch ( Exception ex ) { 1756 _logger.log(Level.FINE,"", ex); 1757 throw new EJBException(ex); 1758 } 1759 } 1760 1761 protected EJBObjectImpl internalGetEJBObjectImpl(Object primaryKey, 1764 byte[] streamKey) { 1765 return internalGetEJBObjectImpl(primaryKey, streamKey, false, 1766 defaultCacheEJBO); 1767 } 1768 1769 protected EJBObjectImpl internalGetEJBObjectImpl(Object primaryKey, 1770 byte[] streamKey, boolean incrementRefCount) 1771 { 1772 return internalGetEJBObjectImpl 1773 (primaryKey, streamKey, incrementRefCount, defaultCacheEJBO); 1774 } 1775 1776 1777 protected EJBObjectImpl internalGetEJBObjectImpl(Object primaryKey, 1780 byte[] streamKey, boolean incrementRefCount, boolean cacheEJBO) { 1781 1783 try { 1785 1786 EJBObjectImpl ejbObjImpl = (EJBObjectImpl) 1787 ejbObjectStore.get(primaryKey, incrementRefCount); 1788 1789 if ( (ejbObjImpl != null) && (ejbObjImpl.getStub() != null) ) { 1790 return ejbObjImpl; 1791 } 1792 1793 ejbObjImpl = (EJBObjectImpl) ejbServant.get(); 1799 if ( ejbObjImpl != null ) { 1800 return ejbObjImpl; 1801 } 1802 1803 ejbObjImpl = instantiateEJBObjectImpl(); 1805 1806 ejbObjImpl.setKey(primaryKey); 1808 1809 ejbServant.set(ejbObjImpl); 1811 1812 1814 if ( streamKey == null ) { 1815 streamKey = EJBUtils.serializeObject(primaryKey, false); 1816 } 1817 EJBObject ejbStub = (EJBObject) 1818 remoteHomeRefFactory.createRemoteReference(streamKey); 1819 1820 ejbObjImpl.setStub(ejbStub); 1821 ejbServant.set(null); 1822 1823 if ((incrementRefCount || cacheEJBO)) { 1824 EJBObjectImpl ejbo1 = 1825 (EJBObjectImpl) ejbObjectStore.put(primaryKey, ejbObjImpl, 1826 incrementRefCount); 1827 if ((ejbo1 != null) && (ejbo1 != ejbObjImpl)) { 1828 remoteHomeRefFactory.destroyReference(ejbObjImpl.getStub(), 1829 ejbObjImpl); 1830 ejbObjImpl = ejbo1; 1831 } 1832 } 1833 1834 return ejbObjImpl; 1835 } 1836 catch ( Exception ex ) { 1837 _logger.log(Level.FINE, "ejb.get_ejb_context_exception", logParams); 1838 _logger.log(Level.FINE,"",ex); 1839 throw new EJBException(ex); 1840 } 1841 } 1843 1844 protected EntityContextImpl getPooledEJB() { 1846 try { 1847 return (EntityContextImpl) entityCtxPool.getObject(true, null); 1848 } catch (com.sun.ejb.containers.util.pool.PoolException inEx) { 1849 throw new EJBException(inEx); 1850 } 1851 } 1852 1853 protected void addPooledEJB(EntityContextImpl context) { 1857 if ( context.getState() == POOLED ) { 1858 return; 1859 } 1860 context.setEJBLocalObjectImpl(null); 1863 context.setEJBObjectImpl(null); 1864 context.setEJBStub(null); 1865 context.setState(POOLED); 1866 context.clearCachedPrimaryKey(); 1867 1868 entityCtxPool.returnObject(context); 1870 1871 } 1872 1873 protected void passivateAndPoolEJB(EntityContextImpl context) { 1875 if ( context.getState() == DESTROYED || context.getState() == POOLED ) 1876 return; 1877 1878 EntityBean ejb = (EntityBean) context.getEJB(); 1883 synchronized ( context ) { 1884 Invocation inv = new Invocation(ejb, this, context); 1885 inv.method = ejbPassivateMethod; 1886 invocationManager.preInvoke(inv); 1887 1888 try { 1889 ejb.ejbPassivate(); 1890 } catch ( Exception ex ) { 1891 _logger.log(Level.FINE,"Exception in passivateAndPoolEJB()",ex); 1892 forceDestroyBean(context); 1893 return; 1894 } finally { 1895 invocationManager.postInvoke(inv); 1896 } 1897 1898 1900 1901 Object primaryKey = context.getPrimaryKey(); 1902 if ( isRemote ) { 1903 removeEJBObjectFromStore(primaryKey); 1904 } 1905 if ( isLocal ) { 1906 ejbLocalObjectStore.remove(primaryKey); 1907 } 1908 1909 addPooledEJB(context); 1910 } 1911 } 1912 1913 1914 1921 protected EntityContextImpl activateEJBFromPool(Object primaryKey, 1922 Invocation inv) { 1923 EntityContextImpl context = null; 1924 context = getPooledEJB(); 1926 1927 1930 if ( inv.isLocal ) { 1932 EJBLocalObjectImpl localObjImpl = 1933 internalGetEJBLocalObjectImpl(primaryKey, true); 1934 inv.ejbObject = localObjImpl; 1935 context.setEJBLocalObjectImpl(localObjImpl); 1936 } else { EJBObjectImpl ejbObjImpl = 1942 internalGetEJBObjectImpl(primaryKey, null, true); 1943 inv.ejbObject = ejbObjImpl; 1944 context.setEJBObjectImpl(ejbObjImpl); 1945 context.setEJBStub((EJBObject)ejbObjImpl.getStub()); 1946 1947 if ( isLocal ) { 1948 context.setEJBLocalObjectImpl( 1950 internalGetEJBLocalObjectImpl(primaryKey, true)); 1951 } 1952 } 1953 1954 context.setState(READY); 1955 1956 EntityBean ejb = (EntityBean)context.getEJB(); 1957 1958 Invocation inv2 = new Invocation(ejb, this, context); 1959 inv2.method = ejbActivateMethod; 1960 invocationManager.preInvoke(inv2); 1961 1962 try { 1963 ejb.ejbActivate(); 1964 1965 1969 } catch ( Exception ex ) { 1970 forceDestroyBean(context); 1972 throw new EJBException(ex); 1973 } finally { 1974 invocationManager.postInvoke(inv2); 1975 } 1976 1977 context.setNewlyActivated(true); 1978 1980 afterNewlyActivated(context); 1981 1982 return context; 1983 } 1985 1986 1988 1989 1994 private EntityContextImpl getEJBWithIncompleteTx(Invocation inv) { 1995 2001 J2EETransaction current = null; 2002 try { 2003 current = (J2EETransaction) transactionManager.getTransaction(); 2004 } catch ( SystemException ex ) { 2005 throw new EJBException(ex); 2006 } 2007 2008 EntityContextImpl ctx = null; 2009 if (current != null) { 2010 ActiveTxCache activeTxCache = (ActiveTxCache) 2011 current.getActiveTxCache(); 2012 ctx = (activeTxCache == null) 2013 ? null 2014 : activeTxCache.get(this, inv.ejbObject.getKey()); 2015 } 2016 2017 return ctx; 2018 } 2019 2020 2021 2027 private void addIncompleteTxEJB(EntityContextImpl context) { 2028 J2EETransaction current = (J2EETransaction) context.getTransaction(); 2029 if ( current == null ) { 2030 return; 2031 } 2032 if ( (context.getEJBObjectImpl() == null) && 2033 (context.getEJBLocalObjectImpl() == null) ) { 2034 return; 2035 } 2036 2037 ActiveTxCache activeTxCache = (ActiveTxCache) current.getActiveTxCache(); 2039 if (activeTxCache == null) { 2040 activeTxCache = new ActiveTxCache(DEFAULT_TX_CACHE_BUCKETS); 2041 current.setActiveTxCache(activeTxCache); 2042 } 2043 2044 activeTxCache.add(context); 2045 2046 Vector beans; 2047 synchronized ( txBeanTable ) { 2048 beans = (Vector)txBeanTable.get(current); 2049 if ( beans == null ) { 2050 beans = new Vector(); 2051 txBeanTable.put(current, beans); 2052 } 2053 } 2054 beans.add(context); 2055 } 2056 2057 2061 protected void removeIncompleteTxEJB(EntityContextImpl context, 2062 boolean updateTxBeanTable) 2063 { 2064 J2EETransaction current = (J2EETransaction) context.getTransaction(); 2065 2066 if (current == null) { 2067 return; 2068 } 2069 if ( (context.getEJBObjectImpl() == null) && 2070 (context.getEJBLocalObjectImpl() == null) ) { 2071 return; 2072 } 2073 2074 ActiveTxCache activeTxCache = (ActiveTxCache) 2075 (((J2EETransaction) current).getActiveTxCache()); 2076 if (activeTxCache != null) { 2077 activeTxCache.remove(this, context.getPrimaryKey()); 2078 } 2079 2080 if ( updateTxBeanTable ) { 2081 Vector beans = (Vector)txBeanTable.get(current); 2082 if ( beans != null ) { 2083 beans.remove(context); } 2085 } 2086 } 2087 2088 2091 private class IdleBeansPassivator 2092 extends java.util.TimerTask 2093 { 2094 Cache cache; 2095 2096 IdleBeansPassivator(Cache cache) { 2097 this.cache = cache; 2098 } 2099 2100 public void run() { 2101 if (timerValid) { 2102 cache.trimExpiredEntries(Integer.MAX_VALUE); 2103 } 2104 } 2105 2106 public boolean cancel() { 2107 cache = null; 2108 return super.cancel(); 2109 } 2110 } 2111 2112 2113 private class EJBTxKey { 2115 2116 Transaction tx; Object primaryKey; 2118 int pkHashCode; 2119 2120 EJBTxKey(Object primaryKey, Transaction tx) { 2121 this.tx = tx; 2122 this.primaryKey = primaryKey; 2123 this.pkHashCode = primaryKey.hashCode(); 2124 } 2125 2126 public final int hashCode() { 2127 2133 return pkHashCode; 2135 } 2136 2137 public final boolean equals(Object obj) { 2138 if ( !(obj instanceof EJBTxKey) ) { 2139 return false; 2140 } 2141 EJBTxKey other = (EJBTxKey) obj; 2142 try { 2143 if ( primaryKey.equals(other.primaryKey) ) { 2146 if ( (tx == null) && (other.tx == null) ) { 2147 return true; 2148 } else if ( (tx != null) && (other.tx != null) 2149 && tx.equals(other.tx) ) { 2150 return true; 2151 } else { 2152 return false; 2153 } 2154 } else { 2155 return false; 2156 } 2157 } catch ( Exception ex ) { 2158 _logger.log(Level.FINE, "Exception in equals()", ex); 2159 return false; 2160 } 2161 } 2162 2163 } 2164 2165 protected class CacheProperties { 2166 2167 int maxCacheSize ; 2168 int numberOfVictimsToSelect ; 2169 int cacheIdleTimeoutInSeconds ; 2170 String victimSelectionPolicy; 2171 int removalTimeoutInSeconds; 2172 2173 public CacheProperties() { 2174 numberOfVictimsToSelect = 2175 new Integer (ejbContainer.getCacheResizeQuantity()).intValue(); 2176 maxCacheSize=new Integer (ejbContainer.getMaxCacheSize()).intValue(); 2177 cacheIdleTimeoutInSeconds = 2178 new Integer (ejbContainer.getCacheIdleTimeoutInSeconds()).intValue(); 2179 victimSelectionPolicy = ejbContainer.getVictimSelectionPolicy(); 2180 removalTimeoutInSeconds = 2181 new Integer (ejbContainer.getRemovalTimeoutInSeconds()).intValue(); 2182 2183 if(beanCacheDes != null) { 2184 int temp = 0; 2185 if((temp = beanCacheDes.getResizeQuantity()) != -1) { 2186 numberOfVictimsToSelect = temp; 2187 } 2188 2189 if((temp = beanCacheDes.getMaxCacheSize()) != -1) { 2190 maxCacheSize = temp; 2191 } 2192 2193 if ((temp = beanCacheDes.getCacheIdleTimeoutInSeconds()) != -1) 2194 { 2195 cacheIdleTimeoutInSeconds = temp; 2196 } 2197 2198 if (( beanCacheDes.getVictimSelectionPolicy()) != null) { 2199 victimSelectionPolicy = 2200 beanCacheDes.getVictimSelectionPolicy(); 2201 } 2202 if ((temp = beanCacheDes.getRemovalTimeoutInSeconds()) != -1) { 2203 removalTimeoutInSeconds = temp; 2204 } 2205 } 2206 } 2207 } 2209 private class PoolProperties { 2210 int maxPoolSize; 2211 int poolIdleTimeoutInSeconds; 2212 int poolResizeQuantity; 2214 int steadyPoolSize; 2215 2216 public PoolProperties() { 2217 2218 maxPoolSize = new Integer (ejbContainer.getMaxPoolSize()).intValue(); 2219 poolIdleTimeoutInSeconds = new Integer ( 2220 ejbContainer.getPoolIdleTimeoutInSeconds()).intValue(); 2221 poolResizeQuantity = new Integer ( 2222 ejbContainer.getPoolResizeQuantity()).intValue(); 2223 steadyPoolSize = new Integer ( 2224 ejbContainer.getSteadyPoolSize()).intValue(); 2225 if(beanPoolDes != null) { 2226 int temp = 0; 2227 if (( temp = beanPoolDes.getMaxPoolSize()) != -1) { 2228 maxPoolSize = temp; 2229 } 2230 if (( temp = beanPoolDes.getPoolIdleTimeoutInSeconds()) != -1) { 2231 poolIdleTimeoutInSeconds = temp; 2232 } 2233 if (( temp = beanPoolDes.getPoolResizeQuantity()) != -1) { 2234 poolResizeQuantity = temp; 2235 } 2236 if (( temp = beanPoolDes.getSteadyPoolSize()) != -1) { 2237 steadyPoolSize = temp; 2238 } 2239 } 2240 } 2241 } 2243 2244 boolean isIdentical(EJBObjectImpl ejbObjImpl, EJBObject other) 2245 throws RemoteException 2246 { 2247 if ( other == ejbObjImpl.getStub() ) { 2248 return true; 2249 } else { 2250 try { 2251 if ( !protocolMgr.isIdentical(ejbHomeStub, 2254 other.getEJBHome())) 2255 return false; 2256 2257 if (!ejbObjImpl.getPrimaryKey().equals(other.getPrimaryKey())) { 2259 return false; 2260 } 2261 2262 return true; 2263 } catch ( Exception ex ) { 2264 _logger.log(Level.INFO, "ejb.ejb_comparison_exception", 2265 logParams); 2266 _logger.log(Level.INFO, "", ex); 2267 throw new RemoteException ("Exception in isIdentical()", ex); 2268 } 2269 } 2270 } 2271 2272 2273 protected void callEJBLoad(EntityBean ejb, EntityContextImpl context) 2274 throws Exception 2275 { 2276 try { 2277 context.setInEjbLoad(true); 2278 ejb.ejbLoad(); 2279 } catch(Exception e) { 2282 throw e; 2283 } finally { 2284 context.setInEjbLoad(false); 2285 } 2286 } 2287 2288 protected void callEJBStore(EntityBean ejb, EntityContextImpl context) 2289 throws Exception 2290 { 2291 try { 2292 context.setInEjbStore(true); 2293 ejb.ejbStore(); 2294 } finally { 2295 context.setInEjbStore(false); 2296 context.setDirty(false); } 2298 } 2299 2300 protected void callEJBRemove(EntityBean ejb, EntityContextImpl context) 2301 throws Exception 2302 { 2303 Exception exc = null; 2304 try { 2305 context.setInEjbRemove(true); 2306 ejb.ejbRemove(); 2307 } catch ( Exception ex ) { 2308 exc = ex; 2309 throw ex; 2310 } finally { 2311 context.setInEjbRemove(false); 2312 context.setDirty(false); if ( AppVerification.doInstrument() ) { 2314 AppVerification.getInstrumentLogger().doInstrumentForEjb( 2315 ejbDescriptor, ejbRemoveMethod, exc); 2316 } 2317 } 2318 } 2319 2320 void doTimerInvocationInit(Invocation inv, RuntimeTimerState timerState) 2321 throws Exception 2322 { 2323 Object primaryKey = timerState.getTimedObjectPrimaryKey(); 2324 if( isRemote ) { 2325 inv.ejbObject = internalGetEJBObjectImpl(primaryKey, null); 2326 inv.isLocal = false; 2327 } else { 2328 inv.ejbObject = internalGetEJBLocalObjectImpl(primaryKey); 2329 inv.isLocal = true; 2330 } 2331 if( inv.ejbObject == null ) { 2332 throw new Exception ("Timed object identity (" + primaryKey + 2333 " ) no longer exists " ); 2334 } 2335 } 2336 2337 public void undeploy() { 2338 2339 super.setUndeployedState(); 2341 2342 String ejbName = ejbDescriptor.getName(); 2343 2344 _logger.log(Level.FINE,"[EntityContainer]: Undeploying " + ejbName + 2345 " ..."); 2346 2347 2349 try { 2350 Iterator elements = ejbObjectStore.values(); 2351 while ( elements.hasNext() ) { 2352 EJBObjectImpl ejbObjImpl = (EJBObjectImpl) elements.next(); 2353 try { 2354 if ( isRemote ) { 2355 remoteHomeRefFactory.destroyReference 2356 (ejbObjImpl.getStub(), ejbObjImpl.getEJBObject()); 2357 2358 } 2359 } catch ( Exception ex ) { 2360 _logger.log(Level.FINE, "Exception in undeploy()", ex); 2361 } 2362 } 2363 2364 ejbObjectStore.destroy(); ejbObjectStore = null; 2366 2367 ejbLocalObjectStore.destroy(); ejbLocalObjectStore = null; 2369 2370 destroyReadyStoreOnUndeploy(); 2373 2383 2384 entityCtxPool.close(); 2385 2386 if (idleBeansPassivator != null) { 2390 try { 2391 idleBeansPassivator.cancel(); 2392 } catch (Exception e) { 2393 _logger.log(Level.FINE, 2394 "[EntityContainer] cancelTimerTask: ", e); 2395 } 2396 this.idleBeansPassivator.cache = null; 2397 } 2398 cancelTimerTasks(); 2399 } 2400 finally { 2401 2402 super.undeploy(); 2403 2404 this.ejbObjectStore = null; 2406 this.ejbLocalObjectStore = null; 2407 this.passivationCandidates = null; 2408 this.readyStore = null; 2409 this.entityCtxPool = null; 2410 this.txBeanTable = null; 2411 this.iased = null; 2412 this.beanCacheDes = null; 2413 this.beanPoolDes = null; 2414 this.svr = null; 2415 this.cfg = null; 2416 this.ejbContainer = null; 2417 this.cacheProp = null; 2418 this.poolProp = null; 2419 this.asyncTaskSemaphore = null; 2420 this.idleBeansPassivator = null; 2421 2422 } 2423 2424 _logger.log(Level.FINE," [EntityContainer]: Successfully Undeployed " + 2425 ejbName); 2426 } 2427 2428 protected void afterNewlyActivated(EntityContextImpl context) { 2429 } 2431 2432 protected EntityContextImpl createEntityContextInstance(EntityBean ejb, 2433 EntityContainer entityContainer) 2434 { 2435 return new EntityContextImpl(ejb, entityContainer); 2436 } 2437 2438 private class EntityContextFactory 2439 implements ObjectFactory 2440 { 2441 private EntityContainer entityContainer; 2442 2443 public EntityContextFactory(EntityContainer entityContainer) { 2444 this.entityContainer = entityContainer; 2445 } 2446 2447 public Object create(Object param) { 2448 EntityContextImpl entityCtx = null; 2449 ComponentInvocation ci = null; 2450 try { 2451 EntityBean ejb = (EntityBean) ejbClass.newInstance(); 2455 2456 entityCtx = createEntityContextInstance(ejb, entityContainer); 2458 2459 ci = new ComponentInvocation(ejb, entityContainer,entityCtx); 2460 invocationManager.preInvoke(ci); 2461 2462 ejb.setEntityContext(entityCtx); 2465 2466 2469 } catch (Exception ex ) { 2470 throw new EJBException("Could not create Entity EJB", ex); 2471 } finally { 2472 if ( ci != null ) { 2473 invocationManager.postInvoke(ci); 2474 } 2475 } 2476 2477 entityCtx.touch(); 2478 return entityCtx; 2479 } 2480 2481 2482 public void destroy(Object object) { 2483 if (object == null) { 2484 return; 2487 } 2488 2489 EntityContextImpl context = (EntityContextImpl) object; 2490 EntityBean ejb = (EntityBean)context.getEJB(); 2491 if (context.getState() != DESTROYED) { 2492 ComponentInvocation ci = new ComponentInvocation(ejb, entityContainer, context); 2493 invocationManager.preInvoke(ci); 2494 2495 try { 2497 synchronized ( context ) { 2498 context.setEJBLocalObjectImpl(null); 2499 context.setEJBObjectImpl(null); 2500 context.setEJBStub(null); 2501 context.setState(DESTROYED); 2502 context.setInUnsetEntityContext(true); 2504 2505 try { 2506 ejb.unsetEntityContext(); 2507 } catch ( Exception ex ) { 2508 _logger.log(Level.FINE, 2509 "Exception in ejb.unsetEntityContext()", ex); 2510 } 2511 2512 transactionManager.ejbDestroyed(context); 2514 } 2515 } finally { 2516 invocationManager.postInvoke(ci); 2517 } 2518 } else { 2519 try { 2521 synchronized ( context ) { 2522 context.setEJBLocalObjectImpl(null); 2523 context.setEJBObjectImpl(null); 2524 context.setEJBStub(null); 2525 context.setState(DESTROYED); 2526 2528 Transaction tx = context.getTransaction(); 2530 if ( tx != null && tx.getStatus() != 2531 Status.STATUS_NO_TRANSACTION ) { 2532 context.getTransaction().setRollbackOnly(); 2533 } 2534 2535 transactionManager.ejbDestroyed(context); 2537 2538 } 2539 } catch (Exception ex) { 2540 _logger.log(Level.FINE, "Exception in destroy()", ex); 2541 } 2542 } 2543 } 2544 2545 } 2547 private static void appendStat(StringBuffer sbuf, String header, Map map) { 2548 2549 sbuf.append("\n\t[").append(header).append(": "); 2550 if (map != null) { 2551 Iterator iter = map.keySet().iterator(); 2552 2553 while (iter.hasNext()) { 2554 String name = (String )iter.next(); 2555 sbuf.append(name).append("=").append(map.get(name)) 2556 .append("; "); 2557 } 2558 } else { 2559 sbuf.append("NONE"); 2560 } 2561 sbuf.append("]"); 2562 } 2563 2564 private void createCaches() throws Exception { 2565 2566 cacheProp = new CacheProperties(); 2567 2568 int cacheSize = cacheProp.maxCacheSize; 2569 int numberOfVictimsToSelect = cacheProp.numberOfVictimsToSelect; 2570 float loadFactor = DEFAULT_LOAD_FACTOR; 2571 idleTimeout = cacheProp.cacheIdleTimeoutInSeconds * 1000; 2572 2573 createReadyStore(cacheSize, numberOfVictimsToSelect, loadFactor, 2574 idleTimeout); 2575 2576 createEJBObjectStores(cacheSize, numberOfVictimsToSelect, 2577 idleTimeout); 2578 2579 } 2580 2581 protected void createReadyStore(int cacheSize, int numberOfVictimsToSelect, 2582 float loadFactor, long idleTimeout) throws Exception 2583 { 2584 idleTimeout = (idleTimeout <= 0) ? -1 : idleTimeout; 2585 if (cacheSize <= 0 && idleTimeout <= 0) { 2586 readyStore = new BaseCache(); 2587 cacheSize = DEFAULT_CACHE_SIZE; 2588 readyStore.init(cacheSize, loadFactor, null); 2589 } else { 2590 cacheSize = (cacheSize <= 0) ? DEFAULT_CACHE_SIZE : cacheSize; 2591 LruCache lru = new LruCache(DEFAULT_CACHE_SIZE); 2592 if (numberOfVictimsToSelect >= 0) { 2593 loadFactor = (float) (1.0 - (1.0 * 2594 numberOfVictimsToSelect/cacheSize)); 2595 } 2596 lru.init(cacheSize, idleTimeout, loadFactor, null); 2597 readyStore = lru; 2598 readyStore.addCacheListener(this); 2599 } 2600 2601 if (idleTimeout > 0) { 2602 idleBeansPassivator = setupIdleBeansPassivator(readyStore); 2603 } 2604 } 2605 2606 protected void createEJBObjectStores(int cacheSize, 2607 int numberOfVictimsToSelect, long idleTimeout) throws Exception { 2608 2609 EJBObjectCache lru = null; 2610 String ejbName = ejbDescriptor.getName(); 2611 idleTimeout = (idleTimeout <= 0) ? -1 : idleTimeout; 2612 2613 if (cacheSize <= 0 && idleTimeout <= 0) { 2614 ejbObjectStore = new UnboundedEJBObjectCache(ejbName); 2615 ejbObjectStore.init(DEFAULT_CACHE_SIZE, numberOfVictimsToSelect, 0L, 2616 (float)1.0, null); 2617 2618 ejbLocalObjectStore = new UnboundedEJBObjectCache(ejbName); 2619 ejbLocalObjectStore.init(DEFAULT_CACHE_SIZE, 2620 numberOfVictimsToSelect, 0L, (float)1.0, null); 2621 } else { 2622 cacheSize = (cacheSize <= 0) ? DEFAULT_CACHE_SIZE : cacheSize; 2623 ejbObjectStore = new FIFOEJBObjectCache(ejbName); 2624 ejbObjectStore.init(cacheSize, numberOfVictimsToSelect, idleTimeout, 2625 (float)1.0, null); 2626 ejbObjectStore.setEJBObjectCacheListener( 2627 new EJBObjectCacheVictimHandler()); 2628 2629 ejbLocalObjectStore = new FIFOEJBObjectCache(ejbName); 2630 ejbLocalObjectStore.init(cacheSize, numberOfVictimsToSelect, 2631 idleTimeout, (float)1.0, null); 2632 ejbLocalObjectStore.setEJBObjectCacheListener( 2633 new LocalEJBObjectCacheVictimHandler()); 2634 } 2635 2636 if (idleTimeout > 0) { 2637 idleEJBObjectPassivator = setupIdleBeansPassivator(ejbObjectStore); 2638 idleLocalEJBObjectPassivator = 2639 setupIdleBeansPassivator(ejbLocalObjectStore); 2640 } 2641 } 2642 2643 protected EntityContextImpl getReadyEJB(Invocation inv) { 2644 Object primaryKey = inv.ejbObject.getKey(); 2645 EntityContextImpl context = null; 2646 context = (EntityContextImpl)readyStore.remove(primaryKey); 2649 if (context == null || context.getState() != READY) { 2650 context = activateEJBFromPool(primaryKey, inv); 2651 } 2652 return context; 2653 } 2655 protected void addReadyEJB(EntityContextImpl context) { 2656 Object primaryKey = context.getPrimaryKey(); 2658 context.setState(READY); 2659 readyStore.add(primaryKey, context); 2660 } 2661 2662 protected void destroyReadyStoreOnUndeploy() { 2663 if (readyStore == null) { 2664 return; 2665 } 2666 2667 synchronized ( readyStore ) { 2669 2670 Iterator beans = readyStore.values(); 2671 while ( beans.hasNext() ) { 2672 EJBContextImpl ctx = (EJBContextImpl)beans.next(); 2673 transactionManager.ejbDestroyed(ctx); 2674 } 2675 } 2676 readyStore.destroy(); 2677 readyStore = null; 2678 } 2679 2680 protected void removeContextFromReadyStore(Object primaryKey, 2681 EntityContextImpl context) { 2682 readyStore.remove(primaryKey, context); 2683 } 2684 2685 protected void doFlush( Invocation inv ) { 2686 if( !inv.invocationInfo.flushEnabled || 2687 inv.exception != null ) { 2688 return; 2689 } 2690 2691 if( !isContainerManagedPers ) { 2692 _logger.log(Level.WARNING, 2694 "Cannot turn on flush-enabled-at-end-of-method for a bean with Bean Managed Persistence"); 2695 return; 2696 } 2697 2698 InvocationInfo invInfo = inv.invocationInfo; 2699 Throwable exception = inv.exception; 2700 EntityContextImpl context = (EntityContextImpl)inv.context; 2701 Transaction tx = context.getTransaction(); 2702 2703 if( tx == null) { 2706 return; 2707 } 2708 2709 try { 2711 if( context.getRollbackOnly() ) { 2712 return; 2713 } 2714 } catch( Throwable ex ) { 2715 _logger.log(Level.WARNING, "Exception when calling getRollbackOnly", ex); 2716 return; 2717 } 2718 2719 if ( invInfo.isBusinessMethod ) { 2720 try { 2721 storeAllBeansInTx( tx ); 2723 } catch( Throwable ex ) { 2724 inv.exception = ex; 2725 return; 2726 } 2727 } 2728 2729 try { 2730 BeanStateSynchronization pmcontract = (BeanStateSynchronization)inv.ejb; 2731 pmcontract.ejb__flush(); 2732 } catch( Throwable ex ) { 2733 if( invInfo.startsWithCreate ) { 2735 CreateException ejbEx = new CreateException(); 2736 ejbEx.initCause(ex); 2737 inv.exception = ejbEx; 2738 } else if( invInfo.startsWithRemove ) { 2739 RemoveException ejbEx = new RemoveException(); 2740 ejbEx.initCause(ex); 2741 inv.exception = ejbEx; 2742 } else { 2743 EJBException ejbEx = new EJBException(); 2744 ejbEx.initCause(ex); 2745 inv.exception = ejbEx; 2746 } 2747 2748 return; 2749 } 2750 2751 } 2753 private void storeAllBeansInTx(Transaction tx) { 2754 Vector beans = (Vector)txBeanTable.get(tx); 2756 if ( beans == null ) { 2757 return; 2759 } 2760 2761 Iterator itr = beans.iterator(); 2762 while ( itr.hasNext() ) { 2763 EntityContextImpl ctx = (EntityContextImpl)itr.next(); 2764 if ( ctx.getState() == INCOMPLETE_TX && ctx.isDirty() ) { 2765 EntityContainer cont = (EntityContainer)ctx.getContainer(); 2768 cont.enlistResourcesAndStore(ctx); 2769 } 2770 } 2771 } 2772 2773 2774 protected class LocalEJBObjectCacheVictimHandler 2775 implements EJBObjectCacheListener, 2776 com.sun.enterprise.util.threadpool.Servicable 2777 { 2778 2779 protected Object lock = new Object (); 2780 protected boolean addedTask = false; 2781 protected ArrayList keys = new ArrayList(16); 2782 2783 protected LocalEJBObjectCacheVictimHandler() { 2784 } 2785 2786 public void handleOverflow(Object key) { 2788 doCleanup(key); 2789 } 2790 2791 public void handleBatchOverflow(ArrayList paramKeys) { 2792 int size = paramKeys.size(); 2793 synchronized (lock) { 2794 for (int i=0; i<size; i++) { 2795 keys.add(paramKeys.get(i)); 2796 } 2797 if (addedTask == true) { 2798 return; 2799 } 2800 addedTask = true; 2801 } 2802 2803 try { 2804 ContainerWorkPool.addLast(this); 2805 } catch (Exception ex) { 2806 _logger.log(Level.WARNING, "ejb.entity_add_async_task", ex); 2807 synchronized (lock) { 2808 addedTask = false; 2809 } 2810 } 2811 } 2812 2813 2814 public void prolog() { } 2815 2816 public void epilog() { } 2817 2818 public void service() { run(); } 2819 2820 public void run() { 2821 final Thread currentThread = Thread.currentThread(); 2822 final ClassLoader previousClassLoader = 2823 currentThread.getContextClassLoader(); 2824 final ClassLoader myClassLoader = loader; 2825 2826 try { 2827 2829 java.security.AccessController.doPrivileged( 2830 new java.security.PrivilegedAction () { 2831 public java.lang.Object run() { 2832 currentThread.setContextClassLoader(loader); 2833 return null; 2834 } 2835 } 2836 ); 2837 2838 ArrayList localKeys = null; 2839 do { 2840 synchronized (lock) { 2841 int size = keys.size(); 2842 if (size == 0) { 2843 return; 2844 } 2845 2846 localKeys = keys; 2847 keys = new ArrayList(16); 2848 } 2849 2850 int maxIndex = localKeys.size(); 2851 for (int i=0; i<maxIndex; i++) { 2852 doCleanup(localKeys.get(i)); 2853 } 2854 } while (true); 2855 2856 } catch (Throwable th) { 2857 th.printStackTrace(); 2858 } finally { 2859 synchronized (lock) { 2860 addedTask = false; 2861 } 2862 java.security.AccessController.doPrivileged( 2863 new java.security.PrivilegedAction () { 2864 public java.lang.Object run() { 2865 currentThread.setContextClassLoader(previousClassLoader); 2866 return null; 2867 } 2868 } 2869 ); 2870 } 2871 } 2872 2873 protected void doCleanup(Object key) { 2874 ejbLocalObjectStore.remove(key, false); 2875 } 2876 } 2878 protected class EJBObjectCacheVictimHandler 2879 extends LocalEJBObjectCacheVictimHandler 2880 { 2881 2882 protected EJBObjectCacheVictimHandler() { 2883 } 2884 2885 protected void doCleanup(Object key) { 2886 removeEJBObjectFromStore(key, false); 2887 } 2888 } 2890 2891 2892 class EntityCacheStatsProvider 2893 implements com.sun.ejb.spi.stats.EJBCacheStatsProvider 2894 { 2895 private BaseCache cache; 2896 private String configData; 2897 private int confMaxCacheSize; 2898 2899 EntityCacheStatsProvider(BaseCache cache, int maxCacheSize) { 2900 this.cache = cache; 2901 this.confMaxCacheSize = maxCacheSize; 2902 } 2903 2904 public int getCacheHits() { 2905 return ((Integer ) cache.getStatByName( 2906 Constants.STAT_BASECACHE_HIT_COUNT)).intValue(); 2907 } 2908 2909 public int getCacheMisses() { 2910 return ((Integer ) cache.getStatByName( 2911 Constants.STAT_BASECACHE_MISS_COUNT)).intValue(); 2912 } 2913 2914 public int getNumBeansInCache() { 2915 return cache.getEntryCount(); 2916 } 2917 2918 public int getNumExpiredSessionsRemoved() { 2919 return 0; 2920 } 2921 2922 public int getNumPassivationErrors() { 2923 return totalPassivationErrors; 2924 } 2925 2926 public int getNumPassivations() { 2927 return totalPassivations; 2928 } 2929 2930 public int getNumPassivationSuccess() { 2931 return totalPassivations - totalPassivationErrors; 2932 } 2933 2934 public int getMaxCacheSize() { 2935 return this.confMaxCacheSize; 2936 } 2937 2938 public void appendStats(StringBuffer sbuf) { 2939 sbuf.append("[Cache: ") 2940 .append("Size=").append(getNumBeansInCache()).append("; ") 2941 .append("HitCount=").append(getCacheHits()).append("; ") 2942 .append("MissCount=").append(getCacheMisses()).append("; ") 2943 .append("Passivations=").append(getNumPassivations()).append("; "); 2944 if (configData != null) { 2945 sbuf.append(configData); 2946 } 2947 sbuf.append("]"); 2948 } 2949 2950 } 2952} 2953 2954class ActiveTxCache { 2956 2957 private static Logger _logger = 2958 LogDomains.getLogger(LogDomains.EJB_LOGGER); 2959 2960 private EntityContextImpl[] buckets; 2961 private int bucketMask; 2962 2963 ActiveTxCache(int numBuckets) { 2964 this.bucketMask = numBuckets - 1; 2965 initialize(); 2966 } 2967 2968 EntityContextImpl get(BaseContainer container, Object pk) { 2969 int pkHashCode = pk.hashCode(); 2970 int index = getIndex(pkHashCode); 2971 2972 EntityContextImpl ctx = buckets[index]; 2973 while (ctx != null) { 2974 if (ctx.doesMatch(container, pkHashCode, pk)) { 2975 return ctx; 2976 } 2977 ctx = ctx._getNext(); 2978 } 2979 2980 return null; 2981 } 2982 2983 void add(EntityContextImpl ctx) { 2984 ctx.cachePrimaryKey(); 2985 int index = getIndex(ctx._getPKHashCode()); 2986 ctx._setNext(buckets[index]); 2987 buckets[index] = ctx; 2988 } 2989 2990 EntityContextImpl remove(BaseContainer container, Object pk) { 2991 int pkHashCode = pk.hashCode(); 2992 int index = getIndex(pkHashCode); 2993 2994 EntityContextImpl ctx = buckets[index]; 2995 for (EntityContextImpl prev = null; ctx != null; ctx = ctx._getNext()) { 2996 if (ctx.doesMatch(container, pkHashCode, pk)) { 2997 if (prev == null) { 2998 buckets[index] = ctx._getNext(); 2999 } else { 3000 prev._setNext(ctx._getNext()); 3001 } 3002 ctx._setNext(null); 3003 break; 3004 } 3005 prev = ctx; 3006 } 3007 3008 return ctx; 3009 } 3010 3011 EntityContextImpl remove(Object pk, EntityContextImpl existingCtx) { 3013 int pkHashCode = pk.hashCode(); 3014 int index = getIndex(pkHashCode); 3015 3016 EntityContextImpl ctx = buckets[index]; 3017 for (EntityContextImpl prev = null; ctx != null; ctx = ctx._getNext()) { 3018 if (ctx == existingCtx) { 3019 if (prev == null) { 3020 buckets[index] = ctx._getNext(); 3021 } else { 3022 prev._setNext(ctx._getNext()); 3023 } 3024 ctx._setNext(null); 3025 break; 3026 } 3027 prev = ctx; 3028 } 3029 3030 return ctx; 3031 } 3032 3033 private void initialize() { 3034 buckets = new EntityContextImpl[bucketMask+1]; 3035 } 3036 3037 private final int getIndex(int hashCode) { 3038 return (hashCode & bucketMask); 3039 } 3040 3041} 3042 | Popular Tags |