KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > EntityInstanceCache


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.plugins;
23
24 import java.rmi.RemoteException JavaDoc;
25 import java.rmi.NoSuchObjectException JavaDoc;
26
27 import org.jboss.ejb.Container;
28 import org.jboss.ejb.EntityContainer;
29 import org.jboss.ejb.EnterpriseContext;
30 import org.jboss.ejb.EntityEnterpriseContext;
31 import org.jboss.ejb.GlobalTxEntityMap;
32 import org.jboss.metadata.EntityMetaData;
33 import org.jboss.metadata.ConfigurationMetaData;
34 import org.jboss.util.propertyeditor.PropertyEditors;
35
36 /**
37  * Cache subclass for entity beans.
38  *
39  * @author <a HREF="mailto:simone.bordet@compaq.com">Simone Bordet</a>
40  * @author <a HREF="bill@burkecentral.com">Bill Burke</a>
41  * @version $Revision: 44053 $
42  * @jmx:mbean extends="org.jboss.ejb.plugins.AbstractInstanceCacheMBean"
43  */

44 public class EntityInstanceCache
45    extends AbstractInstanceCache
46    implements org.jboss.ejb.EntityCache, EntityInstanceCacheMBean
47 {
48    // Constants -----------------------------------------------------
49

50    // Attributes ----------------------------------------------------
51
/* The container */
52    private EntityContainer m_container;
53
54    // Static --------------------------------------------------------
55

56    // Constructors --------------------------------------------------
57

58    // Public --------------------------------------------------------
59

60    /* From ContainerPlugin interface */
61    public void setContainer(Container c)
62    {
63       m_container = (EntityContainer)c;
64    }
65
66    // Z implementation ----------------------------------------------
67
public Object JavaDoc createCacheKey(Object JavaDoc id)
68    {
69       return id;
70    }
71
72    // Y overrides ---------------------------------------------------
73
public EnterpriseContext get(Object JavaDoc id)
74       throws RemoteException JavaDoc, NoSuchObjectException JavaDoc
75    {
76       EnterpriseContext rtn = null;
77       rtn = super.get(id);
78       return rtn;
79    }
80
81    /**
82     * @jmx:managed-operation
83     */

84    public void remove(String JavaDoc id)
85       throws Exception JavaDoc
86    {
87       EntityMetaData metaData = (EntityMetaData) m_container.getBeanMetaData();
88       String JavaDoc primKeyClass = metaData.getPrimaryKeyClass();
89       Object JavaDoc key = PropertyEditors.convertValue(id, primKeyClass);
90       remove(key);
91    }
92
93    public void destroy()
94    {
95       synchronized( this )
96       {
97          this.m_container = null;
98       }
99       super.destroy();
100    }
101
102    protected Object JavaDoc getKey(EnterpriseContext ctx)
103    {
104       return ((EntityEnterpriseContext)ctx).getCacheKey();
105    }
106
107    protected void setKey(Object JavaDoc id, EnterpriseContext ctx)
108    {
109       ((EntityEnterpriseContext)ctx).setCacheKey(id);
110       ctx.setId(id);
111    }
112
113    protected synchronized Container getContainer()
114    {
115       return m_container;
116    }
117
118    protected void passivate(EnterpriseContext ctx) throws RemoteException JavaDoc
119    {
120       m_container.getPersistenceManager().passivateEntity((EntityEnterpriseContext)ctx);
121    }
122
123    protected void activate(EnterpriseContext ctx) throws RemoteException JavaDoc
124    {
125       m_container.getPersistenceManager().activateEntity((EntityEnterpriseContext)ctx);
126    }
127
128    protected EnterpriseContext acquireContext() throws Exception JavaDoc
129    {
130       return m_container.getInstancePool().get();
131    }
132
133    protected void freeContext(EnterpriseContext ctx)
134    {
135       m_container.getInstancePool().free(ctx);
136    }
137
138    protected boolean canPassivate(EnterpriseContext ctx)
139    {
140       if (ctx.isLocked())
141       {
142          // The context is in the interceptor chain
143
return false;
144       }
145
146       if (ctx.getTransaction() != null)
147       {
148          return false;
149       }
150
151       Object JavaDoc key = ((EntityEnterpriseContext)ctx).getCacheKey();
152       return m_container.getLockManager().canPassivate(key);
153    }
154
155    // Package protected ---------------------------------------------
156

157    // Protected -----------------------------------------------------
158

159    protected void unableToPassivateDueToCtxLock(EnterpriseContext ctx, boolean passivateAfterCommit)
160    {
161       EntityEnterpriseContext ectx = (EntityEnterpriseContext)ctx;
162       ectx.setPassivateAfterCommit(passivateAfterCommit);
163       ConfigurationMetaData config = m_container.getBeanMetaData().getContainerConfiguration();
164       if(!config.isStoreNotFlushed() && ectx.hasTxSynchronization())
165       {
166          ectx.setTxAssociation(GlobalTxEntityMap.PREVENT_SYNC);
167       }
168    }
169
170    // Private -------------------------------------------------------
171

172    // Inner classes -------------------------------------------------
173
}
174
Popular Tags