1 |
|
package org.apache.jcs.engine.control; |
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
import java.io.FileInputStream; |
23 |
|
import java.io.IOException; |
24 |
|
import java.util.ArrayList; |
25 |
|
import java.util.Enumeration; |
26 |
|
import java.util.List; |
27 |
|
import java.util.Properties; |
28 |
|
import java.util.StringTokenizer; |
29 |
|
|
30 |
|
import org.apache.commons.logging.Log; |
31 |
|
import org.apache.commons.logging.LogFactory; |
32 |
|
import org.apache.jcs.auxiliary.AuxiliaryCache; |
33 |
|
import org.apache.jcs.auxiliary.AuxiliaryCacheAttributes; |
34 |
|
import org.apache.jcs.auxiliary.AuxiliaryCacheFactory; |
35 |
|
import org.apache.jcs.config.OptionConverter; |
36 |
|
import org.apache.jcs.config.PropertySetter; |
37 |
|
import org.apache.jcs.engine.behavior.ICache; |
38 |
|
import org.apache.jcs.engine.behavior.ICompositeCacheAttributes; |
39 |
|
import org.apache.jcs.engine.behavior.IElementAttributes; |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
6 |
public class CompositeCacheConfigurator |
50 |
|
{ |
51 |
1018 |
private final static Log log = LogFactory.getLog( CompositeCacheConfigurator.class ); |
52 |
|
|
53 |
|
final static String DEFAULT_REGION = "jcs.default"; |
54 |
|
|
55 |
|
final static String REGION_PREFIX = "jcs.region."; |
56 |
|
|
57 |
|
final static String SYSTEM_REGION_PREFIX = "jcs.system."; |
58 |
|
|
59 |
|
final static String AUXILIARY_PREFIX = "jcs.auxiliary."; |
60 |
|
|
61 |
|
final static String ATTRIBUTE_PREFIX = ".attributes"; |
62 |
|
|
63 |
|
final static String CACHE_ATTRIBUTE_PREFIX = ".cacheattributes"; |
64 |
|
|
65 |
|
final static String ELEMENT_ATTRIBUTE_PREFIX = ".elementattributes"; |
66 |
|
|
67 |
|
private CompositeCacheManager compositeCacheManager; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
15 |
public CompositeCacheConfigurator( CompositeCacheManager ccMgr ) |
75 |
455 |
{ |
76 |
470 |
this.compositeCacheManager = ccMgr; |
77 |
470 |
} |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
protected void doConfigure( String configFileName ) |
88 |
|
{ |
89 |
0 |
Properties props = new Properties(); |
90 |
|
try |
91 |
|
{ |
92 |
0 |
FileInputStream istream = new FileInputStream( configFileName ); |
93 |
0 |
props.load( istream ); |
94 |
0 |
istream.close(); |
95 |
|
} |
96 |
0 |
catch ( IOException e ) |
97 |
|
{ |
98 |
0 |
log.error( "Could not read configuration file, ignored: " + configFileName, e ); |
99 |
0 |
return; |
100 |
0 |
} |
101 |
|
|
102 |
|
|
103 |
0 |
doConfigure( props ); |
104 |
0 |
} |
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
public void doConfigure( Properties properties ) |
122 |
|
{ |
123 |
267 |
long start = System.currentTimeMillis(); |
124 |
|
|
125 |
|
|
126 |
267 |
compositeCacheManager.props = properties; |
127 |
|
|
128 |
267 |
setDefaultAuxValues( properties ); |
129 |
|
|
130 |
267 |
setDefaultCompositeCacheAttributes( properties ); |
131 |
|
|
132 |
267 |
setDefaultElementAttributes( properties ); |
133 |
|
|
134 |
|
|
135 |
|
|
136 |
267 |
parseSystemRegions( properties ); |
137 |
|
|
138 |
|
|
139 |
267 |
parseRegions( properties ); |
140 |
|
|
141 |
266 |
long end = System.currentTimeMillis(); |
142 |
266 |
if ( log.isInfoEnabled() ) |
143 |
|
{ |
144 |
266 |
log.info( "Finished configuration in " + ( end - start ) + " ms." ); |
145 |
|
} |
146 |
|
|
147 |
266 |
} |
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
protected void setDefaultAuxValues( Properties props ) |
155 |
|
{ |
156 |
267 |
String value = OptionConverter.findAndSubst( DEFAULT_REGION, props ); |
157 |
267 |
compositeCacheManager.defaultAuxValues = value; |
158 |
|
|
159 |
267 |
if ( log.isInfoEnabled() ) |
160 |
|
{ |
161 |
267 |
log.info( "Setting default auxiliaries to " + value ); |
162 |
|
} |
163 |
267 |
} |
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
protected void setDefaultCompositeCacheAttributes( Properties props ) |
171 |
|
{ |
172 |
275 |
ICompositeCacheAttributes icca = parseCompositeCacheAttributes( props, "", |
173 |
8 |
CompositeCacheConfigurator.DEFAULT_REGION ); |
174 |
267 |
compositeCacheManager.setDefaultCacheAttributes( icca ); |
175 |
|
|
176 |
267 |
log.info( "setting defaultCompositeCacheAttributes to " + icca ); |
177 |
267 |
} |
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
protected void setDefaultElementAttributes( Properties props ) |
185 |
|
{ |
186 |
267 |
IElementAttributes iea = parseElementAttributes( props, "", CompositeCacheConfigurator.DEFAULT_REGION ); |
187 |
267 |
compositeCacheManager.setDefaultElementAttributes( iea ); |
188 |
|
|
189 |
267 |
log.info( "setting defaultElementAttributes to " + iea ); |
190 |
267 |
} |
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
protected void parseSystemRegions( Properties props ) |
199 |
|
{ |
200 |
267 |
Enumeration en = props.propertyNames(); |
201 |
6650 |
while ( en.hasMoreElements() ) |
202 |
|
{ |
203 |
6375 |
String key = (String) en.nextElement(); |
204 |
6375 |
if ( key.startsWith( SYSTEM_REGION_PREFIX ) && ( key.indexOf( "attributes" ) == -1 ) ) |
205 |
|
{ |
206 |
29 |
String regionName = key.substring( SYSTEM_REGION_PREFIX.length() ); |
207 |
29 |
String value = OptionConverter.findAndSubst( key, props ); |
208 |
|
ICache cache; |
209 |
29 |
synchronized ( regionName ) |
210 |
|
{ |
211 |
29 |
cache = parseRegion( props, regionName, value, null, SYSTEM_REGION_PREFIX ); |
212 |
29 |
} |
213 |
29 |
compositeCacheManager.systemCaches.put( regionName, cache ); |
214 |
|
|
215 |
|
|
216 |
29 |
compositeCacheManager.caches.put( regionName, cache ); |
217 |
|
} |
218 |
6253 |
} |
219 |
267 |
} |
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
protected void parseRegions( Properties props ) |
227 |
|
{ |
228 |
267 |
List regionNames = new ArrayList(); |
229 |
|
|
230 |
267 |
Enumeration en = props.propertyNames(); |
231 |
6639 |
while ( en.hasMoreElements() ) |
232 |
|
{ |
233 |
6365 |
String key = (String) en.nextElement(); |
234 |
6365 |
if ( key.startsWith( REGION_PREFIX ) && ( key.indexOf( "attributes" ) == -1 ) ) |
235 |
|
{ |
236 |
373 |
String regionName = key.substring( REGION_PREFIX.length() ); |
237 |
|
|
238 |
373 |
regionNames.add( regionName ); |
239 |
|
|
240 |
373 |
String value = OptionConverter.findAndSubst( key, props ); |
241 |
|
ICache cache; |
242 |
380 |
synchronized ( regionName ) |
243 |
|
{ |
244 |
373 |
cache = parseRegion( props, regionName, value ); |
245 |
365 |
} |
246 |
372 |
compositeCacheManager.caches.put( regionName, cache ); |
247 |
|
} |
248 |
6242 |
} |
249 |
|
|
250 |
266 |
if ( log.isInfoEnabled() ) |
251 |
|
{ |
252 |
266 |
log.info( "Parsed regions " + regionNames ); |
253 |
|
} |
254 |
|
|
255 |
266 |
} |
256 |
|
|
257 |
|
|
258 |
|
|
259 |
|
|
260 |
|
|
261 |
|
|
262 |
|
|
263 |
|
|
264 |
|
|
265 |
|
protected CompositeCache parseRegion( Properties props, String regName, String value ) |
266 |
|
{ |
267 |
373 |
return parseRegion( props, regName, value, null, REGION_PREFIX ); |
268 |
|
} |
269 |
|
|
270 |
|
|
271 |
|
|
272 |
|
|
273 |
|
|
274 |
|
|
275 |
|
|
276 |
|
|
277 |
|
|
278 |
|
|
279 |
|
|
280 |
|
|
281 |
|
protected CompositeCache parseRegion( Properties props, String regName, String value, ICompositeCacheAttributes cca ) |
282 |
|
{ |
283 |
203 |
return parseRegion( props, regName, value, cca, REGION_PREFIX ); |
284 |
|
} |
285 |
|
|
286 |
|
|
287 |
|
|
288 |
|
|
289 |
|
|
290 |
|
|
291 |
|
|
292 |
|
|
293 |
|
|
294 |
|
|
295 |
|
|
296 |
|
protected CompositeCache parseRegion( Properties props, String regName, String value, |
297 |
|
ICompositeCacheAttributes cca, String regionPrefix ) |
298 |
|
{ |
299 |
|
|
300 |
|
|
301 |
|
|
302 |
605 |
if ( cca == null ) |
303 |
|
{ |
304 |
402 |
cca = parseCompositeCacheAttributes( props, regName, regionPrefix ); |
305 |
|
} |
306 |
|
|
307 |
605 |
IElementAttributes ea = parseElementAttributes( props, regName, regionPrefix ); |
308 |
|
|
309 |
605 |
CompositeCache cache = new CompositeCache( regName, cca, ea ); |
310 |
|
|
311 |
|
|
312 |
|
|
313 |
605 |
List auxList = new ArrayList(); |
314 |
|
|
315 |
605 |
if ( log.isDebugEnabled() ) |
316 |
|
{ |
317 |
0 |
log.debug( "Parsing region name '" + regName + "', value '" + value + "'" ); |
318 |
|
} |
319 |
|
|
320 |
|
|
321 |
605 |
StringTokenizer st = new StringTokenizer( value, "," ); |
322 |
|
|
323 |
|
|
324 |
|
|
325 |
|
|
326 |
605 |
if ( !( value.startsWith( "," ) || value.equals( "" ) ) ) |
327 |
|
{ |
328 |
|
|
329 |
374 |
if ( !st.hasMoreTokens() ) |
330 |
|
{ |
331 |
0 |
return null; |
332 |
|
} |
333 |
|
} |
334 |
|
|
335 |
|
AuxiliaryCache auxCache; |
336 |
|
String auxName; |
337 |
978 |
while ( st.hasMoreTokens() ) |
338 |
|
{ |
339 |
374 |
auxName = st.nextToken().trim(); |
340 |
374 |
if ( auxName == null || auxName.equals( "," ) ) |
341 |
|
{ |
342 |
0 |
continue; |
343 |
|
} |
344 |
374 |
log.debug( "Parsing auxiliary named \"" + auxName + "\"." ); |
345 |
|
|
346 |
374 |
auxCache = parseAuxiliary( cache, props, auxName, regName ); |
347 |
|
|
348 |
373 |
if ( auxCache != null ) |
349 |
|
{ |
350 |
373 |
auxList.add( auxCache ); |
351 |
373 |
} |
352 |
|
} |
353 |
|
|
354 |
|
|
355 |
|
|
356 |
604 |
cache.setAuxCaches( (AuxiliaryCache[]) auxList.toArray( new AuxiliaryCache[0] ) ); |
357 |
|
|
358 |
|
|
359 |
|
|
360 |
604 |
return cache; |
361 |
|
} |
362 |
|
|
363 |
|
|
364 |
|
|
365 |
|
|
366 |
|
|
367 |
|
|
368 |
|
|
369 |
|
|
370 |
|
protected ICompositeCacheAttributes parseCompositeCacheAttributes( Properties props, String regName ) |
371 |
|
{ |
372 |
0 |
return parseCompositeCacheAttributes( props, regName, REGION_PREFIX ); |
373 |
|
} |
374 |
|
|
375 |
|
|
376 |
|
|
377 |
|
|
378 |
|
|
379 |
|
|
380 |
|
|
381 |
|
|
382 |
|
|
383 |
|
protected ICompositeCacheAttributes parseCompositeCacheAttributes( Properties props, String regName, |
384 |
|
String regionPrefix ) |
385 |
|
{ |
386 |
|
ICompositeCacheAttributes ccAttr; |
387 |
|
|
388 |
669 |
String attrName = regionPrefix + regName + CACHE_ATTRIBUTE_PREFIX; |
389 |
|
|
390 |
|
|
391 |
|
|
392 |
669 |
ccAttr = (ICompositeCacheAttributes) OptionConverter |
393 |
15 |
.instantiateByKey( props, attrName, org.apache.jcs.engine.behavior.ICompositeCacheAttributes.class, null ); |
394 |
|
|
395 |
669 |
if ( ccAttr == null ) |
396 |
|
{ |
397 |
0 |
if ( log.isInfoEnabled() ) |
398 |
|
{ |
399 |
0 |
log.info( "No special CompositeCacheAttributes class defined for key [" + attrName + "], using default class." ); |
400 |
|
} |
401 |
|
|
402 |
0 |
ICompositeCacheAttributes ccAttr2 = compositeCacheManager.getDefaultCacheAttributes(); |
403 |
0 |
ccAttr = ccAttr2.copy(); |
404 |
|
} |
405 |
|
|
406 |
669 |
if ( log.isDebugEnabled() ) |
407 |
|
{ |
408 |
0 |
log.debug( "Parsing options for '" + attrName + "'" ); |
409 |
|
} |
410 |
|
|
411 |
669 |
PropertySetter.setProperties( ccAttr, props, attrName + "." ); |
412 |
669 |
ccAttr.setCacheName( regName ); |
413 |
|
|
414 |
669 |
if ( log.isDebugEnabled() ) |
415 |
|
{ |
416 |
0 |
log.debug( "End of parsing for \"" + attrName + "\"." ); |
417 |
|
} |
418 |
|
|
419 |
|
|
420 |
669 |
ccAttr.setCacheName( regName ); |
421 |
669 |
return ccAttr; |
422 |
|
} |
423 |
|
|
424 |
|
|
425 |
|
|
426 |
|
|
427 |
|
|
428 |
|
|
429 |
|
|
430 |
|
|
431 |
|
|
432 |
|
|
433 |
|
protected IElementAttributes parseElementAttributes( Properties props, String regName, String regionPrefix ) |
434 |
|
{ |
435 |
|
IElementAttributes eAttr; |
436 |
|
|
437 |
872 |
String attrName = regionPrefix + regName + CompositeCacheConfigurator.ELEMENT_ATTRIBUTE_PREFIX; |
438 |
|
|
439 |
|
|
440 |
|
|
441 |
872 |
eAttr = (IElementAttributes) OptionConverter |
442 |
22 |
.instantiateByKey( props, attrName, org.apache.jcs.engine.behavior.IElementAttributes.class, null ); |
443 |
872 |
if ( eAttr == null ) |
444 |
|
{ |
445 |
820 |
if ( log.isInfoEnabled() ) |
446 |
|
{ |
447 |
820 |
log.info( "No special ElementAttribute class defined for key [" + attrName + "], using default class." ); |
448 |
|
} |
449 |
|
|
450 |
820 |
IElementAttributes eAttr2 = compositeCacheManager.getDefaultElementAttributes(); |
451 |
820 |
eAttr = eAttr2.copy(); |
452 |
|
} |
453 |
|
|
454 |
872 |
if ( log.isDebugEnabled() ) |
455 |
|
{ |
456 |
0 |
log.debug( "Parsing options for '" + attrName + "'" ); |
457 |
|
} |
458 |
|
|
459 |
872 |
PropertySetter.setProperties( eAttr, props, attrName + "." ); |
460 |
|
|
461 |
|
|
462 |
872 |
if ( log.isDebugEnabled() ) |
463 |
|
{ |
464 |
0 |
log.debug( "End of parsing for \"" + attrName + "\"." ); |
465 |
|
} |
466 |
|
|
467 |
|
|
468 |
|
|
469 |
872 |
return eAttr; |
470 |
|
} |
471 |
|
|
472 |
|
|
473 |
|
|
474 |
|
|
475 |
|
|
476 |
|
|
477 |
|
|
478 |
|
|
479 |
|
|
480 |
|
|
481 |
|
|
482 |
|
|
483 |
|
|
484 |
|
|
485 |
|
protected AuxiliaryCache parseAuxiliary( CompositeCache cache, Properties props, String auxName, String regName ) |
486 |
|
{ |
487 |
|
AuxiliaryCache auxCache; |
488 |
|
|
489 |
374 |
if ( log.isDebugEnabled() ) |
490 |
|
{ |
491 |
|
|
492 |
|
|
493 |
0 |
log.debug( "parseAuxiliary, Cache = " + cache ); |
494 |
|
} |
495 |
|
|
496 |
|
|
497 |
374 |
AuxiliaryCacheFactory auxFac = compositeCacheManager.registryFacGet( auxName ); |
498 |
374 |
if ( auxFac == null ) |
499 |
|
{ |
500 |
|
|
501 |
145 |
String prefix = AUXILIARY_PREFIX + auxName; |
502 |
145 |
auxFac = (AuxiliaryCacheFactory) OptionConverter |
503 |
|
.instantiateByKey( props, prefix, org.apache.jcs.auxiliary.AuxiliaryCacheFactory.class, null ); |
504 |
145 |
if ( auxFac == null ) |
505 |
|
{ |
506 |
0 |
log.error( "Could not instantiate auxFactory named \"" + auxName + "\"." ); |
507 |
0 |
return null; |
508 |
|
} |
509 |
|
|
510 |
145 |
auxFac.setName( auxName ); |
511 |
|
|
512 |
145 |
compositeCacheManager.registryFacPut( auxFac ); |
513 |
|
} |
514 |
|
|
515 |
|
|
516 |
374 |
AuxiliaryCacheAttributes auxAttr = compositeCacheManager.registryAttrGet( auxName ); |
517 |
374 |
String attrName = AUXILIARY_PREFIX + auxName + ATTRIBUTE_PREFIX; |
518 |
374 |
if ( auxAttr == null ) |
519 |
|
{ |
520 |
|
|
521 |
145 |
String prefix = AUXILIARY_PREFIX + auxName + ATTRIBUTE_PREFIX; |
522 |
145 |
auxAttr = (AuxiliaryCacheAttributes) OptionConverter |
523 |
|
.instantiateByKey( props, prefix, org.apache.jcs.auxiliary.AuxiliaryCacheAttributes.class, null ); |
524 |
145 |
if ( auxFac == null ) |
525 |
|
{ |
526 |
0 |
log.error( "Could not instantiate auxAttr named '" + attrName + "'" ); |
527 |
0 |
return null; |
528 |
|
} |
529 |
145 |
auxAttr.setName( auxName ); |
530 |
145 |
compositeCacheManager.registryAttrPut( auxAttr ); |
531 |
|
} |
532 |
|
|
533 |
374 |
auxAttr = auxAttr.copy(); |
534 |
|
|
535 |
374 |
if ( log.isDebugEnabled() ) |
536 |
|
{ |
537 |
0 |
log.debug( "Parsing options for '" + attrName + "'" ); |
538 |
|
} |
539 |
|
|
540 |
374 |
PropertySetter.setProperties( auxAttr, props, attrName + "." ); |
541 |
374 |
auxAttr.setCacheName( regName ); |
542 |
|
|
543 |
374 |
if ( log.isDebugEnabled() ) |
544 |
|
{ |
545 |
0 |
log.debug( "End of parsing for '" + attrName + "'" ); |
546 |
|
} |
547 |
|
|
548 |
|
|
549 |
374 |
auxAttr.setCacheName( regName ); |
550 |
|
|
551 |
|
|
552 |
|
|
553 |
|
|
554 |
374 |
auxCache = auxFac.createCache( auxAttr, compositeCacheManager ); |
555 |
373 |
return auxCache; |
556 |
|
} |
557 |
|
} |