| 1 |
package org.apache.commons.jcs3.engine; |
| 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.util.Collections; |
| 24 |
import java.util.Set; |
| 25 |
import java.util.concurrent.ConcurrentHashMap; |
| 26 |
import java.util.concurrent.ConcurrentMap; |
| 27 |
import java.util.concurrent.CopyOnWriteArraySet; |
| 28 |
|
| 29 |
import org.apache.commons.jcs3.engine.behavior.ICacheListener; |
| 30 |
import org.apache.commons.jcs3.engine.behavior.ICacheObserver; |
| 31 |
import org.apache.commons.jcs3.log.Log; |
| 32 |
import org.apache.commons.jcs3.log.LogManager; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class CacheWatchRepairable |
| 40 |
implements ICacheObserver |
| 41 |
{ |
| 42 |
|
| 43 |
private static final Log log = LogManager.getLog( CacheWatchRepairable.class ); |
| 44 |
|
| 45 |
|
| 46 |
private ICacheObserver cacheWatch; |
| 47 |
|
| 48 |
|
| 49 |
private final ConcurrentMap<String, Set<ICacheListener<?, ?>>> cacheMap = |
| 50 |
new ConcurrentHashMap<>(); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public void setCacheWatch( final ICacheObserver cacheWatch ) |
| 59 |
{ |
| 60 |
this.cacheWatch = cacheWatch; |
| 61 |
cacheMap.forEach((cacheName, value) -> value.forEach(listener -> { |
| 62 |
try |
| 63 |
{ |
| 64 |
log.info( "Adding listener to cache watch. ICacheListener = " |
| 65 |
+ "{0} | ICacheObserver = {1}", listener, cacheWatch ); |
| 66 |
cacheWatch.addCacheListener( cacheName, listener ); |
| 67 |
} |
| 68 |
catch ( final IOException ex ) |
| 69 |
{ |
| 70 |
log.error( "Problem adding listener. ICacheListener = {0} | " |
| 71 |
+ "ICacheObserver = {1}", listener, cacheWatch, ex ); |
| 72 |
} |
| 73 |
})); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
@Override |
| 84 |
public <K, V> void addCacheListener( final String cacheName, final ICacheListener<K, V> obj ) |
| 85 |
throws IOException |
| 86 |
{ |
| 87 |
|
| 88 |
|
| 89 |
cacheMap.computeIfAbsent(cacheName, key -> new CopyOnWriteArraySet<>(Collections.singletonList(obj))); |
| 90 |
|
| 91 |
log.info( "Adding listener to cache watch. ICacheListener = {0} | " |
| 92 |
+ "ICacheObserver = {1} | cacheName = {2}", obj, cacheWatch, |
| 93 |
cacheName ); |
| 94 |
cacheWatch.addCacheListener( cacheName, obj ); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
@Override |
| 104 |
public <K, V> void addCacheListener( final ICacheListener<K, V> obj ) |
| 105 |
throws IOException |
| 106 |
{ |
| 107 |
|
| 108 |
|
| 109 |
cacheMap.values().forEach(set -> set.add(obj)); |
| 110 |
|
| 111 |
log.info( "Adding listener to cache watch. ICacheListener = {0} | " |
| 112 |
+ "ICacheObserver = {1}", obj, cacheWatch ); |
| 113 |
cacheWatch.addCacheListener( obj ); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
@Override |
| 124 |
public <K, V> void removeCacheListener( final String cacheName, final ICacheListener<K, V> obj ) |
| 125 |
throws IOException |
| 126 |
{ |
| 127 |
log.info( "removeCacheListener, cacheName [{0}]", cacheName ); |
| 128 |
|
| 129 |
|
| 130 |
final Set<ICacheListener<?, ?>> listenerSet = cacheMap.get( cacheName ); |
| 131 |
if ( listenerSet != null ) |
| 132 |
{ |
| 133 |
listenerSet.remove( obj ); |
| 134 |
} |
| 135 |
cacheWatch.removeCacheListener( cacheName, obj ); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
@Override |
| 143 |
public <K, V> void removeCacheListener( final ICacheListener<K, V> obj ) |
| 144 |
throws IOException |
| 145 |
{ |
| 146 |
log.info( "removeCacheListener, ICacheListener [{0}]", obj ); |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
cacheMap.values().forEach(set -> { |
| 151 |
log.debug("Before removing [{0}] the listenerSet = {1}", obj, set); |
| 152 |
set.remove( obj ); |
| 153 |
}); |
| 154 |
cacheWatch.removeCacheListener( obj ); |
| 155 |
} |
| 156 |
} |