1 package org.apache.commons.jcs.auxiliary.remote.http.client;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheDispatcher;
23 import org.apache.commons.jcs.auxiliary.remote.http.client.behavior.IRemoteHttpCacheClient;
24 import org.apache.commons.jcs.auxiliary.remote.util.RemoteCacheRequestFactory;
25 import org.apache.commons.jcs.auxiliary.remote.value.RemoteCacheRequest;
26 import org.apache.commons.jcs.auxiliary.remote.value.RemoteCacheResponse;
27 import org.apache.commons.jcs.engine.behavior.ICacheElement;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 import java.io.IOException;
32 import java.io.Serializable;
33 import java.util.Collections;
34 import java.util.Map;
35 import java.util.Set;
36
37
38 public class RemoteHttpCacheClient<K, V>
39 implements IRemoteHttpCacheClient<K, V>
40 {
41
42 private static final Log log = LogFactory.getLog( RemoteHttpCacheClient.class );
43
44
45 private IRemoteCacheDispatcher remoteDispatcher;
46
47
48 private RemoteHttpCacheAttributes remoteHttpCacheAttributes;
49
50
51 private boolean initialized = false;
52
53
54 public RemoteHttpCacheClient()
55 {
56
57 }
58
59
60
61
62
63
64 public RemoteHttpCacheClient( RemoteHttpCacheAttributes attributes )
65 {
66 setRemoteHttpCacheAttributes( attributes );
67 initialize( attributes );
68 }
69
70
71
72
73
74
75
76 @Override
77 public void initialize( RemoteHttpCacheAttributes attributes )
78 {
79 setRemoteDispatcher( new RemoteHttpCacheDispatcher( attributes ) );
80
81 if ( log.isInfoEnabled() )
82 {
83 log.info( "Created remote Dispatcher." + getRemoteDispatcher() );
84 }
85 setInitialized( true );
86 }
87
88
89
90
91
92
93
94
95
96 @Override
97 public ICacheElement<K, V> get( String cacheName, K key )
98 throws IOException
99 {
100 return get( cacheName, key, 0 );
101 }
102
103
104
105
106
107
108
109
110
111
112 @Override
113 public ICacheElement<K, V> get( String cacheName, K key, long requesterId )
114 throws IOException
115 {
116 if ( !isInitialized() )
117 {
118 String message = "The Remote Http Client is not initialized. Cannot process request.";
119 log.warn( message );
120 throw new IOException( message );
121 }
122 RemoteCacheRequest<K, Serializable> remoteHttpCacheRequest =
123 RemoteCacheRequestFactory.createGetRequest( cacheName, key, requesterId );
124
125 RemoteCacheResponse<ICacheElement<K, V>> remoteHttpCacheResponse =
126 getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest );
127
128 if ( log.isDebugEnabled() )
129 {
130 log.debug( "Get [" + key + "] = " + remoteHttpCacheResponse );
131 }
132
133 if ( remoteHttpCacheResponse != null)
134 {
135 return remoteHttpCacheResponse.getPayload();
136 }
137
138 return null;
139 }
140
141
142
143
144
145
146
147
148
149
150 @Override
151 public Map<K, ICacheElement<K, V>> getMatching( String cacheName, String pattern )
152 throws IOException
153 {
154 return getMatching( cacheName, pattern, 0 );
155 }
156
157
158
159
160
161
162
163
164
165
166
167 @Override
168 public Map<K, ICacheElement<K, V>> getMatching( String cacheName, String pattern, long requesterId )
169 throws IOException
170 {
171 if ( !isInitialized() )
172 {
173 String message = "The Remote Http Client is not initialized. Cannot process request.";
174 log.warn( message );
175 throw new IOException( message );
176 }
177
178 RemoteCacheRequest<K, V> remoteHttpCacheRequest =
179 RemoteCacheRequestFactory.createGetMatchingRequest( cacheName, pattern, requesterId );
180
181 RemoteCacheResponse<Map<K, ICacheElement<K, V>>> remoteHttpCacheResponse =
182 getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest );
183
184 if ( log.isDebugEnabled() )
185 {
186 log.debug( "GetMatching [" + pattern + "] = " + remoteHttpCacheResponse );
187 }
188
189 return remoteHttpCacheResponse.getPayload();
190 }
191
192
193
194
195
196
197
198
199
200
201 @Override
202 public Map<K, ICacheElement<K, V>> getMultiple( String cacheName, Set<K> keys )
203 throws IOException
204 {
205 return getMultiple( cacheName, keys, 0 );
206 }
207
208
209
210
211
212
213
214
215
216
217
218 @Override
219 public Map<K, ICacheElement<K, V>> getMultiple( String cacheName, Set<K> keys, long requesterId )
220 throws IOException
221 {
222 if ( !isInitialized() )
223 {
224 String message = "The Remote Http Client is not initialized. Cannot process request.";
225 log.warn( message );
226 throw new IOException( message );
227 }
228
229 RemoteCacheRequest<K, V> remoteHttpCacheRequest =
230 RemoteCacheRequestFactory.createGetMultipleRequest( cacheName, keys, requesterId );
231
232 RemoteCacheResponse<Map<K, ICacheElement<K, V>>> remoteHttpCacheResponse =
233 getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest );
234
235 if ( log.isDebugEnabled() )
236 {
237 log.debug( "GetMultiple [" + keys + "] = " + remoteHttpCacheResponse );
238 }
239
240 return remoteHttpCacheResponse.getPayload();
241 }
242
243
244
245
246
247
248
249
250 @Override
251 public void remove( String cacheName, K key )
252 throws IOException
253 {
254 remove( cacheName, key, 0 );
255 }
256
257
258
259
260
261
262
263
264
265 @Override
266 public void remove( String cacheName, K key, long requesterId )
267 throws IOException
268 {
269 if ( !isInitialized() )
270 {
271 String message = "The Remote Http Client is not initialized. Cannot process request.";
272 log.warn( message );
273 throw new IOException( message );
274 }
275
276 RemoteCacheRequest<K, V> remoteHttpCacheRequest =
277 RemoteCacheRequestFactory.createRemoveRequest( cacheName, key, requesterId );
278
279 getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest );
280 }
281
282
283
284
285
286
287
288 @Override
289 public void removeAll( String cacheName )
290 throws IOException
291 {
292 removeAll( cacheName, 0 );
293 }
294
295
296
297
298
299
300
301
302 @Override
303 public void removeAll( String cacheName, long requesterId )
304 throws IOException
305 {
306 if ( !isInitialized() )
307 {
308 String message = "The Remote Http Client is not initialized. Cannot process request.";
309 log.warn( message );
310 throw new IOException( message );
311 }
312
313 RemoteCacheRequest<K, V> remoteHttpCacheRequest =
314 RemoteCacheRequestFactory.createRemoveAllRequest( cacheName, requesterId );
315
316 getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest );
317 }
318
319
320
321
322
323
324
325 @Override
326 public void update( ICacheElement<K, V> item )
327 throws IOException
328 {
329 update( item, 0 );
330 }
331
332
333
334
335
336
337
338
339 @Override
340 public void update( ICacheElement<K, V> cacheElement, long requesterId )
341 throws IOException
342 {
343 if ( !isInitialized() )
344 {
345 String message = "The Remote Http Client is not initialized. Cannot process request.";
346 log.warn( message );
347 throw new IOException( message );
348 }
349
350 RemoteCacheRequest<K, V> remoteHttpCacheRequest =
351 RemoteCacheRequestFactory.createUpdateRequest( cacheElement, requesterId );
352
353 getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest );
354 }
355
356
357
358
359
360
361
362 @Override
363 public void dispose( String cacheName )
364 throws IOException
365 {
366 if ( !isInitialized() )
367 {
368 String message = "The Remote Http Client is not initialized. Cannot process request.";
369 log.warn( message );
370 throw new IOException( message );
371 }
372
373 RemoteCacheRequest<K, V> remoteHttpCacheRequest =
374 RemoteCacheRequestFactory.createDisposeRequest( cacheName, 0 );
375
376 getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest );
377 }
378
379
380
381
382
383
384 @Override
385 public void release()
386 throws IOException
387 {
388
389 }
390
391
392
393
394
395
396
397 @Override
398 public Set<K> getKeySet( String cacheName ) throws IOException
399 {
400 if ( !isInitialized() )
401 {
402 String message = "The Remote Http Client is not initialized. Cannot process request.";
403 log.warn( message );
404 throw new IOException( message );
405 }
406
407 RemoteCacheRequest<String, String> remoteHttpCacheRequest =
408 RemoteCacheRequestFactory.createGetKeySetRequest(cacheName, 0 );
409
410 RemoteCacheResponse<Set<K>> remoteHttpCacheResponse = getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest );
411
412 if ( remoteHttpCacheResponse != null && remoteHttpCacheResponse.getPayload() != null )
413 {
414 return remoteHttpCacheResponse.getPayload();
415 }
416
417 return Collections.emptySet();
418 }
419
420
421
422
423
424
425
426 @Override
427 public boolean isAlive()
428 throws IOException
429 {
430 if ( !isInitialized() )
431 {
432 String message = "The Remote Http Client is not initialized. Cannot process request.";
433 log.warn( message );
434 throw new IOException( message );
435 }
436
437 RemoteCacheRequest<K, V> remoteHttpCacheRequest = RemoteCacheRequestFactory.createAliveCheckRequest( 0 );
438 RemoteCacheResponse<String> remoteHttpCacheResponse =
439 getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest );
440
441 if ( remoteHttpCacheResponse != null )
442 {
443 return remoteHttpCacheResponse.isSuccess();
444 }
445
446 return false;
447 }
448
449
450
451
452 public void setRemoteDispatcher( IRemoteCacheDispatcher remoteDispatcher )
453 {
454 this.remoteDispatcher = remoteDispatcher;
455 }
456
457
458
459
460 public IRemoteCacheDispatcher getRemoteDispatcher()
461 {
462 return remoteDispatcher;
463 }
464
465
466
467
468 public void setRemoteHttpCacheAttributes( RemoteHttpCacheAttributes remoteHttpCacheAttributes )
469 {
470 this.remoteHttpCacheAttributes = remoteHttpCacheAttributes;
471 }
472
473
474
475
476 public RemoteHttpCacheAttributes getRemoteHttpCacheAttributes()
477 {
478 return remoteHttpCacheAttributes;
479 }
480
481
482
483
484 protected void setInitialized( boolean initialized )
485 {
486 this.initialized = initialized;
487 }
488
489
490
491
492 protected boolean isInitialized()
493 {
494 return initialized;
495 }
496 }