001package org.apache.commons.jcs.auxiliary;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.commons.jcs.engine.behavior.IElementSerializer;
023import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger;
024import org.apache.commons.jcs.utils.config.OptionConverter;
025import org.apache.commons.jcs.utils.config.PropertySetter;
026import org.apache.commons.jcs.utils.serialization.StandardSerializer;
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029
030import java.util.Properties;
031
032/**
033 * Configuration util for auxiliary caches. I plan to move the auxiliary configuration from the
034 * composite cache configurator here.
035 */
036public class AuxiliaryCacheConfigurator
037{
038    /** The logger. */
039    private static final Log log = LogFactory.getLog( AuxiliaryCacheConfigurator.class );
040
041    /** .attributes */
042    public static final String ATTRIBUTE_PREFIX = ".attributes";
043
044    /**
045     * jcs.auxiliary.NAME.cacheeventlogger=CLASSNAME
046     * <p>
047     * jcs.auxiliary.NAME.cacheeventlogger.attributes.CUSTOMPROPERTY=VALUE
048     */
049    public static final String CACHE_EVENT_LOGGER_PREFIX = ".cacheeventlogger";
050
051    /**
052     * jcs.auxiliary.NAME.serializer=CLASSNAME
053     * <p>
054     * jcs.auxiliary.NAME.serializer.attributes.CUSTOMPROPERTY=VALUE
055     */
056    public static final String SERIALIZER_PREFIX = ".serializer";
057
058    /**
059     * Parses the event logger config, if there is any for the auxiliary.
060     * <p>
061     * @param props
062     * @param auxPrefix - ex. AUXILIARY_PREFIX + auxName
063     * @return cacheEventLogger
064     */
065    public static ICacheEventLogger parseCacheEventLogger( Properties props, String auxPrefix )
066    {
067        ICacheEventLogger cacheEventLogger = null;
068
069        // auxFactory was not previously initialized.
070        String eventLoggerClassName = auxPrefix + CACHE_EVENT_LOGGER_PREFIX;
071        cacheEventLogger = OptionConverter.instantiateByKey( props, eventLoggerClassName, null );
072        if ( cacheEventLogger != null )
073        {
074            String cacheEventLoggerAttributePrefix = auxPrefix + CACHE_EVENT_LOGGER_PREFIX + ATTRIBUTE_PREFIX;
075            PropertySetter.setProperties( cacheEventLogger, props, cacheEventLoggerAttributePrefix + "." );
076            if ( log.isInfoEnabled() )
077            {
078                log.info( "Using custom cache event logger [" + cacheEventLogger + "] for auxiliary [" + auxPrefix
079                    + "]" );
080            }
081        }
082        else
083        {
084            if ( log.isInfoEnabled() )
085            {
086                log.info( "No cache event logger defined for auxiliary [" + auxPrefix + "]" );
087            }
088        }
089        return cacheEventLogger;
090    }
091
092    /**
093     * Parses the element config, if there is any for the auxiliary.
094     * <p>
095     * @param props
096     * @param auxPrefix - ex. AUXILIARY_PREFIX + auxName
097     * @return cacheEventLogger
098     */
099    public static IElementSerializer parseElementSerializer( Properties props, String auxPrefix )
100    {
101        // TODO take in the entire prop key
102        IElementSerializer elementSerializer = null;
103
104        // auxFactory was not previously initialized.
105        String elementSerializerClassName = auxPrefix + SERIALIZER_PREFIX;
106        elementSerializer = OptionConverter.instantiateByKey( props, elementSerializerClassName, null );
107        if ( elementSerializer != null )
108        {
109            String attributePrefix = auxPrefix + SERIALIZER_PREFIX + ATTRIBUTE_PREFIX;
110            PropertySetter.setProperties( elementSerializer, props, attributePrefix + "." );
111            if ( log.isInfoEnabled() )
112            {
113                log.info( "Using custom element serializer [" + elementSerializer + "] for auxiliary [" + auxPrefix
114                    + "]" );
115            }
116        }
117        else
118        {
119            // use the default standard serializer
120            elementSerializer = new StandardSerializer();
121            if ( log.isInfoEnabled() )
122            {
123                log.info( "Using standard serializer [" + elementSerializer + "] for auxiliary [" + auxPrefix + "]" );
124            }
125        }
126        return elementSerializer;
127    }
128}