KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > loader > SharedStoreCacheLoader


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.cache.loader;
8
9 import org.jgroups.Address;
10 import org.jgroups.View;
11 import org.jboss.cache.TreeCacheListener;
12 import org.jboss.cache.Fqn;
13 import org.jboss.cache.TreeCache;
14 import org.jboss.logging.Logger;
15
16 import java.util.*;
17
18 /**
19  * CacheLoader proxy used only when multiple CacheLoaders in a cluster access the same underlying store (e.g.
20  * a shared filesystem, or DB). SharedStoreCacheLoader is a simply facade to a real CacheLoader implementation. It
21  * always delegates reads to the real CacheLoader. Writes are forwarded only if this SharedStoreCacheLoader is
22  * currently the cordinator. This avoid having all CacheLoaders in a cluster writing the same data to the same
23  * underlying store. Although not incorrect (e.g. a DB will just discard additional INSERTs for the same key, and
24  * throw an exception), this will avoid a lot of redundant work.<br/>
25  * Whenever the current coordinator dies (or leaves), the second in line will take over. That SharedStoreCacheLoader
26  * will then pass writes through to its underlying CacheLoader.
27  * @author Bela Ban
28  * @version $Id: SharedStoreCacheLoader.java,v 1.4.4.3 2005/04/06 21:06:44 starksm Exp $
29  */

30 public class SharedStoreCacheLoader implements TreeCacheListener, CacheLoader {
31    CacheLoader loader=null;
32    Address local_addr=null;
33    boolean active=true; // only active if coordinator
34
Logger log=Logger.getLogger(getClass());
35    TreeCache cache=null;
36
37
38    public SharedStoreCacheLoader(CacheLoader loader, Address local_addr, boolean coordinator) {
39       this.loader=loader;
40       this.local_addr=local_addr;
41       this.active=coordinator;
42    }
43
44
45
46
47    public void nodeCreated(Fqn fqn) {}
48    public void nodeRemoved(Fqn fqn) {}
49    public void nodeLoaded(Fqn fqn) {}
50    public void nodeEvicted(Fqn fqn) {}
51    public void nodeModified(Fqn fqn) {}
52    public void nodeVisited(Fqn fqn) {}
53    public void cacheStarted(TreeCache cache) {}
54    public void cacheStopped(TreeCache cache) {}
55
56    public void viewChange(View new_view) {
57       boolean tmp=active;
58       if(new_view != null && local_addr != null) {
59          Vector mbrs=new_view.getMembers();
60          if(mbrs != null && mbrs.size() > 0 && local_addr.equals(mbrs.firstElement())) {
61             tmp=true;
62          }
63          else {
64             tmp=false;
65          }
66       }
67       if(active != tmp) {
68          active=tmp;
69          log.info("changed mode: active=" + active);
70       }
71    }
72
73
74    public void setConfig(Properties props) {
75       loader.setConfig(props);
76    }
77
78    public void setCache(TreeCache c) {
79       this.cache=c;
80       loader.setCache(c);
81    }
82
83    public Set getChildrenNames(Fqn fqn) throws Exception JavaDoc {
84       return loader.getChildrenNames(fqn);
85    }
86
87    public Object JavaDoc get(Fqn name, Object JavaDoc key) throws Exception JavaDoc {
88       return loader.get(name, key);
89    }
90
91    public Map get(Fqn name) throws Exception JavaDoc {
92       return loader.get(name);
93    }
94
95    public boolean exists(Fqn name) throws Exception JavaDoc {
96       return loader.exists(name);
97    }
98
99    public Object JavaDoc put(Fqn name, Object JavaDoc key, Object JavaDoc value) throws Exception JavaDoc {
100       if(active)
101          return loader.put(name, key, value);
102       else
103          return null;
104    }
105
106    public void put(Fqn name, Map attributes) throws Exception JavaDoc {
107       if(active)
108          loader.put(name, attributes);
109    }
110
111    public void put(List modifications) throws Exception JavaDoc {
112       if(active)
113          loader.put(modifications);
114    }
115
116    public Object JavaDoc remove(Fqn name, Object JavaDoc key) throws Exception JavaDoc {
117       if(active)
118          return loader.remove(name, key);
119       return null;
120    }
121
122    public void remove(Fqn name) throws Exception JavaDoc {
123       if(active)
124          loader.remove(name);
125    }
126
127    public void removeData(Fqn name) throws Exception JavaDoc {
128       if(active)
129          loader.removeData(name);
130    }
131
132    public void prepare(Object JavaDoc tx, List modifications, boolean one_phase) throws Exception JavaDoc {
133       if(active)
134          loader.prepare(tx, modifications, one_phase);
135    }
136
137    public void commit(Object JavaDoc tx) throws Exception JavaDoc {
138       if(active)
139          loader.commit(tx);
140    }
141
142    public void rollback(Object JavaDoc tx) {
143       if(active)
144          loader.rollback(tx);
145    }
146
147    public byte[] loadEntireState() throws Exception JavaDoc {
148       return loader.loadEntireState();
149    }
150
151    public void storeEntireState(byte[] state) throws Exception JavaDoc {
152       if(active)
153          loader.storeEntireState(state);
154    }
155
156    public void create() throws Exception JavaDoc {
157       loader.create();
158    }
159
160    public void start() throws Exception JavaDoc {
161       loader.start();
162    }
163
164    public void stop() {
165       loader.stop();
166    }
167
168    public void destroy() {
169       loader.destroy();
170    }
171
172 }
173
Popular Tags