1 23 package com.sun.ejb.containers; 24 25 import java.rmi.RemoteException ; 26 import java.lang.reflect.Method ; 27 import javax.ejb.*; 28 29 import com.sun.ejb.*; 30 import com.sun.enterprise.*; 31 32 36 37 public class EntityContextImpl 38 extends EJBContextImpl 39 implements EntityContext 40 { 41 private int lastTxStatus=-1; 42 private boolean newlyActivated = false; 43 private int nCallsInProgress = 0; 44 private boolean dirty = false; 45 private boolean inUnsetEntityContext = false; 46 private boolean inEjbLoad = false; 47 private boolean inEjbStore = false; 48 49 private boolean cascadeDeleteBeforeEJBRemove = false; 50 private boolean cascadeDeleteAfterSuperEJBRemove = false; 51 52 private Object _primaryKey; 55 private int _pkHashCode; 56 private EntityContextImpl _next; 57 58 EntityContextImpl(Object ejb, BaseContainer container) { 59 super(ejb, container); 60 } 61 62 int getLastTransactionStatus() { 63 return lastTxStatus; 64 } 65 66 void setLastTransactionStatus(int status) { 67 lastTxStatus = status; 68 } 69 70 void setInUnsetEntityContext(boolean flag) { 71 inUnsetEntityContext = flag; 72 } 73 74 void setInEjbLoad(boolean flag) { 75 inEjbLoad = flag; 76 } 77 78 void setInEjbStore(boolean flag) { 79 inEjbStore = flag; 80 } 81 82 boolean isDirty() { 83 return dirty; 84 } 85 86 void setDirty(boolean b) { 87 dirty = b; 88 } 89 90 void setState(int s) { 92 state = s; 93 if ( state == EntityContainer.POOLED || 94 state == EntityContainer.DESTROYED ) 95 { 96 dirty = false; 97 } 98 } 99 100 101 boolean isNewlyActivated() { 102 return newlyActivated; 103 } 104 105 void setNewlyActivated(boolean b) { 106 newlyActivated = b; 107 } 108 109 boolean hasReentrantCall() { 110 return (nCallsInProgress > 1); 111 } 112 113 synchronized void decrementCalls() { 114 nCallsInProgress--; 115 } 116 117 synchronized void incrementCalls() { 118 nCallsInProgress++; 119 } 120 121 boolean hasIdentity() { 122 return( (ejbObjectImpl != null) || (ejbLocalObjectImpl != null) ); 123 } 124 125 128 public Object getPrimaryKey() throws IllegalStateException { 129 if ( ejbObjectImpl == null && ejbLocalObjectImpl == null ) { 130 throw new IllegalStateException ("Primary key not available"); 133 } 134 135 if ( ejbLocalObjectImpl != null ) 136 return ejbLocalObjectImpl.getKey(); 137 else 138 return ejbObjectImpl.getKey(); 139 } 140 141 144 public EJBObject getEJBObject() 145 throws IllegalStateException 146 { 147 if (! isRemoteInterfaceSupported) { 148 throw new IllegalStateException ("EJBObject not available"); 149 } 150 151 if ( ejbStub == null ) { 152 Object pkey = getPrimaryKey(); ejbStub = ((EntityContainer) container).getEJBObjectStub(pkey, null); 154 } 155 156 return ejbStub; 157 } 158 159 public TimerService getTimerService() throws IllegalStateException { 160 if( state == NOT_INITIALIZED || inUnsetEntityContext || inFinder() ) { 161 throw new IllegalStateException ("Operation not allowed"); 162 } 163 164 ContainerFactoryImpl cf = (ContainerFactoryImpl) 165 Switch.getSwitch().getContainerFactory(); 166 EJBTimerService timerService = cf.getEJBTimerService(); 167 if( timerService == null ) { 168 throw new EJBException("EJB Timer service not available"); 169 } 170 return new EJBTimerServiceWrapper(timerService, 171 (EntityContextImpl) this); 172 } 173 174 protected void checkAccessToCallerSecurity() 175 throws IllegalStateException 176 { 177 if( state == NOT_INITIALIZED || inUnsetEntityContext ) { 178 throw new IllegalStateException ("Operation not allowed"); 179 } 180 checkActivatePassivate(); 181 182 if (inEjbLoad || inEjbStore) { 183 return; 188 } 189 } 190 191 public void checkTimerServiceMethodAccess() 192 throws IllegalStateException 193 { 194 195 if( (state == NOT_INITIALIZED) || 198 inUnsetEntityContext || 199 inFinder() || 200 inActivatePassivate() || 201 !hasIdentity() ) { 202 throw new IllegalStateException ("Operation not allowed"); 203 } 204 205 } 206 207 public final boolean isCascadeDeleteAfterSuperEJBRemove() { 208 return cascadeDeleteAfterSuperEJBRemove; 209 } 210 211 public final void setCascadeDeleteAfterSuperEJBRemove(boolean value) { 212 this.cascadeDeleteAfterSuperEJBRemove = value; 213 } 214 215 public final boolean isCascadeDeleteBeforeEJBRemove() { 216 return cascadeDeleteBeforeEJBRemove; 217 } 218 219 public final void setCascadeDeleteBeforeEJBRemove(boolean value) { 220 this.cascadeDeleteBeforeEJBRemove = value; 221 } 222 223 private boolean inFinder() { 224 boolean inFinder = false; 225 ComponentInvocation i = 226 container.invocationManager.getCurrentInvocation(); 227 if ( i instanceof Invocation ) { 228 Invocation inv = (Invocation) i; 229 Method currentMethod = inv.method; 230 inFinder = ( (currentMethod != null) && inv.isHome && 231 currentMethod.getName().startsWith("find") ); 232 } 233 return inFinder; 234 } 235 236 final void cachePrimaryKey() { 238 Object pk = getPrimaryKey(); 239 this._primaryKey = pk; 240 this._pkHashCode = pk.hashCode(); 241 } 242 243 final void clearCachedPrimaryKey() { 244 this._primaryKey = null; 245 } 246 247 final boolean doesMatch(BaseContainer baseContainer, int pkHashCode, Object pk) { 249 return ( 250 (container == baseContainer) 251 && (_pkHashCode == pkHashCode) 252 && (_primaryKey.equals(pk)) 253 ); 254 } 255 256 final void _setNext(EntityContextImpl val) { 257 this._next = val; 258 } 259 260 final EntityContextImpl _getNext() { 261 return _next; 262 } 263 264 final int _getPKHashCode() { 265 return this._pkHashCode; 266 } 267 268 } 269 | Popular Tags |