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.ICacheElement;
23  import org.apache.commons.jcs.engine.behavior.IElementSerializer;
24  import org.apache.commons.jcs.engine.logging.CacheEvent;
25  import org.apache.commons.jcs.engine.logging.behavior.ICacheEvent;
26  import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger;
27  import org.apache.commons.jcs.engine.match.KeyMatcherPatternImpl;
28  import org.apache.commons.jcs.engine.match.behavior.IKeyMatcher;
29  import org.apache.commons.jcs.utils.serialization.StandardSerializer;
30  
31  /** This holds convenience methods used by most auxiliary caches. */
32  public abstract class AbstractAuxiliaryCache<K, V>
33      implements AuxiliaryCache<K, V>
34  {
35      /** An optional event logger */
36      private ICacheEventLogger cacheEventLogger;
37  
38      /** The serializer. Uses a standard serializer by default. */
39      private IElementSerializer elementSerializer = new StandardSerializer();
40  
41      /** Key matcher used by the getMatching API */
42      private IKeyMatcher<K> keyMatcher = new KeyMatcherPatternImpl<K>();
43  
44      /**
45       * Logs an event if an event logger is configured.
46       * <p>
47       * @param item
48       * @param eventName
49       * @return ICacheEvent
50       */
51      protected ICacheEvent<K> createICacheEvent( ICacheElement<K, V> item, String eventName )
52      {
53          if ( cacheEventLogger == null )
54          {
55              return new CacheEvent<K>();
56          }
57          String diskLocation = getEventLoggingExtraInfo();
58          String regionName = null;
59          K key = null;
60          if ( item != null )
61          {
62              regionName = item.getCacheName();
63              key = item.getKey();
64          }
65          return cacheEventLogger.createICacheEvent( getAuxiliaryCacheAttributes().getName(), regionName, eventName,
66                                                     diskLocation, key );
67      }
68  
69      /**
70       * Logs an event if an event logger is configured.
71       * <p>
72       * @param regionName
73       * @param key
74       * @param eventName
75       * @return ICacheEvent
76       */
77      protected <T> ICacheEvent<T> createICacheEvent( String regionName, T key, String eventName )
78      {
79          if ( cacheEventLogger == null )
80          {
81              return new CacheEvent<T>();
82          }
83          String diskLocation = getEventLoggingExtraInfo();
84          return cacheEventLogger.createICacheEvent( getAuxiliaryCacheAttributes().getName(), regionName, eventName,
85                                                     diskLocation, key );
86  
87      }
88  
89      /**
90       * Logs an event if an event logger is configured.
91       * <p>
92       * @param cacheEvent
93       */
94      protected <T> void logICacheEvent( ICacheEvent<T> cacheEvent )
95      {
96          if ( cacheEventLogger != null )
97          {
98              cacheEventLogger.logICacheEvent( cacheEvent );
99          }
100     }
101 
102     /**
103      * Logs an event if an event logger is configured.
104      * <p>
105      * @param source
106      * @param eventName
107      * @param optionalDetails
108      */
109     protected void logApplicationEvent( String source, String eventName, String optionalDetails )
110     {
111         if ( cacheEventLogger != null )
112         {
113             cacheEventLogger.logApplicationEvent( source, eventName, optionalDetails );
114         }
115     }
116 
117     /**
118      * Logs an event if an event logger is configured.
119      * <p>
120      * @param source
121      * @param eventName
122      * @param errorMessage
123      */
124     protected void logError( String source, String eventName, String errorMessage )
125     {
126         if ( cacheEventLogger != null )
127         {
128             cacheEventLogger.logError( source, eventName, errorMessage );
129         }
130     }
131 
132     /**
133      * Gets the extra info for the event log.
134      * <p>
135      * @return IP, or disk location, etc.
136      */
137     public abstract String getEventLoggingExtraInfo();
138 
139     /**
140      * Allows it to be injected.
141      * <p>
142      * @param cacheEventLogger
143      */
144     @Override
145     public void setCacheEventLogger( ICacheEventLogger cacheEventLogger )
146     {
147         this.cacheEventLogger = cacheEventLogger;
148     }
149 
150     /**
151      * Allows it to be injected.
152      * <p>
153      * @return cacheEventLogger
154      */
155     public ICacheEventLogger getCacheEventLogger()
156     {
157         return this.cacheEventLogger;
158     }
159 
160     /**
161      * Allows you to inject a custom serializer. A good example would be a compressing standard
162      * serializer.
163      * <p>
164      * Does not allow you to set it to null.
165      * <p>
166      * @param elementSerializer
167      */
168     @Override
169     public void setElementSerializer( IElementSerializer elementSerializer )
170     {
171         if ( elementSerializer != null )
172         {
173             this.elementSerializer = elementSerializer;
174         }
175     }
176 
177     /**
178      * Allows it to be injected.
179      * <p>
180      * @return elementSerializer
181      */
182     public IElementSerializer getElementSerializer()
183     {
184         return this.elementSerializer;
185     }
186 
187     /**
188      * Sets the key matcher used by get matching.
189      * <p>
190      * @param keyMatcher
191      */
192     @Override
193     public void setKeyMatcher( IKeyMatcher<K> keyMatcher )
194     {
195         if ( keyMatcher != null )
196         {
197             this.keyMatcher = keyMatcher;
198         }
199     }
200 
201     /**
202      * Returns the key matcher used by get matching.
203      * <p>
204      * @return keyMatcher
205      */
206     public IKeyMatcher<K> getKeyMatcher()
207     {
208         return this.keyMatcher;
209     }
210 }