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