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.ICacheElement; 023import org.apache.commons.jcs.engine.behavior.IElementSerializer; 024import org.apache.commons.jcs.engine.logging.CacheEvent; 025import org.apache.commons.jcs.engine.logging.behavior.ICacheEvent; 026import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger; 027import org.apache.commons.jcs.engine.match.KeyMatcherPatternImpl; 028import org.apache.commons.jcs.engine.match.behavior.IKeyMatcher; 029import org.apache.commons.jcs.utils.serialization.StandardSerializer; 030 031/** This holds convenience methods used by most auxiliary caches. */ 032public abstract class AbstractAuxiliaryCache<K, V> 033 implements AuxiliaryCache<K, V> 034{ 035 /** An optional event logger */ 036 private ICacheEventLogger cacheEventLogger; 037 038 /** The serializer. Uses a standard serializer by default. */ 039 private IElementSerializer elementSerializer = new StandardSerializer(); 040 041 /** Key matcher used by the getMatching API */ 042 private IKeyMatcher<K> keyMatcher = new KeyMatcherPatternImpl<K>(); 043 044 /** 045 * Logs an event if an event logger is configured. 046 * <p> 047 * @param item 048 * @param eventName 049 * @return ICacheEvent 050 */ 051 protected ICacheEvent<K> createICacheEvent( ICacheElement<K, V> item, String eventName ) 052 { 053 if ( cacheEventLogger == null ) 054 { 055 return new CacheEvent<K>(); 056 } 057 String diskLocation = getEventLoggingExtraInfo(); 058 String regionName = null; 059 K key = null; 060 if ( item != null ) 061 { 062 regionName = item.getCacheName(); 063 key = item.getKey(); 064 } 065 return cacheEventLogger.createICacheEvent( getAuxiliaryCacheAttributes().getName(), regionName, eventName, 066 diskLocation, key ); 067 } 068 069 /** 070 * Logs an event if an event logger is configured. 071 * <p> 072 * @param regionName 073 * @param key 074 * @param eventName 075 * @return ICacheEvent 076 */ 077 protected <T> ICacheEvent<T> createICacheEvent( String regionName, T key, String eventName ) 078 { 079 if ( cacheEventLogger == null ) 080 { 081 return new CacheEvent<T>(); 082 } 083 String diskLocation = getEventLoggingExtraInfo(); 084 return cacheEventLogger.createICacheEvent( getAuxiliaryCacheAttributes().getName(), regionName, eventName, 085 diskLocation, key ); 086 087 } 088 089 /** 090 * Logs an event if an event logger is configured. 091 * <p> 092 * @param cacheEvent 093 */ 094 protected <T> void logICacheEvent( ICacheEvent<T> cacheEvent ) 095 { 096 if ( cacheEventLogger != null ) 097 { 098 cacheEventLogger.logICacheEvent( cacheEvent ); 099 } 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}