View Javadoc
1   package org.apache.commons.jcs.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 org.apache.commons.jcs.engine.behavior.IElementSerializer;
23  import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger;
24  import org.apache.commons.jcs.utils.config.OptionConverter;
25  import org.apache.commons.jcs.utils.config.PropertySetter;
26  import org.apache.commons.jcs.utils.serialization.StandardSerializer;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  import java.util.Properties;
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 = LogFactory.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( Properties props, String auxPrefix )
66      {
67          ICacheEventLogger cacheEventLogger = null;
68  
69          // auxFactory was not previously initialized.
70          String eventLoggerClassName = auxPrefix + CACHE_EVENT_LOGGER_PREFIX;
71          cacheEventLogger = OptionConverter.instantiateByKey( props, eventLoggerClassName, null );
72          if ( cacheEventLogger != null )
73          {
74              String cacheEventLoggerAttributePrefix = auxPrefix + CACHE_EVENT_LOGGER_PREFIX + ATTRIBUTE_PREFIX;
75              PropertySetter.setProperties( cacheEventLogger, props, cacheEventLoggerAttributePrefix + "." );
76              if ( log.isInfoEnabled() )
77              {
78                  log.info( "Using custom cache event logger [" + cacheEventLogger + "] for auxiliary [" + auxPrefix
79                      + "]" );
80              }
81          }
82          else
83          {
84              if ( log.isInfoEnabled() )
85              {
86                  log.info( "No cache event logger defined for auxiliary [" + auxPrefix + "]" );
87              }
88          }
89          return cacheEventLogger;
90      }
91  
92      /**
93       * Parses the element config, if there is any for the auxiliary.
94       * <p>
95       * @param props
96       * @param auxPrefix - ex. AUXILIARY_PREFIX + auxName
97       * @return cacheEventLogger
98       */
99      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 }