1 package org.apache.commons.jcs.auxiliary.lateral;
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.AbstractAuxiliaryCache;
23 import org.apache.commons.jcs.auxiliary.AuxiliaryCacheAttributes;
24 import org.apache.commons.jcs.engine.CacheAdaptor;
25 import org.apache.commons.jcs.engine.CacheEventQueueFactory;
26 import org.apache.commons.jcs.engine.CacheInfo;
27 import org.apache.commons.jcs.engine.CacheStatus;
28 import org.apache.commons.jcs.engine.behavior.ICacheElement;
29 import org.apache.commons.jcs.engine.behavior.ICacheEventQueue;
30 import org.apache.commons.jcs.engine.behavior.ICacheServiceNonLocal;
31 import org.apache.commons.jcs.engine.stats.StatElement;
32 import org.apache.commons.jcs.engine.stats.Stats;
33 import org.apache.commons.jcs.engine.stats.behavior.IStatElement;
34 import org.apache.commons.jcs.engine.stats.behavior.IStats;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 import java.io.IOException;
39 import java.rmi.UnmarshalException;
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.HashMap;
43 import java.util.Map;
44 import java.util.Set;
45
46
47
48
49
50 public class LateralCacheNoWait<K, V>
51 extends AbstractAuxiliaryCache<K, V>
52 {
53
54 private static final Log log = LogFactory.getLog( LateralCacheNoWait.class );
55
56
57 private final LateralCache<K, V> cache;
58
59
60 private ICacheEventQueue<K, V> eventQueue;
61
62
63 private int getCount = 0;
64
65
66 private int removeCount = 0;
67
68
69 private int putCount = 0;
70
71
72
73
74
75
76
77 public LateralCacheNoWait( LateralCache<K, V> cache )
78 {
79 this.cache = cache;
80
81 if ( log.isDebugEnabled() )
82 {
83 log.debug( "Constructing LateralCacheNoWait, LateralCache = [" + cache + "]" );
84 }
85
86 CacheEventQueueFactory<K, V> fact = new CacheEventQueueFactory<K, V>();
87 this.eventQueue = fact.createCacheEventQueue( new CacheAdaptor<K, V>( cache ), CacheInfo.listenerId, cache
88 .getCacheName(), cache.getAuxiliaryCacheAttributes().getEventQueuePoolName(), cache
89 .getAuxiliaryCacheAttributes().getEventQueueType() );
90
91
92
93
94
95
96
97 if ( cache.getStatus() == CacheStatus.ERROR )
98 {
99 eventQueue.destroy();
100 }
101 }
102
103
104
105
106
107 @Override
108 public void update( ICacheElement<K, V> ce )
109 throws IOException
110 {
111 putCount++;
112 try
113 {
114 eventQueue.addPutEvent( ce );
115 }
116 catch ( IOException ex )
117 {
118 log.error( ex );
119 eventQueue.destroy();
120 }
121 }
122
123
124
125
126
127
128
129 @Override
130 public ICacheElement<K, V> get( K key )
131 {
132 getCount++;
133 if ( this.getStatus() != CacheStatus.ERROR )
134 {
135 try
136 {
137 return cache.get( key );
138 }
139 catch ( UnmarshalException ue )
140 {
141 log.debug( "Retrying the get owing to UnmarshalException..." );
142 try
143 {
144 return cache.get( key );
145 }
146 catch ( IOException ex )
147 {
148 log.error( "Failed in retrying the get for the second time." );
149 eventQueue.destroy();
150 }
151 }
152 catch ( IOException ex )
153 {
154 eventQueue.destroy();
155 }
156 }
157 return null;
158 }
159
160
161
162
163
164
165
166
167 @Override
168 public Map<K, ICacheElement<K, V>> getMultiple(Set<K> keys)
169 {
170 Map<K, ICacheElement<K, V>> elements = new HashMap<K, ICacheElement<K, V>>();
171
172 if ( keys != null && !keys.isEmpty() )
173 {
174 for (K key : keys)
175 {
176 ICacheElement<K, V> element = get( key );
177
178 if ( element != null )
179 {
180 elements.put( key, element );
181 }
182 }
183 }
184
185 return elements;
186 }
187
188
189
190
191
192
193
194 @Override
195 public Map<K, ICacheElement<K, V>> getMatching(String pattern)
196 {
197 getCount++;
198 if ( this.getStatus() != CacheStatus.ERROR )
199 {
200 try
201 {
202 return cache.getMatching( pattern );
203 }
204 catch ( UnmarshalException ue )
205 {
206 log.debug( "Retrying the get owing to UnmarshalException." );
207 try
208 {
209 return cache.getMatching( pattern );
210 }
211 catch ( IOException ex )
212 {
213 log.error( "Failed in retrying the get for the second time." );
214 eventQueue.destroy();
215 }
216 }
217 catch ( IOException ex )
218 {
219 eventQueue.destroy();
220 }
221 }
222 return Collections.emptyMap();
223 }
224
225
226
227
228
229
230 @Override
231 public Set<K> getKeySet() throws IOException
232 {
233 try
234 {
235 return cache.getKeySet();
236 }
237 catch ( IOException ex )
238 {
239 log.error( ex );
240 eventQueue.destroy();
241 }
242 return Collections.emptySet();
243 }
244
245
246
247
248
249
250
251 @Override
252 public boolean remove( K key )
253 {
254 removeCount++;
255 try
256 {
257 eventQueue.addRemoveEvent( key );
258 }
259 catch ( IOException ex )
260 {
261 log.error( ex );
262 eventQueue.destroy();
263 }
264 return false;
265 }
266
267
268 @Override
269 public void removeAll()
270 {
271 try
272 {
273 eventQueue.addRemoveAllEvent();
274 }
275 catch ( IOException ex )
276 {
277 log.error( ex );
278 eventQueue.destroy();
279 }
280 }
281
282
283 @Override
284 public void dispose()
285 {
286 try
287 {
288 eventQueue.addDisposeEvent();
289 }
290 catch ( IOException ex )
291 {
292 log.error( ex );
293 eventQueue.destroy();
294 }
295 }
296
297
298
299
300
301
302 @Override
303 public int getSize()
304 {
305 return cache.getSize();
306 }
307
308
309
310
311
312
313 @Override
314 public CacheType getCacheType()
315 {
316 return cache.getCacheType();
317 }
318
319
320
321
322
323
324
325 @Override
326 public CacheStatus getStatus()
327 {
328 return eventQueue.isWorking() ? cache.getStatus() : CacheStatus.ERROR;
329 }
330
331
332
333
334
335
336 @Override
337 public String getCacheName()
338 {
339 return cache.getCacheName();
340 }
341
342
343
344
345
346
347
348 public void fixCache( ICacheServiceNonLocal<K, V> lateral )
349 {
350 cache.fixCache( lateral );
351 resetEventQ();
352 }
353
354
355
356
357 public void resetEventQ()
358 {
359 if ( eventQueue.isWorking() )
360 {
361 eventQueue.destroy();
362 }
363 CacheEventQueueFactory<K, V> fact = new CacheEventQueueFactory<K, V>();
364 this.eventQueue = fact.createCacheEventQueue( new CacheAdaptor<K, V>( cache ), CacheInfo.listenerId, cache
365 .getCacheName(), cache.getAuxiliaryCacheAttributes().getEventQueuePoolName(), cache
366 .getAuxiliaryCacheAttributes().getEventQueueType() );
367 }
368
369
370
371
372 @Override
373 public AuxiliaryCacheAttributes getAuxiliaryCacheAttributes()
374 {
375 return cache.getAuxiliaryCacheAttributes();
376 }
377
378
379
380
381
382 @Override
383 public String getStats()
384 {
385 return getStatistics().toString();
386 }
387
388
389
390
391
392
393 @Override
394 public String getEventLoggingExtraInfo()
395 {
396 return "Lateral Cache No Wait";
397 }
398
399
400
401
402 @Override
403 public IStats getStatistics()
404 {
405 IStats stats = new Stats();
406 stats.setTypeName( "Lateral Cache No Wait" );
407
408 ArrayList<IStatElement<?>> elems = new ArrayList<IStatElement<?>>();
409
410
411 IStats eqStats = this.eventQueue.getStatistics();
412 elems.addAll(eqStats.getStatElements());
413
414 elems.add(new StatElement<Integer>( "Get Count", Integer.valueOf(this.getCount) ) );
415 elems.add(new StatElement<Integer>( "Remove Count", Integer.valueOf(this.removeCount) ) );
416 elems.add(new StatElement<Integer>( "Put Count", Integer.valueOf(this.putCount) ) );
417 elems.add(new StatElement<AuxiliaryCacheAttributes>( "Attributes", cache.getAuxiliaryCacheAttributes() ) );
418
419 stats.setStatElements( elems );
420
421 return stats;
422 }
423
424
425
426
427 @Override
428 public String toString()
429 {
430 StringBuilder buf = new StringBuilder();
431 buf.append( " LateralCacheNoWait " );
432 buf.append( " Status = " + this.getStatus() );
433 buf.append( " cache = [" + cache.toString() + "]" );
434 return buf.toString();
435 }
436 }