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