KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JBoss, Home of Professional Open Source
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.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.jboss.cache.Fqn;
12 import org.jboss.cache.Modification;
13 import org.jboss.cache.RegionManager;
14 import org.jboss.cache.config.CacheLoaderConfig.IndividualCacheLoaderConfig;
15 import org.jboss.cache.marshall.MethodCall;
16 import org.jboss.cache.marshall.MethodCallFactory;
17 import org.jboss.cache.marshall.MethodDeclarations;
18 import org.jgroups.Address;
19 import org.jgroups.blocks.GroupRequest;
20
21 import java.io.ObjectInputStream JavaDoc;
22 import java.io.ObjectOutputStream JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27
28 /**
29  * A cache loader that consults other members in the cluster for values. Does
30  * not propagate update methods since replication should take care of this. A
31  * <code>timeout</code> property is required, a <code>long</code> that
32  * specifies in milliseconds how long to wait for results before returning a
33  * null.
34  *
35  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
36  */

37 public class ClusteredCacheLoader extends AbstractCacheLoader
38 {
39    private static Log log = LogFactory.getLog(ClusteredCacheLoader.class);
40
41    private ClusteredCacheLoaderConfig config;
42
43    /**
44     * Sets the configuration.
45     * A property <code>timeout</code> is used as the timeout value.
46     */

47    public void setConfig(IndividualCacheLoaderConfig base)
48    {
49       if (base instanceof ClusteredCacheLoaderConfig)
50       {
51          this.config = (ClusteredCacheLoaderConfig) base;
52       }
53       else
54       {
55          config = new ClusteredCacheLoaderConfig(base);
56       }
57    }
58
59    public IndividualCacheLoaderConfig getConfig()
60    {
61       return config;
62    }
63
64    public Set JavaDoc<String JavaDoc> getChildrenNames(Fqn fqn) throws Exception JavaDoc
65    {
66       MethodCall call = MethodCallFactory.create(MethodDeclarations.getChildrenNamesMethodLocal, fqn);
67       Object JavaDoc resp = callRemote(call);
68       return (Set JavaDoc<String JavaDoc>) resp;
69    }
70
71    private Object JavaDoc callRemote(MethodCall call) throws Exception JavaDoc
72    {
73       if (log.isTraceEnabled()) log.trace("cache=" + cache.getLocalAddress() + "; calling with " + call);
74       List JavaDoc<Address> mbrs = cache.getMembers();
75       MethodCall clusteredGet = MethodCallFactory.create(MethodDeclarations.clusteredGetMethod, call, false);
76       List JavaDoc resps = cache.getRPCManager().callRemoteMethods(mbrs, clusteredGet, GroupRequest.GET_FIRST, true, config.getTimeout());
77       if (resps == null)
78       {
79          if (log.isInfoEnabled()) log.info("No replies to call " + call + ". Perhaps we're alone in the cluster?");
80          return null;
81       }
82       else
83       {
84          // test for and remove exceptions
85
Iterator JavaDoc i = resps.iterator();
86          Object JavaDoc result = null;
87          while (i.hasNext())
88          {
89             Object JavaDoc o = i.next();
90             if (o instanceof Exception JavaDoc)
91             {
92                if (log.isDebugEnabled())
93                   log.debug("Found remote exception among responses - removing from responses list", (Exception JavaDoc) o);
94             }
95             else
96             {
97                // keep looping till we find a FOUND answer.
98
List JavaDoc<Boolean JavaDoc> clusteredGetResp = (List JavaDoc<Boolean JavaDoc>) o;
99                // found?
100
if (clusteredGetResp.get(0))
101                {
102                   result = clusteredGetResp.get(1);
103                   break;
104                }
105             }
106          }
107
108          if (log.isTraceEnabled()) log.trace("got responses " + resps);
109          return result;
110       }
111    }
112
113    public Map JavaDoc get(Fqn name) throws Exception JavaDoc
114    {
115       return get0(name);
116    }
117
118    protected Map JavaDoc get0(Fqn name) throws Exception JavaDoc
119    {
120       MethodCall call = MethodCallFactory.create(MethodDeclarations.getDataMapMethodLocal, name);
121       Object JavaDoc resp = callRemote(call);
122       Map JavaDoc m = (Map JavaDoc) resp;
123       return m;
124    }
125
126    public boolean exists(Fqn name) throws Exception JavaDoc
127    {
128       MethodCall call = MethodCallFactory.create(MethodDeclarations.existsMethod, name);
129       Object JavaDoc resp = callRemote(call);
130
131       return resp != null && (Boolean JavaDoc) resp;
132    }
133
134    public Object JavaDoc put(Fqn name, Object JavaDoc key, Object JavaDoc value) throws Exception JavaDoc
135    {
136       if (cache.getInvocationContext().isOriginLocal())
137       {
138          Object JavaDoc o[] = {name, key, true};
139          MethodCall call = MethodCallFactory.create(MethodDeclarations.getKeyValueMethodLocal, o);
140          return callRemote(call);
141       }
142       else
143       {
144          log.trace("Call originated remotely. Not bothering to try and do a clustered get() for this put(). Returning null.");
145          return null;
146       }
147    }
148
149    /**
150     * Does nothing; replication handles put.
151     */

152    public void put(Fqn name, Map JavaDoc attributes) throws Exception JavaDoc
153    {
154    }
155
156    /**
157     * Does nothing; replication handles put.
158     */

159    public void put(List JavaDoc<Modification> modifications) throws Exception JavaDoc
160    {
161    }
162
163    /**
164     * Fetches the remove value, does not remove. Replication handles
165     * removal.
166     */

167    public Object JavaDoc remove(Fqn name, Object JavaDoc key) throws Exception JavaDoc
168    {
169       Map JavaDoc map = get(name);
170       return map == null ? null : map.get(key);
171    }
172
173    /**
174     * Does nothing; replication handles removal.
175     */

176    public void remove(Fqn name) throws Exception JavaDoc
177    {
178       // do nothing
179
}
180
181    /**
182     * Does nothing; replication handles removal.
183     */

184    public void removeData(Fqn name) throws Exception JavaDoc
185    {
186    }
187
188    /**
189     * Does nothing.
190     */

191    public void prepare(Object JavaDoc tx, List JavaDoc modifications, boolean one_phase) throws Exception JavaDoc
192    {
193    }
194
195    /**
196     * Does nothing.
197     */

198    public void commit(Object JavaDoc tx) throws Exception JavaDoc
199    {
200    }
201
202    /**
203     * Does nothing.
204     */

205    public void rollback(Object JavaDoc tx)
206    {
207    }
208
209    public void loadEntireState(ObjectOutputStream JavaDoc os) throws Exception JavaDoc
210    {
211       //intentional no-op
212
}
213
214    public void loadState(Fqn subtree, ObjectOutputStream JavaDoc os) throws Exception JavaDoc
215    {
216       // intentional no-op
217
}
218
219    public void storeEntireState(ObjectInputStream JavaDoc is) throws Exception JavaDoc
220    {
221       // intentional no-op
222
}
223
224    public void storeState(Fqn subtree, ObjectInputStream JavaDoc is) throws Exception JavaDoc
225    {
226       // intentional no-op
227
}
228
229    public void setRegionManager(RegionManager manager)
230    {
231    }
232
233    public void create() throws Exception JavaDoc
234    {
235    }
236
237    public void start() throws Exception JavaDoc
238    {
239    }
240
241    public void stop()
242    {
243    }
244
245    public void destroy()
246    {
247    }
248
249 }
250
Popular Tags