KickJava   Java API By Example, From Geeks To Geeks.

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


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.jboss.cache.Fqn;
10 import org.jboss.cache.Modification;
11 import org.jboss.cache.config.CacheLoaderConfig.IndividualCacheLoaderConfig;
12
13 import java.io.BufferedInputStream JavaDoc;
14 import java.io.BufferedOutputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.ObjectInputStream JavaDoc;
17 import java.io.ObjectOutputStream JavaDoc;
18 import java.net.Socket JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set JavaDoc;
22
23 /**
24  * DelegatingCacheLoader implementation which delegates to a remote (not in the same VM)
25  * CacheImpl using TCP/IP for communication. Example configuration for connecting to a TcpCacheServer
26  * running at myHost:12345:<pre>
27  * <attribute name="CacheLoaderClass">org.jboss.cache.loader.TcpDelegatingCacheLoader</attribute>
28  * <attribute name="CacheLoaderConfig">
29  * host=localhost
30  * port=2099
31  * </attribute>
32  * </pre>
33  *
34  * @author Bela Ban
35  * @version $Id: TcpDelegatingCacheLoader.java,v 1.7 2006/12/30 17:50:01 msurtani Exp $
36  */

37 public class TcpDelegatingCacheLoader extends DelegatingCacheLoader
38 {
39    private Socket JavaDoc sock;
40    private TcpDelegatingCacheLoaderConfig config;
41    ObjectInputStream JavaDoc in;
42    ObjectOutputStream JavaDoc out;
43
44
45    /**
46     * Default constructor.
47     */

48    public TcpDelegatingCacheLoader()
49    {
50       // Empty.
51
}
52
53    /**
54     * Allows programmatic configuration.
55     *
56     * @param host The host on which to look up the remote object.
57     * @param port The port on which to look up the remote object.
58     */

59    public TcpDelegatingCacheLoader(String JavaDoc host, int port)
60    {
61       this.config = new TcpDelegatingCacheLoaderConfig(host, port);
62    }
63
64    /**
65     * Allows configuration via XML config file.
66     *
67     * @see DelegatingCacheLoader#setConfig(org.jboss.cache.config.CacheLoaderConfig.IndividualCacheLoaderConfig)
68     */

69    public void setConfig(IndividualCacheLoaderConfig base)
70    {
71       if (base instanceof TcpDelegatingCacheLoaderConfig)
72       {
73          this.config = (TcpDelegatingCacheLoaderConfig) base;
74       }
75       else
76       {
77          config = new TcpDelegatingCacheLoaderConfig(base);
78       }
79    }
80
81    public IndividualCacheLoaderConfig getConfig()
82    {
83       return config;
84    }
85
86    public void start() throws Exception JavaDoc
87    {
88       init();
89    }
90
91    public void stop()
92    {
93       try
94       {
95          if (in != null) in.close();
96       }
97       catch (IOException JavaDoc e)
98       {
99       }
100       try
101       {
102          if (out != null) out.close();
103       }
104       catch (IOException JavaDoc e)
105       {
106       }
107       try
108       {
109          if (sock != null) sock.close();
110       }
111       catch (IOException JavaDoc e)
112       {
113       }
114    }
115
116
117    private void init() throws IOException JavaDoc
118    {
119       sock = new Socket JavaDoc(config.getHost(), config.getPort());
120       out = new ObjectOutputStream JavaDoc(new BufferedOutputStream JavaDoc(sock.getOutputStream()));
121       out.flush();
122       in = new ObjectInputStream JavaDoc(new BufferedInputStream JavaDoc(sock.getInputStream()));
123    }
124
125    /**
126     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateGetChildrenNames(org.jboss.cache.Fqn)
127     */

128    protected Set JavaDoc delegateGetChildrenNames(Fqn fqn) throws Exception JavaDoc
129    {
130       synchronized (out)
131       {
132          out.reset();
133          out.writeInt(DelegatingCacheLoader.delegateGetChildrenNames);
134          out.writeObject(fqn);
135          out.flush();
136          Object JavaDoc retval = in.readObject();
137          if (retval instanceof Exception JavaDoc)
138          {
139             throw (Exception JavaDoc) retval;
140          }
141          return (Set JavaDoc) retval;
142       }
143    }
144
145    // See http://jira.jboss.com/jira/browse/JBCACHE-118 for why this is commented out.
146

147    /**
148     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateGet(org.jboss.cache.Fqn,Object)
149     */

150 // protected Object delegateGet(Fqn name, Object key) throws Exception {
151
// out.writeInt(DelegatingCacheLoader.delegateGetKey);
152
// out.writeObject(name);
153
// out.writeObject(key);
154
// return in.readObject();
155
// }
156

157    /**
158     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateGet(org.jboss.cache.Fqn)
159     */

160    protected Map JavaDoc delegateGet(Fqn name) throws Exception JavaDoc
161    {
162       synchronized (out)
163       {
164          out.reset();
165
166          out.writeInt(DelegatingCacheLoader.delegateGet);
167          out.writeObject(name);
168          out.flush();
169          Object JavaDoc retval = in.readObject();
170          if (retval instanceof Exception JavaDoc)
171          {
172             throw (Exception JavaDoc) retval;
173          }
174          return (Map JavaDoc) retval;
175       }
176    }
177
178    /**
179     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateExists(org.jboss.cache.Fqn)
180     */

181    protected boolean delegateExists(Fqn name) throws Exception JavaDoc
182    {
183       synchronized (out)
184       {
185          out.reset();
186
187          out.writeInt(DelegatingCacheLoader.delegateExists);
188          out.writeObject(name);
189          out.flush();
190          Object JavaDoc retval = in.readObject();
191          if (retval instanceof Exception JavaDoc)
192          {
193             throw (Exception JavaDoc) retval;
194          }
195          return (Boolean JavaDoc) retval;
196       }
197    }
198
199    /**
200     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegatePut(org.jboss.cache.Fqn,Object,Object)
201     */

202    protected Object JavaDoc delegatePut(Fqn name, Object JavaDoc key, Object JavaDoc value) throws Exception JavaDoc
203    {
204       synchronized (out)
205       {
206          out.reset();
207
208          out.writeInt(DelegatingCacheLoader.delegatePutKeyVal);
209          out.writeObject(name);
210          out.writeObject(key);
211          out.writeObject(value);
212          out.flush();
213          Object JavaDoc retval = in.readObject();
214          if (retval instanceof Exception JavaDoc)
215          {
216             throw (Exception JavaDoc) retval;
217          }
218          return retval;
219       }
220    }
221
222    /**
223     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegatePut(org.jboss.cache.Fqn,java.util.Map)
224     */

225    protected void delegatePut(Fqn name, Map JavaDoc attributes) throws Exception JavaDoc
226    {
227       synchronized (out)
228       {
229          out.reset();
230
231          out.writeInt(DelegatingCacheLoader.delegatePut);
232          out.writeObject(name);
233          out.writeObject(attributes);
234          out.flush();
235          Object JavaDoc retval = in.readObject();
236          if (retval instanceof Exception JavaDoc)
237          {
238             throw (Exception JavaDoc) retval;
239          }
240       }
241    }
242
243    @Override JavaDoc
244    public void put(List JavaDoc<Modification> modifications) throws Exception JavaDoc
245    {
246       synchronized (out)
247       {
248          out.reset();
249
250          out.writeInt(DelegatingCacheLoader.putList);
251          int length = modifications != null ? modifications.size() : 0;
252          out.writeInt(length);
253          if (length > 0)
254          {
255             for (Modification m : modifications)
256             {
257                m.writeExternal(out);
258             }
259          }
260          out.flush();
261          Object JavaDoc retval = in.readObject();
262          if (retval instanceof Exception JavaDoc)
263          {
264             throw (Exception JavaDoc) retval;
265          }
266       }
267    }
268
269    /**
270     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateRemove(org.jboss.cache.Fqn,Object)
271     */

272    protected Object JavaDoc delegateRemove(Fqn name, Object JavaDoc key) throws Exception JavaDoc
273    {
274       synchronized (out)
275       {
276          out.reset();
277
278          out.writeInt(DelegatingCacheLoader.delegateRemoveKey);
279          out.writeObject(name);
280          out.writeObject(key);
281          out.flush();
282          Object JavaDoc retval = in.readObject();
283          if (retval instanceof Exception JavaDoc)
284          {
285             throw (Exception JavaDoc) retval;
286          }
287          return retval;
288       }
289    }
290
291    /**
292     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateRemove(org.jboss.cache.Fqn)
293     */

294    protected void delegateRemove(Fqn name) throws Exception JavaDoc
295    {
296       synchronized (out)
297       {
298          out.reset();
299
300          out.writeInt(DelegatingCacheLoader.delegateRemove);
301          out.writeObject(name);
302          out.flush();
303          Object JavaDoc retval = in.readObject();
304          if (retval instanceof Exception JavaDoc)
305          {
306             throw (Exception JavaDoc) retval;
307          }
308       }
309    }
310
311    /**
312     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateRemoveData(org.jboss.cache.Fqn)
313     */

314    protected void delegateRemoveData(Fqn name) throws Exception JavaDoc
315    {
316       synchronized (out)
317       {
318          out.reset();
319
320          out.writeInt(DelegatingCacheLoader.delegateRemoveData);
321          out.writeObject(name);
322          out.flush();
323          Object JavaDoc retval = in.readObject();
324          if (retval instanceof Exception JavaDoc)
325          {
326             throw (Exception JavaDoc) retval;
327          }
328       }
329    }
330
331    @Override JavaDoc
332    protected void delegateLoadEntireState(ObjectOutputStream JavaDoc os) throws Exception JavaDoc
333    {
334       throw new UnsupportedOperationException JavaDoc("operation is not currently supported - need to define semantics first");
335    }
336
337    @Override JavaDoc
338    protected void delegateLoadState(Fqn subtree, ObjectOutputStream JavaDoc os) throws Exception JavaDoc
339    {
340       throw new UnsupportedOperationException JavaDoc("operation is not currently supported - need to define semantics first");
341    }
342
343    @Override JavaDoc
344    protected void delegateStoreEntireState(ObjectInputStream JavaDoc is) throws Exception JavaDoc
345    {
346       throw new UnsupportedOperationException JavaDoc("operation is not currently supported - need to define semantics first");
347    }
348
349    @Override JavaDoc
350    protected void delegateStoreState(Fqn subtree, ObjectInputStream JavaDoc is) throws Exception JavaDoc
351    {
352       throw new UnsupportedOperationException JavaDoc("operation is not currently supported - need to define semantics first");
353    }
354
355 }
356
Popular Tags