View Javadoc
1   package org.apache.commons.jcs3.auxiliary;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Properties;
23  
24  import org.apache.commons.jcs3.engine.behavior.IElementSerializer;
25  import org.apache.commons.jcs3.engine.logging.behavior.ICacheEventLogger;
26  import org.apache.commons.jcs3.log.Log;
27  import org.apache.commons.jcs3.log.LogManager;
28  import org.apache.commons.jcs3.utils.config.OptionConverter;
29  import org.apache.commons.jcs3.utils.config.PropertySetter;
30  import org.apache.commons.jcs3.utils.serialization.StandardSerializer;
31  
32  /**
33   * Configuration util for auxiliary caches. I plan to move the auxiliary configuration from the
34   * composite cache configurator here.
35   */
36  public class AuxiliaryCacheConfigurator
37  {
38      /** The logger. */
39      private static final Log log = LogManager.getLog( AuxiliaryCacheConfigurator.class );
40  
41      /** .attributes */
42      public static final String ATTRIBUTE_PREFIX = ".attributes";
43  
44      /**
45       * jcs.auxiliary.NAME.cacheeventlogger=CLASSNAME
46       * <p>
47       * jcs.auxiliary.NAME.cacheeventlogger.attributes.CUSTOMPROPERTY=VALUE
48       */
49      public static final String CACHE_EVENT_LOGGER_PREFIX = ".cacheeventlogger";
50  
51      /**
52       * jcs.auxiliary.NAME.serializer=CLASSNAME
53       * <p>
54       * jcs.auxiliary.NAME.serializer.attributes.CUSTOMPROPERTY=VALUE
55       */
56      public static final String SERIALIZER_PREFIX = ".serializer";
57  
58      /**
59       * Parses the event logger config, if there is any for the auxiliary.
60       * <p>
61       * @param props
62       * @param auxPrefix - ex. AUXILIARY_PREFIX + auxName
63       * @return cacheEventLogger
64       */
65      public static ICacheEventLogger parseCacheEventLogger( final Properties props, final String auxPrefix )
66      {
67  
68          // auxFactory was not previously initialized.
69          final String eventLoggerClassName = auxPrefix + CACHE_EVENT_LOGGER_PREFIX;
70          final ICacheEventLogger cacheEventLogger = OptionConverter.instantiateByKey( props, eventLoggerClassName, null );
71          if ( cacheEventLogger != null )
72          {
73              final String cacheEventLoggerAttributePrefix = auxPrefix + CACHE_EVENT_LOGGER_PREFIX + ATTRIBUTE_PREFIX;
74              PropertySetter.setProperties( cacheEventLogger, props, cacheEventLoggerAttributePrefix + "." );
75              log.info( "Using custom cache event logger [{0}] for auxiliary [{1}]",
76                      cacheEventLogger, auxPrefix );
77          }
78          else
79          {
80              log.info( "No cache event logger defined for auxiliary [{0}]", auxPrefix );
81          }
82          return cacheEventLogger;
83      }
84  
85      /**
86       * Parses the element config, if there is any for the auxiliary.
87       * <p>
88       * @param props
89       * @param auxPrefix - ex. AUXILIARY_PREFIX + auxName
90       * @return cacheEventLogger
91       */
92      public static IElementSerializer parseElementSerializer( final Properties props, final String auxPrefix )
93      {
94          // TODO take in the entire prop key
95          // auxFactory was not previously initialized.
96          final String elementSerializerClassName = auxPrefix + SERIALIZER_PREFIX;
97          IElementSerializer elementSerializer = OptionConverter.instantiateByKey( props, elementSerializerClassName, null );
98          if ( elementSerializer != null )
99          {
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 }