KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > loader > tcp > 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.tcp;
8
9 import org.jboss.cache.Fqn;
10 import org.jboss.cache.TreeCache;
11 import org.jboss.cache.loader.DelegatingCacheLoader;
12
13 import java.io.IOException JavaDoc;
14 import java.io.ObjectInputStream JavaDoc;
15 import java.io.ObjectOutputStream JavaDoc;
16 import java.net.Socket JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Properties JavaDoc;
19 import java.util.Set JavaDoc;
20
21 /**
22  * DelegatingCacheLoader implementation which delegates to a remote (not in the same VM)
23  * TreeCache using TCP/IP for communication. Example configuration for connecting to a TcpCacheServer
24  * running at myHost:12345:<pre>
25  * <attribute name="CacheLoaderClass">org.jboss.cache.loader.tcp.TcpDelegatingCacheLoader</attribute>
26    <attribute name="CacheLoaderConfig">
27        host=localhost
28        port=2099
29    </attribute>
30  </pre>
31  *
32  * @author Bela Ban
33  * @version $Id: TcpDelegatingCacheLoader.java,v 1.2 2005/04/28 13:44:27 bela Exp $
34  */

35 public class TcpDelegatingCacheLoader extends DelegatingCacheLoader {
36    private Socket JavaDoc sock;
37    private String JavaDoc host;
38    private int port;
39    ObjectInputStream JavaDoc in;
40    ObjectOutputStream JavaDoc out;
41
42
43    /**
44     * Default constructor.
45     */

46    public TcpDelegatingCacheLoader() {
47       // Empty.
48
}
49
50    /**
51     * Allows programmatic configuration.
52     *
53     * @param cache The cache that the cacheloader will acting on behalf of.
54     * @param host The host on which to look up the remote object.
55     * @param port The port on which to look up the remote object.
56     */

57    public TcpDelegatingCacheLoader(String JavaDoc host, int port) {
58       this.host = host;
59       this.port = port;
60    }
61
62    /**
63     * Allows configuration via XML config file.
64     *
65     * @see org.jboss.cache.loader.DelegatingCacheLoader#setConfig(java.util.Properties)
66     */

67    public void setConfig(Properties JavaDoc props) {
68       this.host = props.getProperty("host");
69       if(this.host == null || this.host.length() == 0) {
70          this.host = "localhost";
71       }
72       this.port = Integer.parseInt(props.getProperty("port"));
73    }
74
75    public void start() throws Exception JavaDoc {
76       init();
77    }
78
79    public void stop() {
80       try {if(in != null) in.close();} catch(IOException JavaDoc e) {}
81       try {if(out != null) out.close();} catch(IOException JavaDoc e) {}
82       try {if(sock != null) sock.close();} catch(IOException JavaDoc e) {}
83    }
84
85
86    private void init() throws IOException JavaDoc {
87       if(host == null)
88          host="localhost";
89       sock=new Socket JavaDoc(host, port);
90       out=new ObjectOutputStream JavaDoc(sock.getOutputStream());
91       in=new ObjectInputStream JavaDoc(sock.getInputStream());
92    }
93
94    /**
95     * Allows configuration via XML config file.
96     *
97     * @see org.jboss.cache.loader.DelegatingCacheLoader#setCache(org.jboss.cache.TreeCache)
98     */

99    public void setCache(TreeCache cache) {
100    }
101
102    /**
103     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateGetChildrenNames(org.jboss.cache.Fqn)
104     */

105    protected Set JavaDoc delegateGetChildrenNames(Fqn fqn) throws Exception JavaDoc {
106       out.writeInt(DelegatingCacheLoader.delegateGetChildrenNames);
107       out.writeObject(fqn);
108       return (Set JavaDoc)in.readObject();
109    }
110
111    /**
112     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateGet(org.jboss.cache.Fqn, Object)
113     */

114    protected Object JavaDoc delegateGet(Fqn name, Object JavaDoc key) throws Exception JavaDoc {
115       out.writeInt(DelegatingCacheLoader.delegateGetKey);
116       out.writeObject(name);
117       out.writeObject(key);
118       return in.readObject();
119    }
120
121    /**
122     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateGet(org.jboss.cache.Fqn)
123     */

124    protected Map JavaDoc delegateGet(Fqn name) throws Exception JavaDoc {
125       out.writeInt(DelegatingCacheLoader.delegateGet);
126       out.writeObject(name);
127       return (Map JavaDoc)in.readObject();
128    }
129
130    /**
131     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateExists(org.jboss.cache.Fqn)
132     */

133    protected boolean delegateExists(Fqn name) throws Exception JavaDoc {
134       out.writeInt(DelegatingCacheLoader.delegateExists);
135       out.writeObject(name);
136       return in.readBoolean();
137    }
138
139    /**
140     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegatePut(org.jboss.cache.Fqn, Object, Object)
141     */

142    protected Object JavaDoc delegatePut(Fqn name, Object JavaDoc key, Object JavaDoc value) throws Exception JavaDoc {
143       out.writeInt(DelegatingCacheLoader.delegatePutKeyVal);
144       out.writeObject(name);
145       out.writeObject(key);
146       out.writeObject(value);
147       return in.readObject();
148    }
149
150    /**
151     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegatePut(org.jboss.cache.Fqn, java.util.Map)
152     */

153    protected void delegatePut(Fqn name, Map JavaDoc attributes) throws Exception JavaDoc {
154       out.writeInt(DelegatingCacheLoader.delegatePut);
155       out.writeObject(name);
156       out.writeObject(attributes);
157       out.flush();
158    }
159
160    /**
161     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateRemove(org.jboss.cache.Fqn, Object)
162     */

163    protected Object JavaDoc delegateRemove(Fqn name, Object JavaDoc key) throws Exception JavaDoc {
164       out.writeInt(DelegatingCacheLoader.delegateRemoveKey);
165       out.writeObject(name);
166       out.writeObject(key);
167       return in.readObject();
168    }
169
170    /**
171     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateRemove(org.jboss.cache.Fqn)
172     */

173    protected void delegateRemove(Fqn name) throws Exception JavaDoc {
174       out.writeInt(DelegatingCacheLoader.delegateRemove);
175       out.writeObject(name);
176       out.flush();
177    }
178
179    /**
180     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateRemoveData(org.jboss.cache.Fqn)
181     */

182    protected void delegateRemoveData(Fqn name) throws Exception JavaDoc {
183       out.writeInt(DelegatingCacheLoader.delegateRemoveData);
184       out.writeObject(name);
185       out.flush();
186    }
187
188    /**
189     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateLoadEntireState()
190     */

191    public byte[] delegateLoadEntireState() throws Exception JavaDoc {
192       out.writeInt(DelegatingCacheLoader.delegateLoadEntireState);
193       return (byte[])in.readObject();
194    }
195
196    /**
197     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateStoreEntireState(byte[])
198     */

199    public void delegateStoreEntireState(byte[] state) throws Exception JavaDoc {
200       out.writeInt(DelegatingCacheLoader.delegateStoreEntireState);
201       out.writeObject(state);
202       out.flush();
203    }
204
205
206
207 }
208
Popular Tags