001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins;
023:
024: import java.rmi.RemoteException;
025: import java.rmi.NoSuchObjectException;
026:
027: import org.jboss.ejb.Container;
028: import org.jboss.ejb.EntityContainer;
029: import org.jboss.ejb.EnterpriseContext;
030: import org.jboss.ejb.EntityEnterpriseContext;
031: import org.jboss.ejb.GlobalTxEntityMap;
032: import org.jboss.metadata.EntityMetaData;
033: import org.jboss.metadata.ConfigurationMetaData;
034: import org.jboss.util.propertyeditor.PropertyEditors;
035:
036: /**
037: * Cache subclass for entity beans.
038: *
039: * @author <a href="mailto:simone.bordet@compaq.com">Simone Bordet</a>
040: * @author <a href="bill@burkecentral.com">Bill Burke</a>
041: * @version $Revision: 57209 $
042: * @jmx:mbean extends="org.jboss.ejb.plugins.AbstractInstanceCacheMBean"
043: */
044: public class EntityInstanceCache extends AbstractInstanceCache
045: implements org.jboss.ejb.EntityCache, EntityInstanceCacheMBean {
046: // Constants -----------------------------------------------------
047:
048: // Attributes ----------------------------------------------------
049: /* The container */
050: private EntityContainer m_container;
051:
052: // Static --------------------------------------------------------
053:
054: // Constructors --------------------------------------------------
055:
056: // Public --------------------------------------------------------
057:
058: /* From ContainerPlugin interface */
059: public void setContainer(Container c) {
060: m_container = (EntityContainer) c;
061: }
062:
063: // Z implementation ----------------------------------------------
064: public Object createCacheKey(Object id) {
065: return id;
066: }
067:
068: // Y overrides ---------------------------------------------------
069: public EnterpriseContext get(Object id) throws RemoteException,
070: NoSuchObjectException {
071: EnterpriseContext rtn = null;
072: rtn = super .get(id);
073: return rtn;
074: }
075:
076: /**
077: * @jmx:managed-operation
078: */
079: public void remove(String id) throws Exception {
080: EntityMetaData metaData = (EntityMetaData) m_container
081: .getBeanMetaData();
082: String primKeyClass = metaData.getPrimaryKeyClass();
083: Object key = PropertyEditors.convertValue(id, primKeyClass);
084: remove(key);
085: }
086:
087: public void destroy() {
088: synchronized (this ) {
089: this .m_container = null;
090: }
091: super .destroy();
092: }
093:
094: protected Object getKey(EnterpriseContext ctx) {
095: return ((EntityEnterpriseContext) ctx).getCacheKey();
096: }
097:
098: protected void setKey(Object id, EnterpriseContext ctx) {
099: ((EntityEnterpriseContext) ctx).setCacheKey(id);
100: ctx.setId(id);
101: }
102:
103: protected synchronized Container getContainer() {
104: return m_container;
105: }
106:
107: protected void passivate(EnterpriseContext ctx)
108: throws RemoteException {
109: m_container.getPersistenceManager().passivateEntity(
110: (EntityEnterpriseContext) ctx);
111: }
112:
113: protected void activate(EnterpriseContext ctx)
114: throws RemoteException {
115: m_container.getPersistenceManager().activateEntity(
116: (EntityEnterpriseContext) ctx);
117: }
118:
119: protected EnterpriseContext acquireContext() throws Exception {
120: return m_container.getInstancePool().get();
121: }
122:
123: protected void freeContext(EnterpriseContext ctx) {
124: m_container.getInstancePool().free(ctx);
125: }
126:
127: protected boolean canPassivate(EnterpriseContext ctx) {
128: if (ctx.isLocked()) {
129: // The context is in the interceptor chain
130: return false;
131: }
132:
133: if (ctx.getTransaction() != null) {
134: return false;
135: }
136:
137: Object key = ((EntityEnterpriseContext) ctx).getCacheKey();
138: return m_container.getLockManager().canPassivate(key);
139: }
140:
141: // Package protected ---------------------------------------------
142:
143: // Protected -----------------------------------------------------
144:
145: protected void unableToPassivateDueToCtxLock(EnterpriseContext ctx,
146: boolean passivateAfterCommit) {
147: EntityEnterpriseContext ectx = (EntityEnterpriseContext) ctx;
148: ectx.setPassivateAfterCommit(passivateAfterCommit);
149: ConfigurationMetaData config = m_container.getBeanMetaData()
150: .getContainerConfiguration();
151: if (!config.isStoreNotFlushed() && ectx.hasTxSynchronization()) {
152: ectx.setTxAssociation(GlobalTxEntityMap.PREVENT_SYNC);
153: }
154: }
155:
156: // Private -------------------------------------------------------
157:
158: // Inner classes -------------------------------------------------
159: }
|