KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > EntityEnterpriseContext


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb;
23
24 import java.io.Serializable JavaDoc;
25 import java.rmi.RemoteException JavaDoc;
26 import java.security.Principal JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Date JavaDoc;
29
30 import javax.ejb.EJBContext JavaDoc;
31 import javax.ejb.EJBException JavaDoc;
32 import javax.ejb.EJBHome JavaDoc;
33 import javax.ejb.EJBLocalHome JavaDoc;
34 import javax.ejb.EJBLocalObject JavaDoc;
35 import javax.ejb.EJBObject JavaDoc;
36 import javax.ejb.EntityBean JavaDoc;
37 import javax.ejb.EntityContext JavaDoc;
38 import javax.ejb.Timer JavaDoc;
39 import javax.ejb.TimerService JavaDoc;
40 import javax.transaction.UserTransaction JavaDoc;
41
42 import org.jboss.ejb.plugins.lock.NonReentrantLock;
43
44
45 /**
46  * The EntityEnterpriseContext is used to associate EntityBean instances
47  * with metadata about it.
48  *
49  * @see EnterpriseContext
50  *
51  * @author <a HREF="mailto:rickard.oberg@telkel.com">Rickard �berg</a>
52  * @author <a HREF="mailto:marc.fleury@telkel.com">Marc Fleury</a>
53  * @author <a HREF="mailto:docodan@mvcsoft.com">Daniel OConnor</a>
54  * @version $Revision: 43998 $
55  */

