1 package org.apache.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 java.io.IOException;
23 import java.io.Serializable;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Set;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.jcs.auxiliary.AbstractAuxiliaryCache;
35 import org.apache.jcs.auxiliary.AuxiliaryCache;
36 import org.apache.jcs.auxiliary.AuxiliaryCacheAttributes;
37 import org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheAttributes;
38 import org.apache.jcs.engine.behavior.ICacheElement;
39 import org.apache.jcs.engine.behavior.ICacheType;
40 import org.apache.jcs.engine.stats.StatElement;
41 import org.apache.jcs.engine.stats.Stats;
42 import org.apache.jcs.engine.stats.behavior.IStatElement;
43 import org.apache.jcs.engine.stats.behavior.IStats;
44
45
46
47
48
49
50
51 public class LateralCacheNoWaitFacade
52 extends AbstractAuxiliaryCache
53 {
54
55 private static final long serialVersionUID = -9047687810358008955L;
56
57
58 private final static Log log = LogFactory.getLog( LateralCacheNoWaitFacade.class );
59
60
61 public LateralCacheNoWait[] noWaits;
62
63
64 private final String cacheName;
65
66
67 private final ILateralCacheAttributes lateralCacheAttributes;
68
69
70
71
72
73
74
75 public LateralCacheNoWaitFacade( LateralCacheNoWait[] noWaits, ILateralCacheAttributes cattr )
76 {
77 if ( log.isDebugEnabled() )
78 {
79 log.debug( "CONSTRUCTING NO WAIT FACADE" );
80 }
81 this.noWaits = noWaits;
82 this.cacheName = cattr.getCacheName();
83 this.lateralCacheAttributes = cattr;
84 }
85
86
87
88
89
90
91
92 public boolean containsNoWait( LateralCacheNoWait noWait )
93 {
94 for ( int i = 0; i < noWaits.length; i++ )
95 {
96
97 if ( noWait.equals( noWaits[i] ) )
98 {
99 return true;
100 }
101 }
102 return false;
103 }
104
105
106
107
108
109
110
111 public synchronized boolean addNoWait( LateralCacheNoWait noWait )
112 {
113 if ( noWait == null )
114 {
115 return false;
116 }
117
118 if ( containsNoWait( noWait ) )
119 {
120 if ( log.isDebugEnabled() )
121 {
122 log.debug( "No Wait already contained, [" + noWait + "]" );
123 }
124 return false;
125 }
126
127 LateralCacheNoWait[] newArray = new LateralCacheNoWait[noWaits.length + 1];
128
129 System.arraycopy( noWaits, 0, newArray, 0, noWaits.length );
130
131
132 newArray[noWaits.length] = noWait;
133
134 noWaits = newArray;
135
136 return true;
137 }
138
139
140
141
142
143
144
145 public synchronized boolean removeNoWait( LateralCacheNoWait noWait )
146 {
147 if ( noWait == null )
148 {
149 return false;
150 }
151
152 int position = -1;
153 for ( int i = 0; i < noWaits.length; i++ )
154 {
155
156 if ( noWait.equals( noWaits[i] ) )
157 {
158 position = i;
159 break;
160 }
161 }
162
163 if ( position == -1 )
164 {
165 return false;
166 }
167
168 LateralCacheNoWait[] newArray = new LateralCacheNoWait[noWaits.length - 1];
169
170 System.arraycopy( noWaits, 0, newArray, 0, position );
171 if ( noWaits.length != position )
172 {
173 System.arraycopy( noWaits, position + 1, newArray, position, noWaits.length - position - 1 );
174 }
175 noWaits = newArray;
176
177 return true;
178 }
179
180
181
182
183
184 public void update( ICacheElement ce )
185 throws IOException
186 {
187 if ( log.isDebugEnabled() )
188 {
189 log.debug( "updating through lateral cache facade, noWaits.length = " + noWaits.length );
190 }
191 try
192 {
193 for ( int i = 0; i < noWaits.length; i++ )
194 {
195 noWaits[i].update( ce );
196 }
197 }
198 catch ( Exception ex )
199 {
200 log.error( ex );
201 }
202 }
203
204
205
206
207
208
209
210 public ICacheElement get( Serializable key )
211 {
212 for ( int i = 0; i < noWaits.length; i++ )
213 {
214 try
215 {
216 Object obj = noWaits[i].get( key );
217
218 if ( obj != null )
219 {
220
221
222
223 return (ICacheElement) obj;
224 }
225 }
226 catch ( Exception ex )
227 {
228 log.error( "Failed to get", ex );
229 }
230 }
231 return null;
232 }
233
234
235
236
237
238
239
240
241 public Map<Serializable, ICacheElement> getMultiple(Set<Serializable> keys)
242 {
243 Map<Serializable, ICacheElement> elements = new HashMap<Serializable, ICacheElement>();
244
245 if ( keys != null && !keys.isEmpty() )
246 {
247 for (Serializable key : keys)
248 {
249 ICacheElement element = get( key );
250
251 if ( element != null )
252 {
253 elements.put( key, element );
254 }
255 }
256 }
257
258 return elements;
259 }
260
261
262
263
264
265
266
267
268 public Map<Serializable, ICacheElement> getMatching(String pattern)
269 {
270 Map<Serializable, ICacheElement> elements = new HashMap<Serializable, ICacheElement>();
271 for ( int i = 0; i < noWaits.length; i++ )
272 {
273 try
274 {
275 elements.putAll( noWaits[i].getMatching( pattern ) );
276 }
277 catch ( Exception ex )
278 {
279 log.error( "Failed to get", ex );
280 }
281 }
282 return elements;
283 }
284
285
286
287
288
289 public Set<Serializable> getGroupKeys( String group )
290 {
291 HashSet<Serializable> allKeys = new HashSet<Serializable>();
292 for ( int i = 0; i < noWaits.length; i++ )
293 {
294 AuxiliaryCache aux = noWaits[i];
295 if ( aux != null )
296 {
297 try
298 {
299 allKeys.addAll( aux.getGroupKeys( group ) );
300 }
301 catch ( IOException e )
302 {
303
304 }
305 }
306 }
307 return allKeys;
308 }
309
310
311
312
313
314
315
316 public boolean remove( Serializable key )
317 {
318 try
319 {
320 for ( int i = 0; i < noWaits.length; i++ )
321 {
322 noWaits[i].remove( key );
323 }
324 }
325 catch ( Exception ex )
326 {
327 log.error( ex );
328 }
329 return false;
330 }
331
332
333
334
335 public void removeAll()
336 {
337 try
338 {
339 for ( int i = 0; i < noWaits.length; i++ )
340 {
341 noWaits[i].removeAll();
342 }
343 }
344 catch ( Exception ex )
345 {
346 log.error( ex );
347 }
348 }
349
350
351 public void dispose()
352 {
353 try
354 {
355 for ( int i = 0; i < noWaits.length; i++ )
356 {
357 noWaits[i].dispose();
358 }
359 }
360 catch ( Exception ex )
361 {
362 log.error( ex );
363 }
364 }
365
366
367
368
369
370 public int getSize()
371 {
372 return 0;
373
374 }
375
376
377
378
379
380
381 public int getCacheType()
382 {
383 return ICacheType.LATERAL_CACHE;
384 }
385
386
387
388
389
390
391 public String getCacheName()
392 {
393 return "";
394
395 }
396
397
398
399
400
401
402 public int getStatus()
403 {
404 return 0;
405
406 }
407
408
409
410
411 public AuxiliaryCacheAttributes getAuxiliaryCacheAttributes()
412 {
413 return this.lateralCacheAttributes;
414 }
415
416
417
418
419 @Override
420 public String toString()
421 {
422 return "LateralCacheNoWaitFacade: " + cacheName;
423 }
424
425
426
427
428
429
430 @Override
431 public String getEventLoggingExtraInfo()
432 {
433 return "Lateral Cache No Wait";
434 }
435
436
437
438
439
440 public String getStats()
441 {
442 return getStatistics().toString();
443 }
444
445
446
447
448 public IStats getStatistics()
449 {
450 IStats stats = new Stats();
451 stats.setTypeName( "Lateral Cache No Wait Facade" );
452
453 ArrayList<IStatElement> elems = new ArrayList<IStatElement>();
454
455 IStatElement se = null;
456
457 if ( noWaits != null )
458 {
459 se = new StatElement();
460 se.setName( "Number of No Waits" );
461 se.setData( "" + noWaits.length );
462 elems.add( se );
463
464 for ( int i = 0; i < noWaits.length; i++ )
465 {
466 if ( noWaits[i] != null )
467 {
468
469
470 IStats sStats = noWaits[i].getStatistics();
471 IStatElement[] sSEs = sStats.getStatElements();
472 List<IStatElement> sL = Arrays.asList( sSEs );
473 elems.addAll( sL );
474 }
475 }
476
477 }
478
479
480 IStatElement[] ses = elems.toArray( new StatElement[0] );
481 stats.setStatElements( ses );
482
483 return stats;
484 }
485 }