KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > containers > EntityContextImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.ejb.containers;
24
25 import java.rmi.RemoteException JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import javax.ejb.*;
28
29 import com.sun.ejb.*;
30 import com.sun.enterprise.*;
31
32 /**
33  * Implementation of EJBContext for EntityBeans
34  *
35  */

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     //The following member variables are used to directly cache
53
// the EntityContext instead of enclosing it within a wrapper class.
54
private Object JavaDoc _primaryKey;
55     private int _pkHashCode;
56     private EntityContextImpl _next;
57
58     EntityContextImpl(Object JavaDoc 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     // overrides EJBContextImpl.setState
91
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     /**
126      * Implementation of EntityContext method.
127      */

128     public Object JavaDoc getPrimaryKey() throws IllegalStateException JavaDoc {
129         if ( ejbObjectImpl == null && ejbLocalObjectImpl == null ) {
130             // There is no ejbObjectImpl/localObject in ejbCreate, ejbFind,
131
// setEntityCtx etc
132
throw new IllegalStateException JavaDoc("Primary key not available");
133         }
134         
135         if ( ejbLocalObjectImpl != null )
136             return ejbLocalObjectImpl.getKey();
137         else
138             return ejbObjectImpl.getKey();
139     }
140     
141     /**
142      * Implementation of EntityContext method, overrides EJBContextImpl method.
143      */

144     public EJBObject getEJBObject()
145         throws IllegalStateException JavaDoc
146     {
147         if (! isRemoteInterfaceSupported) {
148             throw new IllegalStateException JavaDoc("EJBObject not available");
149         }
150
151         if ( ejbStub == null ) {
152             Object JavaDoc pkey = getPrimaryKey(); // throws IllegalStateException
153
ejbStub = ((EntityContainer) container).getEJBObjectStub(pkey, null);
154         }
155
156         return ejbStub;
157     }
158
159     public TimerService getTimerService() throws IllegalStateException JavaDoc {
160         if( state == NOT_INITIALIZED || inUnsetEntityContext || inFinder() ) {
161             throw new IllegalStateException JavaDoc("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 JavaDoc
176     {
177         if( state == NOT_INITIALIZED || inUnsetEntityContext ) {
178             throw new IllegalStateException JavaDoc("Operation not allowed");
179         }
180         checkActivatePassivate();
181         
182         if (inEjbLoad || inEjbStore) {
183             // Security access is allowed from these two methods. In the
184
// case that they are invoked as part of an ejbTimeout call,
185
// getCallerPrincipal will return null and isCallerInRole will
186
// be false
187
return;
188         }
189     }
190     
191     public void checkTimerServiceMethodAccess()
192         throws IllegalStateException JavaDoc
193     {
194         
195         // Prohibit access from constructor, setEntityContext, ejbCreate,
196
// ejbActivate, ejbPassivate, unsetEntityContext, ejbFind
197
if( (state == NOT_INITIALIZED) ||
198         inUnsetEntityContext ||
199         inFinder() ||
200         inActivatePassivate() ||
201         !hasIdentity() ) {
202             throw new IllegalStateException JavaDoc("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 JavaDoc currentMethod = inv.method;
230             inFinder = ( (currentMethod != null) && inv.isHome &&
231                          currentMethod.getName().startsWith("find") );
232         }
233         return inFinder;
234     }
235
236     //Called from EntityContainer after an ejb is obtained from the pool.
237
final void cachePrimaryKey() {
238     Object JavaDoc pk = getPrimaryKey();
239     this._primaryKey = pk;
240     this._pkHashCode = pk.hashCode();
241     }
242
243     final void clearCachedPrimaryKey() {
244     this._primaryKey = null;
245     }
246
247     //Called from IncompleteTxCache to get an already cached context
248
final boolean doesMatch(BaseContainer baseContainer, int pkHashCode, Object JavaDoc 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