56 public class EntityEnterpriseContext extends EnterpriseContext
57 {
58    private EJBObject JavaDoc ejbObject;
59    private EJBLocalObject JavaDoc ejbLocalObject;
60    private EntityContext JavaDoc ctx;
61     
62    /**
63     * True if this instance has been registered with the TM for transactional
64     * demarcation.
65     */

66    private boolean hasTxSynchronization = false;
67
68    /**
69     * Specifies whether the instance is associated with a transaction and should be synchronized.
70     */

71    private GlobalTxEntityMap.TxAssociation txAssociation = GlobalTxEntityMap.NONE;
72
73    /**
74     * True if this instances' state is valid when a bean is called the state
75     * is not synchronized with the DB but "valid" as long as the transaction
76     * runs.
77     */

78    private boolean valid = false;
79     
80    /**
81     * Is this context in a readonly invocation.
82     */

83    private boolean readOnly = false;
84
85    /**
86     * The persistence manager may attach any metadata it wishes to this
87     * context here.
88     */

89    private Object JavaDoc persistenceCtx;
90     
91    /** The cacheKey for this context */
92    private Object JavaDoc key;
93
94    private NonReentrantLock methodLock = new NonReentrantLock();
95
96    /** used to force passivation after commit even if the co is not C */
97    private boolean passivateAfterCommit;
98
99    public EntityEnterpriseContext(Object JavaDoc instance, Container con)
100       throws RemoteException JavaDoc
101    {
102       super(instance, con);
103       ctx = new EntityContextImpl();
104       try
105       {
106          AllowedOperationsAssociation.pushInMethodFlag(IN_SET_ENTITY_CONTEXT);
107          ((EntityBean JavaDoc)instance).setEntityContext(ctx);
108       }
109       finally
110       {
111          AllowedOperationsAssociation.popInMethodFlag();
112       }
113    }
114     
115    /**
116     * A non-reentrant deadlock detectable lock that is used to protected against
117     * entity bean reentrancy.
118     * @return
119     */

120    public NonReentrantLock getMethodLock()
121    {
122       return methodLock;
123    }
124
125    public void clear()
126    {
127       super.clear();
128       
129       hasTxSynchronization = false;
130       valid = false;
131       readOnly = false;
132       key = null;
133       persistenceCtx = null;
134       ejbObject = null;
135       ejbLocalObject = null;
136       txAssociation = GlobalTxEntityMap.NONE;
137       passivateAfterCommit = false;
138    }
139     
140    public void discard() throws RemoteException JavaDoc
141    {
142       ((EntityBean JavaDoc)instance).unsetEntityContext();
143    }
144     
145    public EJBContext JavaDoc getEJBContext()
146    {
147       return ctx;
148    }
149     
150    public void setEJBObject(EJBObject JavaDoc eo)
151    {
152       ejbObject = eo;
153    }
154     
155    public EJBObject JavaDoc getEJBObject()
156    {
157       // Context can have no EJBObject (created by finds) in which case
158
// we need to wire it at call time
159
if(ejbObject == null && con.getProxyFactory() != null)
160       {
161          ejbObject = (EJBObject JavaDoc)con.getProxyFactory().getEntityEJBObject(id);
162       }
163       return ejbObject;
164    }
165     
166    public void setEJBLocalObject(EJBLocalObject JavaDoc eo)
167    {
168       ejbLocalObject = eo;
169    }
170     
171    public EJBLocalObject JavaDoc getEJBLocalObject()
172    {
173       if(ejbLocalObject == null && con.getLocalHomeClass() != null)
174       {
175          ejbLocalObject = ((EntityContainer)con).getLocalProxyFactory().getEntityEJBLocalObject(id);
176       }
177       return ejbLocalObject;
178    }
179     
180    public void setCacheKey(Object JavaDoc key)
181    {
182       this.key = key;
183    }
184     
185    public Object JavaDoc getCacheKey()
186    {
187       return key;
188    }
189     
190    public void setPersistenceContext(Object JavaDoc ctx)
191    {
192       this.persistenceCtx = ctx;
193    }
194     
195    public Object JavaDoc getPersistenceContext()
196    {
197       return persistenceCtx;
198    }
199     
200    public void hasTxSynchronization(boolean value)
201    {
202       hasTxSynchronization = value;
203    }
204     
205    public boolean hasTxSynchronization()
206    {
207       return hasTxSynchronization;
208    }
209
210    public GlobalTxEntityMap.TxAssociation getTxAssociation()
211    {
212       return txAssociation;
213    }
214
215    public void setTxAssociation(GlobalTxEntityMap.TxAssociation txAssociation)
216    {
217       this.txAssociation = txAssociation;
218    }
219
220    public void setValid(boolean valid)
221    {
222       this.valid = valid;
223    }
224     
225    public boolean isValid()
226    {
227       return valid;
228    }
229
230     public void setReadOnly(boolean readOnly)
231    {
232       this.readOnly = readOnly;
233    }
234     
235    public boolean isReadOnly()
236    {
237       return readOnly;
238    }
239
240    public boolean isPassivateAfterCommit()
241    {
242       return passivateAfterCommit;
243    }
244
245    public void setPassivateAfterCommit(boolean passivateAfterCommit)
246    {
247       this.passivateAfterCommit = passivateAfterCommit;
248    }
249
250    public String JavaDoc toString()
251    {
252       return getContainer().getBeanMetaData().getEjbName() + '#' + getId();
253    }
254
255    protected class EntityContextImpl
256       extends EJBContextImpl
257       implements EntityContext JavaDoc
258    {
259       public EJBHome JavaDoc getEJBHome()
260       {
261          AllowedOperationsAssociation.assertAllowedIn("getEJBHome",
262                  IN_SET_ENTITY_CONTEXT | IN_UNSET_ENTITY_CONTEXT |
263                  IN_EJB_CREATE | IN_EJB_POST_CREATE | IN_EJB_REMOVE | IN_EJB_FIND | IN_EJB_HOME |
264                  IN_EJB_ACTIVATE | IN_EJB_PASSIVATE | IN_EJB_LOAD | IN_EJB_STORE | IN_BUSINESS_METHOD |
265                  IN_EJB_TIMEOUT);
266
267          return super.getEJBHome();
268       }
269
270       public EJBLocalHome JavaDoc getEJBLocalHome()
271       {
272          AllowedOperationsAssociation.assertAllowedIn("getEJBLocalHome",
273                  IN_SET_ENTITY_CONTEXT | IN_UNSET_ENTITY_CONTEXT |
274                  IN_EJB_CREATE | IN_EJB_POST_CREATE | IN_EJB_REMOVE | IN_EJB_FIND | IN_EJB_HOME |
275                  IN_EJB_ACTIVATE | IN_EJB_PASSIVATE | IN_EJB_LOAD | IN_EJB_STORE | IN_BUSINESS_METHOD |
276                  IN_EJB_TIMEOUT);
277
278          return super.getEJBLocalHome();
279       }
280
281       public Principal JavaDoc getCallerPrincipal()
282       {
283          AllowedOperationsAssociation.assertAllowedIn("getCallerPrincipal",
284                  IN_EJB_CREATE | IN_EJB_POST_CREATE | IN_EJB_REMOVE | IN_EJB_FIND | IN_EJB_HOME |
285                  IN_EJB_LOAD | IN_EJB_STORE | IN_BUSINESS_METHOD |
286                  IN_EJB_TIMEOUT);
287
288          return super.getCallerPrincipal();
289       }
290
291       public boolean getRollbackOnly()
292       {
293          AllowedOperationsAssociation.assertAllowedIn("getRollbackOnly",
294                  IN_EJB_CREATE | IN_EJB_POST_CREATE | IN_EJB_REMOVE | IN_EJB_FIND | IN_EJB_HOME |
295                  IN_EJB_LOAD | IN_EJB_STORE | IN_BUSINESS_METHOD |
296                  IN_EJB_TIMEOUT);
297
298          return super.getRollbackOnly();
299       }
300
301       public void setRollbackOnly()
302       {
303          AllowedOperationsAssociation.assertAllowedIn("setRollbackOnly",
304                  IN_EJB_CREATE | IN_EJB_POST_CREATE | IN_EJB_REMOVE | IN_EJB_FIND | IN_EJB_HOME |
305                  IN_EJB_LOAD | IN_EJB_STORE | IN_BUSINESS_METHOD |
306                  IN_EJB_TIMEOUT);
307
308          super.setRollbackOnly();
309       }
310
311       public boolean isCallerInRole(String JavaDoc id)
312       {
313          AllowedOperationsAssociation.assertAllowedIn("getCallerInRole",
314                  IN_EJB_CREATE | IN_EJB_POST_CREATE | IN_EJB_REMOVE | IN_EJB_FIND | IN_EJB_HOME |
315                  IN_EJB_LOAD | IN_EJB_STORE | IN_BUSINESS_METHOD |
316                  IN_EJB_TIMEOUT);
317          return super.isCallerInRole(id);
318       }
319
320       public UserTransaction JavaDoc getUserTransaction()
321       {
322          AllowedOperationsAssociation.assertAllowedIn("getUserTransaction",
323                  NOT_ALLOWED);
324          return super.getUserTransaction();
325       }
326
327       public EJBObject JavaDoc getEJBObject()
328       {
329          AllowedOperationsAssociation.assertAllowedIn("getEJBObject",
330                  IN_EJB_POST_CREATE | IN_EJB_REMOVE |
331                  IN_EJB_ACTIVATE | IN_EJB_PASSIVATE | IN_EJB_LOAD | IN_EJB_STORE | IN_BUSINESS_METHOD |
332                  IN_EJB_TIMEOUT);
333
334          if(((EntityContainer)con).getRemoteClass() == null)
335          {
336             throw new IllegalStateException JavaDoc( "No remote interface defined." );
337          }
338
339          if (ejbObject == null)
340          {
341             // Create a new CacheKey
342
Object JavaDoc cacheKey = ((EntityCache)((EntityContainer)con).getInstanceCache()).createCacheKey(id);
343             EJBProxyFactory proxyFactory = con.getProxyFactory();
344             if(proxyFactory == null)
345             {
346                String JavaDoc defaultInvokerName = con.getBeanMetaData().
347                   getContainerConfiguration().getDefaultInvokerName();
348                proxyFactory = con.lookupProxyFactory(defaultInvokerName);
349             }
350             ejbObject = (EJBObject JavaDoc)proxyFactory.getEntityEJBObject(cacheKey);
351          }
352
353          return ejbObject;
354       }
355         
356       public EJBLocalObject JavaDoc getEJBLocalObject()
357       {
358          AllowedOperationsAssociation.assertAllowedIn("getEJBLocalObject",
359                  IN_EJB_POST_CREATE | IN_EJB_REMOVE |
360                  IN_EJB_ACTIVATE | IN_EJB_PASSIVATE | IN_EJB_LOAD | IN_EJB_STORE | IN_BUSINESS_METHOD |
361                  IN_EJB_TIMEOUT);
362
363          if (con.getLocalHomeClass()==null)
364             throw new IllegalStateException JavaDoc( "No local interface for bean." );
365          
366          if (ejbLocalObject == null)
367          {
368             Object JavaDoc cacheKey = ((EntityCache)((EntityContainer)con).getInstanceCache()).createCacheKey(id);
369             ejbLocalObject = ((EntityContainer)con).getLocalProxyFactory().getEntityEJBLocalObject(cacheKey);
370          }
371          return ejbLocalObject;
372       }
373         
374       public Object JavaDoc getPrimaryKey()
375       {
376          AllowedOperationsAssociation.assertAllowedIn("getPrimaryKey",
377                  IN_EJB_POST_CREATE | IN_EJB_REMOVE |
378                  IN_EJB_ACTIVATE | IN_EJB_PASSIVATE | IN_EJB_LOAD | IN_EJB_STORE | IN_BUSINESS_METHOD |
379                  IN_EJB_TIMEOUT);
380
381          return id;
382       }
383
384       public TimerService JavaDoc getTimerService() throws IllegalStateException JavaDoc
385       {
386          AllowedOperationsAssociation.assertAllowedIn("getTimerService",
387                  IN_EJB_CREATE | IN_EJB_POST_CREATE | IN_EJB_REMOVE | IN_EJB_HOME |
388                  IN_EJB_ACTIVATE | IN_EJB_PASSIVATE | IN_EJB_LOAD | IN_EJB_STORE | IN_BUSINESS_METHOD |
389                  IN_EJB_TIMEOUT);
390
391          return new TimerServiceWrapper(this, getContainer().getTimerService(id));
392       }
393    }
394
395    /**
396     * Delegates to the underlying TimerService, after checking access
397     */

398    public class TimerServiceWrapper implements TimerService JavaDoc
399    {
400
401       //private EnterpriseContext.EJBContextImpl context;
402
private TimerService JavaDoc timerService;
403
404       public TimerServiceWrapper(EnterpriseContext.EJBContextImpl ctx, TimerService JavaDoc timerService)
405       {
406          //this.context = ctx;
407
this.timerService = timerService;
408       }
409
410       public Timer JavaDoc createTimer(long duration, Serializable JavaDoc info) throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException JavaDoc
411       {
412          assertAllowedIn("TimerService.createTimer");
413          return timerService.createTimer(duration, info);
414       }
415
416       public Timer JavaDoc createTimer(long initialDuration, long intervalDuration, Serializable JavaDoc info) throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException JavaDoc
417       {
418          assertAllowedIn("TimerService.createTimer");
419          return timerService.createTimer(initialDuration, intervalDuration, info);
420       }
421
422       public Timer JavaDoc createTimer(Date JavaDoc expiration, Serializable JavaDoc info) throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException JavaDoc
423       {
424          assertAllowedIn("TimerService.createTimer");
425          return timerService.createTimer(expiration, info);
426       }
427
428       public Timer JavaDoc createTimer(Date JavaDoc initialExpiration, long intervalDuration, Serializable JavaDoc info) throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException JavaDoc
429       {
430          assertAllowedIn("TimerService.createTimer");
431          return timerService.createTimer(initialExpiration, intervalDuration, info);
432       }
433
434       public Collection JavaDoc getTimers() throws IllegalStateException JavaDoc, EJBException JavaDoc
435       {
436          assertAllowedIn("TimerService.getTimers");
437          return timerService.getTimers();
438       }
439
440       private void assertAllowedIn(String JavaDoc timerMethod)
441       {
442          AllowedOperationsAssociation.assertAllowedIn(timerMethod,
443                  IN_EJB_POST_CREATE | IN_EJB_REMOVE | IN_EJB_LOAD | IN_EJB_STORE |
444                  IN_BUSINESS_METHOD | IN_EJB_TIMEOUT);
445       }
446    }
447 }
Popular Tags