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.deployment.DeploymentException;
025: import org.jboss.ha.framework.interfaces.DistributedState;
026: import org.jboss.metadata.ClusterConfigMetaData;
027: import org.jboss.system.Registry;
028:
029: /**
030: * Cache subclass for entity beans shared accross a cluster with
031: * distributed cache corruption mechanism.
032: *
033: * @author <a href="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>
034: * @version $Revision: 57188 $
035: */
036: public class ClusterSyncEntityInstanceCache extends EntityInstanceCache
037: implements
038: org.jboss.ha.framework.interfaces.DistributedState.DSListenerEx {
039: protected DistributedState ds = null;
040: protected String DS_CATEGORY = null;
041:
042: public void create() throws Exception {
043: super .create();
044:
045: // Get a reference to the DS service
046: ClusterConfigMetaData config = getContainer().getBeanMetaData()
047: .getClusterConfigMetaData();
048: String partitionName = config.getPartitionName();
049: String name = "jboss:service=DistributedState,partitionName="
050: + partitionName;
051: ds = (DistributedState) Registry.lookup(name);
052: if (ds == null)
053: throw new DeploymentException(
054: "Failed to find DistributedState service: " + name);
055: }
056:
057: public void start() throws Exception {
058: super .start();
059:
060: String ejbName = this .getContainer().getBeanMetaData()
061: .getEjbName();
062: this .DS_CATEGORY = "CMPClusteredInMemoryPersistenceManager-"
063: + ejbName;
064:
065: this .ds.registerDSListenerEx(this .DS_CATEGORY, this );
066: }
067:
068: /* From Service interface*/
069: public void stop() {
070: super .stop();
071: this .ds.unregisterDSListenerEx(this .DS_CATEGORY, this );
072: }
073:
074: // DSListener implementation -------------------------------------
075:
076: /**
077: * Called whenever a key has been removed from a category the called object had
078: * subscribed in.
079: * @param category The category under which a key has been removed
080: * @param key The key that has been removed
081: * @param previousContent The previous content of the key that has been removed
082: */
083: public void keyHasBeenRemoved(String category,
084: java.io.Serializable key,
085: java.io.Serializable previousContent,
086: boolean locallyModified) {
087: if (!locallyModified)
088: this .cacheMiss((String) key);
089: }
090:
091: /**
092: * Called whenever a key has been added or modified in the category the called object
093: * has subscribed in.
094: * @param category The category of the modified/added entry
095: * @param key The key that has been added or its value modified
096: * @param value The new value of the key
097: */
098: public void valueHasChanged(String category,
099: java.io.Serializable key, java.io.Serializable value,
100: boolean locallyModified) {
101: if (!locallyModified)
102: this .cacheMiss((String) key);
103: }
104:
105: public void cacheMiss(String key) {
106: // a modification has occured on another node, we clean the cache!
107:
108: try {
109: this .remove(key);
110: } catch (Exception e) {
111: log.warn("failed to remove key", e);
112: }
113: }
114:
115: }
|