001package org.apache.commons.jcs3.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 java.util.Properties;
023
024import org.apache.commons.jcs3.engine.behavior.IElementSerializer;
025import org.apache.commons.jcs3.engine.logging.behavior.ICacheEventLogger;
026import org.apache.commons.jcs3.log.Log;
027import org.apache.commons.jcs3.log.LogManager;
028import org.apache.commons.jcs3.utils.config.OptionConverter;
029import org.apache.commons.jcs3.utils.config.PropertySetter;
030import org.apache.commons.jcs3.utils.serialization.StandardSerializer;
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 = LogManager.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( final Properties props, final String auxPrefix )
066    {
067
068        // auxFactory was not previously initialized.
069        final String eventLoggerClassName = auxPrefix + CACHE_EVENT_LOGGER_PREFIX;
070        final ICacheEventLogger cacheEventLogger = OptionConverter.instantiateByKey( props, eventLoggerClassName, null );
071        if ( cacheEventLogger != null )
072        {
073            final String cacheEventLoggerAttributePrefix = auxPrefix + CACHE_EVENT_LOGGER_PREFIX + ATTRIBUTE_PREFIX;
074            PropertySetter.setProperties( cacheEventLogger, props, cacheEventLoggerAttributePrefix + "." );
075            log.info( "Using custom cache event logger [{0}] for auxiliary [{1}]",
076                    cacheEventLogger, auxPrefix );
077        }
078        else
079        {
080            log.info( "No cache event logger defined for auxiliary [{0}]", auxPrefix );
081        }
082        return cacheEventLogger;
083    }
084
085    /**
086     * Parses the element config, if there is any for the auxiliary.
087     * <p>
088     * @param props
089     * @param auxPrefix - ex. AUXILIARY_PREFIX + auxName
090     * @return cacheEventLogger
091     */
092    public static IElementSerializer parseElementSerializer( final Properties props, final String auxPrefix )
093    {
094        // TODO take in the entire prop key
095        // auxFactory was not previously initialized.
096        final String elementSerializerClassName = auxPrefix + SERIALIZER_PREFIX;
097        IElementSerializer elementSerializer = OptionConverter.instantiateByKey( props, elementSerializerClassName, null );
098        if ( elementSerializer != null )
099        {
100            final String attributePrefix = auxPrefix + SERIALIZER_PREFIX + ATTRIBUTE_PREFIX;
101            PropertySetter.setProperties( elementSerializer, props, attributePrefix + "." );
102            log.info( "Using custom element serializer [{0}] for auxiliary [{1}]",
103                    elementSerializer, auxPrefix );
104        }
105        else
106        {
107            // use the default standard serializer
108            elementSerializer = new StandardSerializer();
109            log.info( "Using standard serializer [{0}] for auxiliary [{1}]",
110                    elementSerializer, auxPrefix );
111        }
112        return elementSerializer;
113    }
114}