1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
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; |
14 |
| import java.io.BufferedOutputStream; |
15 |
| import java.io.IOException; |
16 |
| import java.io.ObjectInputStream; |
17 |
| import java.io.ObjectOutputStream; |
18 |
| import java.net.Socket; |
19 |
| import java.util.List; |
20 |
| import java.util.Map; |
21 |
| import java.util.Set; |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| public class TcpDelegatingCacheLoader extends DelegatingCacheLoader |
38 |
| { |
39 |
| private Socket sock; |
40 |
| private TcpDelegatingCacheLoaderConfig config; |
41 |
| ObjectInputStream in; |
42 |
| ObjectOutputStream out; |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
64
| public TcpDelegatingCacheLoader()
|
49 |
| { |
50 |
| |
51 |
| } |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
0
| public TcpDelegatingCacheLoader(String host, int port)
|
60 |
| { |
61 |
0
| this.config = new TcpDelegatingCacheLoaderConfig(host, port);
|
62 |
| } |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
64
| public void setConfig(IndividualCacheLoaderConfig base)
|
70 |
| { |
71 |
64
| if (base instanceof TcpDelegatingCacheLoaderConfig)
|
72 |
| { |
73 |
0
| this.config = (TcpDelegatingCacheLoaderConfig) base;
|
74 |
| } |
75 |
| else |
76 |
| { |
77 |
64
| config = new TcpDelegatingCacheLoaderConfig(base);
|
78 |
| } |
79 |
| } |
80 |
| |
81 |
64
| public IndividualCacheLoaderConfig getConfig()
|
82 |
| { |
83 |
64
| return config;
|
84 |
| } |
85 |
| |
86 |
64
| public void start() throws Exception
|
87 |
| { |
88 |
64
| init();
|
89 |
| } |
90 |
| |
91 |
64
| public void stop()
|
92 |
| { |
93 |
64
| try
|
94 |
| { |
95 |
64
| if (in != null) in.close();
|
96 |
| } |
97 |
| catch (IOException e) |
98 |
| { |
99 |
| } |
100 |
64
| try
|
101 |
| { |
102 |
64
| if (out != null) out.close();
|
103 |
| } |
104 |
| catch (IOException e) |
105 |
| { |
106 |
| } |
107 |
64
| try
|
108 |
| { |
109 |
64
| if (sock != null) sock.close();
|
110 |
| } |
111 |
| catch (IOException e) |
112 |
| { |
113 |
| } |
114 |
| } |
115 |
| |
116 |
| |
117 |
64
| private void init() throws IOException
|
118 |
| { |
119 |
64
| sock = new Socket(config.getHost(), config.getPort());
|
120 |
64
| out = new ObjectOutputStream(new BufferedOutputStream(sock.getOutputStream()));
|
121 |
64
| out.flush();
|
122 |
64
| in = new ObjectInputStream(new BufferedInputStream(sock.getInputStream()));
|
123 |
| } |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
2044
| protected Set delegateGetChildrenNames(Fqn fqn) throws Exception
|
129 |
| { |
130 |
2044
| synchronized (out)
|
131 |
| { |
132 |
2044
| out.reset();
|
133 |
2044
| out.writeInt(DelegatingCacheLoader.delegateGetChildrenNames);
|
134 |
2044
| out.writeObject(fqn);
|
135 |
2044
| out.flush();
|
136 |
2044
| Object retval = in.readObject();
|
137 |
2044
| if (retval instanceof Exception)
|
138 |
| { |
139 |
0
| throw (Exception) retval;
|
140 |
| } |
141 |
2044
| return (Set) retval;
|
142 |
| } |
143 |
| } |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
2288
| protected Map delegateGet(Fqn name) throws Exception
|
161 |
| { |
162 |
2288
| synchronized (out)
|
163 |
| { |
164 |
2288
| out.reset();
|
165 |
| |
166 |
2288
| out.writeInt(DelegatingCacheLoader.delegateGet);
|
167 |
2288
| out.writeObject(name);
|
168 |
2288
| out.flush();
|
169 |
2288
| Object retval = in.readObject();
|
170 |
2288
| if (retval instanceof Exception)
|
171 |
| { |
172 |
0
| throw (Exception) retval;
|
173 |
| } |
174 |
2288
| return (Map) retval;
|
175 |
| } |
176 |
| } |
177 |
| |
178 |
| |
179 |
| |
180 |
| |
181 |
71
| protected boolean delegateExists(Fqn name) throws Exception
|
182 |
| { |
183 |
71
| synchronized (out)
|
184 |
| { |
185 |
71
| out.reset();
|
186 |
| |
187 |
71
| out.writeInt(DelegatingCacheLoader.delegateExists);
|
188 |
71
| out.writeObject(name);
|
189 |
71
| out.flush();
|
190 |
71
| Object retval = in.readObject();
|
191 |
71
| if (retval instanceof Exception)
|
192 |
| { |
193 |
0
| throw (Exception) retval;
|
194 |
| } |
195 |
71
| return (Boolean) retval;
|
196 |
| } |
197 |
| } |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
2135
| protected Object delegatePut(Fqn name, Object key, Object value) throws Exception
|
203 |
| { |
204 |
2135
| synchronized (out)
|
205 |
| { |
206 |
2135
| out.reset();
|
207 |
| |
208 |
2135
| out.writeInt(DelegatingCacheLoader.delegatePutKeyVal);
|
209 |
2135
| out.writeObject(name);
|
210 |
2135
| out.writeObject(key);
|
211 |
2135
| out.writeObject(value);
|
212 |
2135
| out.flush();
|
213 |
2135
| Object retval = in.readObject();
|
214 |
2135
| if (retval instanceof Exception)
|
215 |
| { |
216 |
0
| throw (Exception) retval;
|
217 |
| } |
218 |
2135
| return retval;
|
219 |
| } |
220 |
| } |
221 |
| |
222 |
| |
223 |
| |
224 |
| |
225 |
2030
| protected void delegatePut(Fqn name, Map attributes) throws Exception
|
226 |
| { |
227 |
2030
| synchronized (out)
|
228 |
| { |
229 |
2030
| out.reset();
|
230 |
| |
231 |
2030
| out.writeInt(DelegatingCacheLoader.delegatePut);
|
232 |
2030
| out.writeObject(name);
|
233 |
2030
| out.writeObject(attributes);
|
234 |
2030
| out.flush();
|
235 |
2030
| Object retval = in.readObject();
|
236 |
2030
| if (retval instanceof Exception)
|
237 |
| { |
238 |
0
| throw (Exception) retval;
|
239 |
| } |
240 |
| } |
241 |
| } |
242 |
| |
243 |
61
| @Override
|
244 |
| public void put(List<Modification> modifications) throws Exception |
245 |
| { |
246 |
61
| synchronized (out)
|
247 |
| { |
248 |
61
| out.reset();
|
249 |
| |
250 |
61
| out.writeInt(DelegatingCacheLoader.putList);
|
251 |
61
| int length = modifications != null ? modifications.size() : 0;
|
252 |
61
| out.writeInt(length);
|
253 |
61
| if (length > 0)
|
254 |
| { |
255 |
61
| for (Modification m : modifications)
|
256 |
| { |
257 |
70
| m.writeExternal(out);
|
258 |
| } |
259 |
| } |
260 |
61
| out.flush();
|
261 |
61
| Object retval = in.readObject();
|
262 |
61
| if (retval instanceof Exception)
|
263 |
| { |
264 |
0
| throw (Exception) retval;
|
265 |
| } |
266 |
| } |
267 |
| } |
268 |
| |
269 |
| |
270 |
| |
271 |
| |
272 |
2024
| protected Object delegateRemove(Fqn name, Object key) throws Exception
|
273 |
| { |
274 |
2024
| synchronized (out)
|
275 |
| { |
276 |
2024
| out.reset();
|
277 |
| |
278 |
2024
| out.writeInt(DelegatingCacheLoader.delegateRemoveKey);
|
279 |
2024
| out.writeObject(name);
|
280 |
2024
| out.writeObject(key);
|
281 |
2024
| out.flush();
|
282 |
2024
| Object retval = in.readObject();
|
283 |
2024
| if (retval instanceof Exception)
|
284 |
| { |
285 |
0
| throw (Exception) retval;
|
286 |
| } |
287 |
2024
| return retval;
|
288 |
| } |
289 |
| } |
290 |
| |
291 |
| |
292 |
| |
293 |
| |
294 |
2147
| protected void delegateRemove(Fqn name) throws Exception
|
295 |
| { |
296 |
2147
| synchronized (out)
|
297 |
| { |
298 |
2147
| out.reset();
|
299 |
| |
300 |
2147
| out.writeInt(DelegatingCacheLoader.delegateRemove);
|
301 |
2147
| out.writeObject(name);
|
302 |
2147
| out.flush();
|
303 |
2147
| Object retval = in.readObject();
|
304 |
2147
| if (retval instanceof Exception)
|
305 |
| { |
306 |
0
| throw (Exception) retval;
|
307 |
| } |
308 |
| } |
309 |
| } |
310 |
| |
311 |
| |
312 |
| |
313 |
| |
314 |
3
| protected void delegateRemoveData(Fqn name) throws Exception
|
315 |
| { |
316 |
3
| synchronized (out)
|
317 |
| { |
318 |
3
| out.reset();
|
319 |
| |
320 |
3
| out.writeInt(DelegatingCacheLoader.delegateRemoveData);
|
321 |
3
| out.writeObject(name);
|
322 |
3
| out.flush();
|
323 |
3
| Object retval = in.readObject();
|
324 |
3
| if (retval instanceof Exception)
|
325 |
| { |
326 |
0
| throw (Exception) retval;
|
327 |
| } |
328 |
| } |
329 |
| } |
330 |
| |
331 |
3
| @Override
|
332 |
| protected void delegateLoadEntireState(ObjectOutputStream os) throws Exception |
333 |
| { |
334 |
3
| throw new UnsupportedOperationException("operation is not currently supported - need to define semantics first");
|
335 |
| } |
336 |
| |
337 |
0
| @Override
|
338 |
| protected void delegateLoadState(Fqn subtree, ObjectOutputStream os) throws Exception |
339 |
| { |
340 |
0
| throw new UnsupportedOperationException("operation is not currently supported - need to define semantics first");
|
341 |
| } |
342 |
| |
343 |
1
| @Override
|
344 |
| protected void delegateStoreEntireState(ObjectInputStream is) throws Exception |
345 |
| { |
346 |
1
| throw new UnsupportedOperationException("operation is not currently supported - need to define semantics first");
|
347 |
| } |
348 |
| |
349 |
0
| @Override
|
350 |
| protected void delegateStoreState(Fqn subtree, ObjectInputStream is) throws Exception |
351 |
| { |
352 |
0
| throw new UnsupportedOperationException("operation is not currently supported - need to define semantics first");
|
353 |
| } |
354 |
| |
355 |
| } |