package org.apache.commons.jcs3.engine.control; /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import java.util.ArrayList; import java.util.List; import java.util.Properties; import org.apache.commons.jcs3.auxiliary.AuxiliaryCache; import org.apache.commons.jcs3.auxiliary.AuxiliaryCacheAttributes; import org.apache.commons.jcs3.auxiliary.AuxiliaryCacheConfigurator; import org.apache.commons.jcs3.auxiliary.AuxiliaryCacheFactory; import org.apache.commons.jcs3.engine.behavior.ICache; import org.apache.commons.jcs3.engine.behavior.ICompositeCacheAttributes; import org.apache.commons.jcs3.engine.behavior.IElementAttributes; import org.apache.commons.jcs3.engine.behavior.IElementSerializer; import org.apache.commons.jcs3.engine.behavior.IRequireScheduler; import org.apache.commons.jcs3.engine.logging.behavior.ICacheEventLogger; import org.apache.commons.jcs3.engine.match.KeyMatcherPatternImpl; import org.apache.commons.jcs3.engine.match.behavior.IKeyMatcher; import org.apache.commons.jcs3.log.Log; import org.apache.commons.jcs3.log.LogManager; import org.apache.commons.jcs3.utils.config.OptionConverter; import org.apache.commons.jcs3.utils.config.PropertySetter; /** * This class configures JCS based on a properties object. *
* This class is based on the log4j class org.apache.log4j.PropertyConfigurator which was made by: * "Luke Blanshard" <Luke@quiq.com>"Mark DONSZELMANN" <Mark.Donszelmann@cern.ch>"Anders Kristensen" * <akristensen@dynamicsoft.com> */ public class CompositeCacheConfigurator { /** The logger */ private static final Log log = LogManager.getLog( CompositeCacheConfigurator.class ); /** The prefix of relevant system properties */ protected static final String SYSTEM_PROPERTY_KEY_PREFIX = "jcs"; /** normal region prefix */ protected static final String REGION_PREFIX = "jcs.region."; /** system region prefix. might not be used */ protected static final String SYSTEM_REGION_PREFIX = "jcs.system."; /** auxiliary prefix */ protected static final String AUXILIARY_PREFIX = "jcs.auxiliary."; /** .attributes */ protected static final String ATTRIBUTE_PREFIX = ".attributes"; /** .cacheattributes */ protected static final String CACHE_ATTRIBUTE_PREFIX = ".cacheattributes"; /** .elementattributes */ protected static final String ELEMENT_ATTRIBUTE_PREFIX = ".elementattributes"; /** * jcs.auxiliary.NAME.keymatcher=CLASSNAME *
* jcs.auxiliary.NAME.keymatcher.attributes.CUSTOMPROPERTY=VALUE */ public static final String KEY_MATCHER_PREFIX = ".keymatcher"; /** * Constructor for the CompositeCacheConfigurator object */ public CompositeCacheConfigurator() { // empty } /** * Create caches used internally. System status gives them creation priority. *
* @param props Configuration properties * @param ccm Cache hub */ protected void parseSystemRegions( final Properties props, final CompositeCacheManager ccm ) { for (final String key : props.stringPropertyNames() ) { if ( key.startsWith( SYSTEM_REGION_PREFIX ) && key.indexOf( "attributes" ) == -1 ) { final String regionName = key.substring( SYSTEM_REGION_PREFIX.length() ); final String auxiliaries = OptionConverter.findAndSubst( key, props ); final ICache, ?> cache; synchronized ( regionName ) { cache = parseRegion( props, ccm, regionName, auxiliaries, null, SYSTEM_REGION_PREFIX ); } ccm.addCache( regionName, cache ); } } } /** * Parse region elements. *
* @param props Configuration properties
* @param ccm Cache hub
*/
protected void parseRegions( final Properties props, final CompositeCacheManager ccm )
{
final List
* @param props Configuration properties
* @param ccm Cache hub
* @param regName Name of the cache region
* @param auxiliaries Comma separated list of auxiliaries
*
* @return CompositeCache
*/
protected
* This method tells the other parse method the name of the region prefix.
*
* @param props Configuration properties
* @param ccm Cache hub
* @param regName Name of the cache region
* @param auxiliaries Comma separated list of auxiliaries
* @param cca Cache configuration
*
* @return CompositeCache
*/
protected
* @param props Configuration properties
* @param ccm Cache hub
* @param regName Name of the cache region
* @param auxiliaries Comma separated list of auxiliaries
* @param cca Cache configuration
* @param regionPrefix Prefix for the region
*
* @return CompositeCache
*/
protected
* @param props Configuration properties
* @param regName the region name
* @param defaultCCAttr the default cache attributes
*
* @return ICompositeCacheAttributes
*/
protected ICompositeCacheAttributes parseCompositeCacheAttributes( final Properties props,
final String regName, final ICompositeCacheAttributes defaultCCAttr )
{
return parseCompositeCacheAttributes( props, regName, defaultCCAttr, REGION_PREFIX );
}
/**
* Get the main attributes for a region.
*
* @param props Configuration properties
* @param regName the region name
* @param defaultCCAttr the default cache attributes
* @param regionPrefix the region prefix
*
* @return ICompositeCacheAttributes
*/
protected ICompositeCacheAttributes parseCompositeCacheAttributes( final Properties props,
final String regName, final ICompositeCacheAttributes defaultCCAttr, final String regionPrefix )
{
ICompositeCacheAttributes ccAttr;
final String attrName = regionPrefix + regName + CACHE_ATTRIBUTE_PREFIX;
// auxFactory was not previously initialized.
// String prefix = regionPrefix + regName + ATTRIBUTE_PREFIX;
ccAttr = OptionConverter.instantiateByKey( props, attrName, null );
if ( ccAttr == null )
{
log.info( "No special CompositeCacheAttributes class defined for "
+ "key [{0}], using default class.", attrName );
ccAttr = defaultCCAttr;
}
log.debug( "Parsing options for \"{0}\"", attrName );
PropertySetter.setProperties( ccAttr, props, attrName + "." );
ccAttr.setCacheName( regName );
log.debug( "End of parsing for \"{0}\"", attrName );
// GET CACHE FROM FACTORY WITH ATTRIBUTES
ccAttr.setCacheName( regName );
return ccAttr;
}
/**
* Create the element attributes from the properties object for a cache region.
*
* @param props Configuration properties
* @param regName the region name
* @param defaultEAttr the default element attributes
* @param regionPrefix the region prefix
*
* @return IElementAttributes
*/
protected IElementAttributes parseElementAttributes( final Properties props, final String regName,
final IElementAttributes defaultEAttr, final String regionPrefix )
{
IElementAttributes eAttr;
final String attrName = regionPrefix + regName + CompositeCacheConfigurator.ELEMENT_ATTRIBUTE_PREFIX;
// auxFactory was not previously initialized.
// String prefix = regionPrefix + regName + ATTRIBUTE_PREFIX;
eAttr = OptionConverter.instantiateByKey( props, attrName, null );
if ( eAttr == null )
{
log.info( "No special ElementAttribute class defined for key [{0}], "
+ "using default class.", attrName );
eAttr = defaultEAttr;
}
log.debug( "Parsing options for \"{0}\"", attrName );
PropertySetter.setProperties( eAttr, props, attrName + "." );
// eAttr.setCacheName( regName );
log.debug( "End of parsing for \"{0}\"", attrName );
// GET CACHE FROM FACTORY WITH ATTRIBUTES
// eAttr.setCacheName( regName );
return eAttr;
}
/**
* Get an aux cache for the listed aux for a region.
*
* @param props the configuration properties
* @param ccm Cache hub
* @param auxName the name of the auxiliary cache
* @param regName the name of the region.
* @return AuxiliaryCache
*/
protected
* @param props
*/
protected static void overrideWithSystemProperties( final Properties props )
{
// override any setting with values from the system properties.
final Properties sysProps = System.getProperties();
for (final String key : sysProps.stringPropertyNames())
{
if ( key.startsWith( SYSTEM_PROPERTY_KEY_PREFIX ) )
{
log.info( "Using system property [[{0}] [{1}]]", () -> key,
() -> sysProps.getProperty( key ) );
props.setProperty( key, sysProps.getProperty( key ) );
}
}
}
/**
* Creates a custom key matcher if one is defined. Else, it uses the default.
*
* @param props
* @param auxPrefix - ex. AUXILIARY_PREFIX + auxName
* @return IKeyMatcher
*/
protected