KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cluster > ds > DistributedStateUser


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.test.cluster.ds;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import javax.management.Notification JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29
30 import org.jboss.ha.framework.interfaces.DistributedState;
31 import org.jboss.ha.framework.interfaces.DistributedState.DSListenerEx;
32 import org.jboss.ha.framework.interfaces.HAPartition;
33 import org.jboss.logging.Logger;
34 import org.jboss.mx.util.JBossNotificationBroadcasterSupport;
35
36 /** Tests of the DistributedState service
37
38     @author <a HREF="mailto:Scott.Stark@jboss.org">Scott Stark</a>.
39     @version $Revision: 58115 $
40 */

41 public class DistributedStateUser extends JBossNotificationBroadcasterSupport
42    implements IDistributedState, DSListenerEx
43 {
44    protected static Logger log = Logger.getLogger(DistributedStateUser.class);
45
46    protected DistributedState entryMap;
47    protected String JavaDoc category;
48    protected String JavaDoc partitionName;
49    protected long sequence;
50
51    public String JavaDoc getPartitionName()
52    {
53       return partitionName;
54    }
55    public void setPartitionName(String JavaDoc partitionName)
56    {
57       this.partitionName = partitionName;
58    }
59
60    public String JavaDoc getLogger()
61    {
62       return category;
63    }
64    public void setLogger(String JavaDoc category)
65    {
66       this.category = category;
67    }
68
69    public void start() throws Exception JavaDoc
70    {
71       // Lookup the parition
72
InitialContext JavaDoc ctx = new InitialContext JavaDoc();
73       String JavaDoc jndiName = "/HAPartition/" + partitionName;
74       HAPartition partition = (HAPartition) ctx.lookup(jndiName);
75       this.entryMap = partition.getDistributedStateService();
76       log.debug("Obtained DistributedState from partition="+partitionName);
77       entryMap.registerDSListenerEx(category, this);
78    }
79    public void stop()
80    {
81       entryMap.unregisterDSListenerEx(category, this);
82       flush();
83    }
84
85    public Serializable JavaDoc get(Serializable JavaDoc key)
86    {
87       Serializable JavaDoc value = entryMap.get(category, key);
88       log.debug("Get: "+key+", value: "+value);
89       return value;
90    }
91
92    public void put(Serializable JavaDoc key, Serializable JavaDoc value)
93       throws Exception JavaDoc
94    {
95       entryMap.set(category, key, value, false);
96       log.debug("Put: "+key+", value: "+value);
97    }
98
99    public void remove(Serializable JavaDoc key)
100       throws Exception JavaDoc
101    {
102       Object JavaDoc value = entryMap.remove(category, key, false);
103       log.debug("Removed: "+key+", value: "+value);
104    }
105    
106    public Collection JavaDoc listAllCategories()
107    {
108       // return results as a vector to avoid hashmap.keyset serialization error
109
Collection JavaDoc cats = entryMap.getAllCategories();
110       if (cats == null || cats.isEmpty()) {
111          return new java.util.Vector JavaDoc();
112       }
113       java.util.Vector JavaDoc vcats = new java.util.Vector JavaDoc();
114       java.util.Iterator JavaDoc iter = cats.iterator();
115       while (iter.hasNext())
116          vcats.add(iter.next());
117       return vcats;
118    }
119    
120    public Collection JavaDoc listAllKeys(String JavaDoc category)
121    {
122       // return results as a vector to avoid hashmap.keyset serialization error
123
Collection JavaDoc keys = entryMap.getAllKeys(category);
124       if (keys == null || keys.isEmpty()) {
125          return new java.util.Vector JavaDoc();
126       }
127       java.util.Vector JavaDoc vkeys = new java.util.Vector JavaDoc();
128       java.util.Iterator JavaDoc iter = keys.iterator();
129       while (iter.hasNext())
130          vkeys.add(iter.next());
131       return vkeys;
132    }
133    
134    public Collection JavaDoc listAllValues(String JavaDoc category)
135    {
136       // return results as a vector to avoid hashmap.keyset serialization error
137
Collection JavaDoc vals = entryMap.getAllKeys(category);
138       if (vals == null || vals.isEmpty()) {
139          return new java.util.Vector JavaDoc();
140       }
141       java.util.Vector JavaDoc vvals = new java.util.Vector JavaDoc();
142       java.util.Iterator JavaDoc iter = vals.iterator();
143       while (iter.hasNext())
144          vvals.add(iter.next());
145       return vvals;
146    }
147
148    /** Remove all entries from the cache.
149     */

150    public void flush()
151    {
152       Collection JavaDoc keys = entryMap.getAllKeys(category);
153       if(keys == null ) return;
154       // Notify the entries of their removal
155
Iterator JavaDoc iter = keys.iterator();
156       while( iter.hasNext() )
157       {
158          Serializable JavaDoc key = (Serializable JavaDoc) iter.next();
159          try
160          {
161             entryMap.remove(category, key);
162          }
163          catch(Exception JavaDoc e)
164          {
165             log.debug("Failed to remove: "+key, e);
166          }
167       }
168    }
169
170    public int size()
171    {
172       return entryMap.getAllKeys(category).size();
173    }
174
175    public void valueHasChanged(String JavaDoc category, Serializable JavaDoc key,
176       Serializable JavaDoc value, boolean locallyModified)
177    {
178       NotifyData data = new NotifyData();
179       data.category = category;
180       data.key = key;
181       data.value = value;
182       data.locallyModified = locallyModified;
183       String JavaDoc address = System.getProperty("jboss.bind.address");
184       long id = nextSequence();
185       Notification JavaDoc msg = new Notification JavaDoc("valueHasChanged", this, id, address);
186       msg.setUserData(data);
187       log.debug("valueHasChanged, "+msg);
188       super.sendNotification(msg);
189    }
190
191    public void keyHasBeenRemoved(String JavaDoc category, Serializable JavaDoc key,
192       Serializable JavaDoc previousContent, boolean locallyModified)
193    {
194       NotifyData data = new NotifyData();
195       data.category = category;
196       data.key = key;
197       data.value = previousContent;
198       data.locallyModified = locallyModified;
199       String JavaDoc address = System.getProperty("jboss.bind.address");
200       long id = nextSequence();
201       Notification JavaDoc msg = new Notification JavaDoc("keyHasBeenRemoved", this, id, address);
202       msg.setUserData(data);
203       log.debug("keyHasBeenRemoved, "+msg);
204       super.sendNotification(msg);
205    }
206
207    private synchronized long nextSequence()
208    {
209       return sequence ++;
210    }
211 }
212
Popular Tags