1 package org.apache.commons.jcs.auxiliary.remote.http.server;
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.Map;
25 import java.util.Set;
26
27 import org.apache.commons.jcs.engine.behavior.ICacheElement;
28 import org.apache.commons.jcs.engine.behavior.ICacheServiceNonLocal;
29 import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager;
30 import org.apache.commons.jcs.engine.control.CompositeCache;
31 import org.apache.commons.jcs.engine.logging.CacheEvent;
32 import org.apache.commons.jcs.engine.logging.behavior.ICacheEvent;
33 import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37
38
39
40
41 public abstract class AbstractRemoteCacheService<K, V>
42 implements ICacheServiceNonLocal<K, V>
43 {
44
45 private transient ICacheEventLogger cacheEventLogger;
46
47
48 private ICompositeCacheManager cacheManager;
49
50
51 private String eventLogSourceName = "AbstractRemoteCacheService";
52
53
54 private int puts = 0;
55
56
57 private final int logInterval = 100;
58
59
60 private static final Log log = LogFactory.getLog( AbstractRemoteCacheService.class );
61
62
63
64
65
66
67
68 public AbstractRemoteCacheService( ICompositeCacheManager cacheManager, ICacheEventLogger cacheEventLogger )
69 {
70 this.cacheManager = cacheManager;
71 this.cacheEventLogger = cacheEventLogger;
72 }
73
74
75
76
77
78 @Override
79 public void update( ICacheElement<K, V> item )
80 throws IOException
81 {
82 update( item, 0 );
83 }
84
85
86
87
88
89
90
91
92 @Override
93 public void update( ICacheElement<K, V> item, long requesterId )
94 throws IOException
95 {
96 ICacheEvent<ICacheElement<K, V>> cacheEvent = createICacheEvent( item, requesterId, ICacheEventLogger.UPDATE_EVENT );
97 try
98 {
99 logUpdateInfo( item );
100
101 processUpdate( item, requesterId );
102 }
103 finally
104 {
105 logICacheEvent( cacheEvent );
106 }
107 }
108
109
110
111
112
113
114
115
116 abstract void processUpdate( ICacheElement<K, V> item, long requesterId )
117 throws IOException;
118
119
120
121
122
123
124 private void logUpdateInfo( ICacheElement<K, V> item )
125 {
126 if ( log.isInfoEnabled() )
127 {
128
129 puts++;
130 if ( puts % logInterval == 0 )
131 {
132 log.info( "puts = " + puts );
133 }
134 }
135
136 if ( log.isDebugEnabled() )
137 {
138 log.debug( "In update, put [" + item.getKey() + "] in [" + item.getCacheName() + "]" );
139 }
140 }
141
142
143
144
145
146
147
148
149
150
151 @Override
152 public ICacheElement<K, V> get( String cacheName, K key )
153 throws IOException
154 {
155 return this.get( cacheName, key, 0 );
156 }
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171 @Override
172 public ICacheElement<K, V> get( String cacheName, K key, long requesterId )
173 throws IOException
174 {
175 ICacheElement<K, V> element = null;
176 ICacheEvent<K> cacheEvent = createICacheEvent( cacheName, key, requesterId, ICacheEventLogger.GET_EVENT );
177 try
178 {
179 element = processGet( cacheName, key, requesterId );
180 }
181 finally
182 {
183 logICacheEvent( cacheEvent );
184 }
185 return element;
186 }
187
188
189
190
191
192
193
194
195
196
197
198
199 abstract ICacheElement<K, V> processGet( String cacheName, K key, long requesterId )
200 throws IOException;
201
202
203
204
205
206
207
208
209
210 @Override
211 public Map<K, ICacheElement<K, V>> getMatching( String cacheName, String pattern )
212 throws IOException
213 {
214 return getMatching( cacheName, pattern, 0 );
215 }
216
217
218
219
220
221
222
223
224
225
226 @Override
227 public Map<K, ICacheElement<K, V>> getMatching( String cacheName, String pattern, long requesterId )
228 throws IOException
229 {
230 ICacheEvent<String> cacheEvent = createICacheEvent( cacheName, pattern, requesterId,
231 ICacheEventLogger.GETMATCHING_EVENT );
232 try
233 {
234 return processGetMatching( cacheName, pattern, requesterId );
235 }
236 finally
237 {
238 logICacheEvent( cacheEvent );
239 }
240 }
241
242
243
244
245
246
247
248
249
250
251 abstract Map<K, ICacheElement<K, V>> processGetMatching( String cacheName, String pattern, long requesterId )
252 throws IOException;
253
254
255
256
257
258
259
260
261
262
263 @Override
264 public Map<K, ICacheElement<K, V>> getMultiple( String cacheName, Set<K> keys )
265 throws IOException
266 {
267 return this.getMultiple( cacheName, keys, 0 );
268 }
269
270
271
272
273
274
275
276
277
278
279
280
281
282 @Override
283 public Map<K, ICacheElement<K, V>> getMultiple( String cacheName, Set<K> keys, long requesterId )
284 throws IOException
285 {
286 ICacheEvent<Serializable> cacheEvent = createICacheEvent( cacheName, (Serializable) keys, requesterId,
287 ICacheEventLogger.GETMULTIPLE_EVENT );
288 try
289 {
290 return processGetMultiple( cacheName, keys, requesterId );
291 }
292 finally
293 {
294 logICacheEvent( cacheEvent );
295 }
296 }
297
298
299
300
301
302
303
304
305
306
307
308 abstract Map<K, ICacheElement<K, V>> processGetMultiple( String cacheName, Set<K> keys, long requesterId )
309 throws IOException;
310
311
312
313
314
315
316 @Override
317 public Set<K> getKeySet( String cacheName )
318 {
319 return processGetKeySet( cacheName );
320 }
321
322
323
324
325
326
327
328 public Set<K> processGetKeySet( String cacheName )
329 {
330 CompositeCache<K, V> cache = getCacheManager().getCache( cacheName );
331
332 return cache.getKeySet();
333 }
334
335
336
337
338
339
340
341
342 @Override
343 public void remove( String cacheName, K key )
344 throws IOException
345 {
346 remove( cacheName, key, 0 );
347 }
348
349
350
351
352
353
354
355
356
357
358
359 @Override
360 public void remove( String cacheName, K key, long requesterId )
361 throws IOException
362 {
363 ICacheEvent<K> cacheEvent = createICacheEvent( cacheName, key, requesterId, ICacheEventLogger.REMOVE_EVENT );
364 try
365 {
366 processRemove( cacheName, key, requesterId );
367 }
368 finally
369 {
370 logICacheEvent( cacheEvent );
371 }
372 }
373
374
375
376
377
378
379
380
381
382 abstract void processRemove( String cacheName, K key, long requesterId )
383 throws IOException;
384
385
386
387
388
389
390
391 @Override
392 public void removeAll( String cacheName )
393 throws IOException
394 {
395 removeAll( cacheName, 0 );
396 }
397
398
399
400
401
402
403
404
405
406
407 @Override
408 public void removeAll( String cacheName, long requesterId )
409 throws IOException
410 {
411 ICacheEvent<String> cacheEvent = createICacheEvent( cacheName, "all", requesterId, ICacheEventLogger.REMOVEALL_EVENT );
412 try
413 {
414 processRemoveAll( cacheName, requesterId );
415 }
416 finally
417 {
418 logICacheEvent( cacheEvent );
419 }
420 }
421
422
423
424
425
426
427
428
429 abstract void processRemoveAll( String cacheName, long requesterId )
430 throws IOException;
431
432
433
434
435
436
437
438 @Override
439 public void dispose( String cacheName )
440 throws IOException
441 {
442 dispose( cacheName, 0 );
443 }
444
445
446
447
448
449
450
451
452 public void dispose( String cacheName, long requesterId )
453 throws IOException
454 {
455 ICacheEvent<String> cacheEvent = createICacheEvent( cacheName, "none", requesterId, ICacheEventLogger.DISPOSE_EVENT );
456 try
457 {
458 processDispose( cacheName, requesterId );
459 }
460 finally
461 {
462 logICacheEvent( cacheEvent );
463 }
464 }
465
466
467
468
469
470
471 abstract void processDispose( String cacheName, long requesterId )
472 throws IOException;
473
474
475
476
477
478
479
480 public String getStats()
481 throws IOException
482 {
483 return cacheManager.getStats();
484 }
485
486
487
488
489
490
491
492
493
494 protected ICacheEvent<ICacheElement<K, V>> createICacheEvent( ICacheElement<K, V> item, long requesterId, String eventName )
495 {
496 if ( cacheEventLogger == null )
497 {
498 return new CacheEvent<ICacheElement<K, V>>();
499 }
500 String ipAddress = getExtraInfoForRequesterId( requesterId );
501 return cacheEventLogger.createICacheEvent( getEventLogSourceName(), item.getCacheName(), eventName, ipAddress,
502 item );
503 }
504
505
506
507
508
509
510
511
512
513
514 protected <T> ICacheEvent<T> createICacheEvent( String cacheName, T key, long requesterId, String eventName )
515 {
516 if ( cacheEventLogger == null )
517 {
518 return new CacheEvent<T>();
519 }
520 String ipAddress = getExtraInfoForRequesterId( requesterId );
521 return cacheEventLogger.createICacheEvent( getEventLogSourceName(), cacheName, eventName, ipAddress, key );
522 }
523
524
525
526
527
528
529
530
531 protected void logApplicationEvent( String source, String eventName, String optionalDetails )
532 {
533 if ( cacheEventLogger != null )
534 {
535 cacheEventLogger.logApplicationEvent( source, eventName, optionalDetails );
536 }
537 }
538
539
540
541
542
543
544 protected <T> void logICacheEvent( ICacheEvent<T> cacheEvent )
545 {
546 if ( cacheEventLogger != null )
547 {
548 cacheEventLogger.logICacheEvent( cacheEvent );
549 }
550 }
551
552
553
554
555
556
557
558
559
560 protected abstract String getExtraInfoForRequesterId( long requesterId );
561
562
563
564
565
566
567 public void setCacheEventLogger( ICacheEventLogger cacheEventLogger )
568 {
569 this.cacheEventLogger = cacheEventLogger;
570 }
571
572
573
574
575 protected void setCacheManager( ICompositeCacheManager cacheManager )
576 {
577 this.cacheManager = cacheManager;
578 }
579
580
581
582
583 protected ICompositeCacheManager getCacheManager()
584 {
585 return cacheManager;
586 }
587
588
589
590
591 protected void setEventLogSourceName( String eventLogSourceName )
592 {
593 this.eventLogSourceName = eventLogSourceName;
594 }
595
596
597
598
599 protected String getEventLogSourceName()
600 {
601 return eventLogSourceName;
602 }
603 }