KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jboss.deployment.DeploymentException;
25 import org.jboss.ha.framework.interfaces.DistributedState;
26 import org.jboss.metadata.ClusterConfigMetaData;
27 import org.jboss.system.Registry;
28
29 /**
30  * Cache subclass for entity beans shared accross a cluster with
31  * distributed cache corruption mechanism.
32  *
33  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>
34  * @version $Revision: 37459 $
35  */

36 public class ClusterSyncEntityInstanceCache
37    extends EntityInstanceCache
38    implements org.jboss.ha.framework.interfaces.DistributedState.DSListenerEx
39 {
40    protected DistributedState ds = null;
41    protected String JavaDoc DS_CATEGORY = null;
42
43    public void create() throws Exception JavaDoc
44    {
45       super.create ();
46
47       // Get a reference to the DS service
48
ClusterConfigMetaData config = getContainer().getBeanMetaData().getClusterConfigMetaData();
49       String JavaDoc partitionName = config.getPartitionName();
50       String JavaDoc name = "jboss:service=DistributedState,partitionName=" + partitionName;
51       ds = (DistributedState) Registry.lookup (name);
52       if( ds == null )
53          throw new DeploymentException("Failed to find DistributedState service: "+name);
54    }
55
56    public void start() throws Exception JavaDoc
57    {
58       super.start ();
59
60       String JavaDoc ejbName = this.getContainer ().getBeanMetaData ().getEjbName ();
61       this.DS_CATEGORY = "CMPClusteredInMemoryPersistenceManager-" + ejbName;
62
63       this.ds.registerDSListenerEx (this.DS_CATEGORY, this);
64    }
65
66    /* From Service interface*/
67    public void stop()
68    {
69       super.stop ();
70       this.ds.unregisterDSListenerEx (this.DS_CATEGORY, this);
71    }
72
73    // DSListener implementation -------------------------------------
74

75    /**
76     * Called whenever a key has been removed from a category the called object had
77     * subscribed in.
78     * @param category The category under which a key has been removed
79     * @param key The key that has been removed
80     * @param previousContent The previous content of the key that has been removed
81     */

82    public void keyHasBeenRemoved (String JavaDoc category, java.io.Serializable JavaDoc key, java.io.Serializable JavaDoc previousContent, boolean locallyModified)
83    {
84       if (!locallyModified)
85          this.cacheMiss ((String JavaDoc)key);
86    }
87
88    /**
89     * Called whenever a key has been added or modified in the category the called object
90     * has subscribed in.
91     * @param category The category of the modified/added entry
92     * @param key The key that has been added or its value modified
93     * @param value The new value of the key
94     */

95    public void valueHasChanged (String JavaDoc category, java.io.Serializable JavaDoc key, java.io.Serializable JavaDoc value, boolean locallyModified)
96    {
97       if (!locallyModified)
98          this.cacheMiss ((String JavaDoc)key);
99    }
100
101    public void cacheMiss(String JavaDoc key)
102    {
103       // a modification has occured on another node, we clean the cache!
104

105       try
106       {
107          this.remove(key);
108       }
109       catch (Exception JavaDoc e)
110       {
111          log.warn("failed to remove key" ,e);
112       }
113    }
114
115 }
116
117
Popular Tags