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 org.jboss.cache.invalidation.InvalidationManagerMBean;
025: import org.jboss.cache.invalidation.InvalidationGroup;
026: import org.jboss.metadata.ConfigurationMetaData;
027: import org.jboss.metadata.EntityMetaData;
028: import org.jboss.system.Registry;
029: import org.jboss.ejb.EnterpriseContext;
030:
031: /**
032: * Cache implementation that registers with an InvalidationManager when in
033: * commit option A or D. Information is found in the EB meta-data (IM name,
034: * IG name and commit-option)
035: *
036: * @see org.jboss.cache.invalidation.InvalidationManagerMBean
037: * @see org.jboss.cache.invalidation.triggers.EntityBeanCacheBatchInvalidatorInterceptor
038: *
039: * @author <a href="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
040: * @version $Revision: 57209 $
041: */
042: public class InvalidableEntityInstanceCache extends
043: org.jboss.ejb.plugins.EntityInstanceCache implements
044: org.jboss.cache.invalidation.Invalidatable {
045:
046: // Constants -----------------------------------------------------
047:
048: // Attributes ----------------------------------------------------
049:
050: protected InvalidationManagerMBean invalMgr = null;
051: protected InvalidationGroup ig = null;
052:
053: protected boolean isTraceEnabled = false;
054:
055: // Static --------------------------------------------------------
056:
057: // Constructors --------------------------------------------------
058:
059: public InvalidableEntityInstanceCache() {
060: super ();
061: }
062:
063: // Public --------------------------------------------------------
064:
065: // Invalidatable implementation ----------------------------------------------
066:
067: public void areInvalid(java.io.Serializable[] keys) {
068: if (this .isTraceEnabled)
069: log.trace("Invalidating entry in cache. Quantity: "
070: + keys.length);
071:
072: for (int i = 0; i < keys.length; i++) {
073: try {
074: doInvalidate(keys[i]);
075: } catch (Exception ignored) {
076: log.debug(ignored);
077: }
078: }
079: }
080:
081: public void isInvalid(java.io.Serializable key) {
082: try {
083: doInvalidate(key);
084: } catch (Exception ignored) {
085: log.debug(ignored);
086: }
087: }
088:
089: public void invalidateAll() {
090: flush();
091: }
092:
093: // ServiceMBeanSupport overrides ---------------------------------------------------
094:
095: public void start() throws Exception {
096: super .start();
097:
098: log.debug("Starting InvalidableEntityInstanceCache...");
099:
100: EntityMetaData emd = ((EntityMetaData) this .getContainer()
101: .getBeanMetaData());
102:
103: boolean participateInDistInvalidations = emd
104: .doDistributedCacheInvalidations();
105: byte co = emd.getContainerConfiguration().getCommitOption();
106:
107: if (participateInDistInvalidations
108: && (co == ConfigurationMetaData.A_COMMIT_OPTION || co == ConfigurationMetaData.D_COMMIT_OPTION)) {
109: // we are interested in receiving cache invalidation callbacks
110: //
111: String groupName = emd
112: .getDistributedCacheInvalidationConfig()
113: .getInvalidationGroupName();
114: String imName = emd.getDistributedCacheInvalidationConfig()
115: .getInvalidationManagerName();
116:
117: this .invalMgr = (InvalidationManagerMBean) Registry
118: .lookup(imName);
119: this .ig = this .invalMgr.getInvalidationGroup(groupName);
120: this .ig.register(this );
121: this .isTraceEnabled = log.isTraceEnabled();
122: }
123:
124: }
125:
126: public void stop() {
127: try {
128: this .ig.unregister(this );
129: this .ig = null;
130: this .invalMgr = null;
131: } catch (Exception e) {
132: log.debug(e);
133: }
134:
135: super .stop();
136: }
137:
138: // Package protected ---------------------------------------------
139:
140: // Protected -----------------------------------------------------
141:
142: protected void doInvalidate(java.io.Serializable key) {
143: if (key != null) {
144: synchronized (getCacheLock()) {
145: EnterpriseContext ctx = (EnterpriseContext) getCache()
146: .peek(key);
147: if (ctx != null) {
148: release(ctx);
149: }
150: }
151: }
152: }
153:
154: // Private -------------------------------------------------------
155:
156: // Inner classes -------------------------------------------------
157:
158: }
|