001package org.apache.commons.jcs.auxiliary.remote.http.server; 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.io.IOException; 023import java.util.Map; 024import java.util.Set; 025 026import org.apache.commons.jcs.engine.behavior.ICacheElement; 027import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager; 028import org.apache.commons.jcs.engine.control.CompositeCache; 029import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger; 030 031/** 032 * This does the work. It's called by the processor. The base class wraps the processing calls in 033 * event logs, if an event logger is present. 034 * <p> 035 * For now we assume that all clients are non-cluster clients. And listener notification is not 036 * supported. 037 */ 038public class RemoteHttpCacheService<K, V> 039 extends AbstractRemoteCacheService<K, V> 040{ 041 /** The name used in the event logs. */ 042 private static final String EVENT_LOG_SOURCE_NAME = "RemoteHttpCacheServer"; 043 044 /** The configuration */ 045 private final RemoteHttpCacheServerAttributes remoteHttpCacheServerAttributes; 046 047 /** 048 * Create a process with a cache manager. 049 * <p> 050 * @param cacheManager 051 * @param remoteHttpCacheServerAttributes 052 * @param cacheEventLogger 053 */ 054 public RemoteHttpCacheService( ICompositeCacheManager cacheManager, 055 RemoteHttpCacheServerAttributes remoteHttpCacheServerAttributes, 056 ICacheEventLogger cacheEventLogger ) 057 { 058 super( cacheManager, cacheEventLogger ); 059 setEventLogSourceName( EVENT_LOG_SOURCE_NAME ); 060 this.remoteHttpCacheServerAttributes = remoteHttpCacheServerAttributes; 061 } 062 063 /** 064 * Processes a get request. 065 * <p> 066 * If isAllowClusterGet is enabled we will treat this as a normal request or non-remote origins. 067 * <p> 068 * @param cacheName 069 * @param key 070 * @param requesterId 071 * @return ICacheElement 072 * @throws IOException 073 */ 074 @Override 075 public ICacheElement<K, V> processGet( String cacheName, K key, long requesterId ) 076 throws IOException 077 { 078 CompositeCache<K, V> cache = getCacheManager().getCache( cacheName ); 079 080 boolean keepLocal = !remoteHttpCacheServerAttributes.isAllowClusterGet(); 081 if ( keepLocal ) 082 { 083 return cache.localGet( key ); 084 } 085 else 086 { 087 return cache.get( key ); 088 } 089 } 090 091 /** 092 * Processes a get request. 093 * <p> 094 * If isAllowClusterGet is enabled we will treat this as a normal request of non-remote 095 * origination. 096 * <p> 097 * @param cacheName 098 * @param keys 099 * @param requesterId 100 * @return Map 101 * @throws IOException 102 */ 103 @Override 104 public Map<K, ICacheElement<K, V>> processGetMultiple( String cacheName, Set<K> keys, long requesterId ) 105 throws IOException 106 { 107 CompositeCache<K, V> cache = getCacheManager().getCache( cacheName ); 108 109 boolean keepLocal = !remoteHttpCacheServerAttributes.isAllowClusterGet(); 110 if ( keepLocal ) 111 { 112 return cache.localGetMultiple( keys ); 113 } 114 else 115 { 116 return cache.getMultiple( keys ); 117 } 118 } 119 120 /** 121 * Processes a get request. 122 * <p> 123 * If isAllowClusterGet is enabled we will treat this as a normal request of non-remote 124 * origination. 125 * <p> 126 * @param cacheName 127 * @param pattern 128 * @param requesterId 129 * @return Map 130 * @throws IOException 131 */ 132 @Override 133 public Map<K, ICacheElement<K, V>> processGetMatching( String cacheName, String pattern, long requesterId ) 134 throws IOException 135 { 136 CompositeCache<K, V> cache = getCacheManager().getCache( cacheName ); 137 138 boolean keepLocal = !remoteHttpCacheServerAttributes.isAllowClusterGet(); 139 if ( keepLocal ) 140 { 141 return cache.localGetMatching( pattern ); 142 } 143 else 144 { 145 return cache.getMatching( pattern ); 146 } 147 } 148 149 /** 150 * Processes an update request. 151 * <p> 152 * If isLocalClusterConsistency is enabled we will treat this as a normal request of non-remote 153 * origination. 154 * <p> 155 * @param item 156 * @param requesterId 157 * @throws IOException 158 */ 159 @Override 160 public void processUpdate( ICacheElement<K, V> item, long requesterId ) 161 throws IOException 162 { 163 CompositeCache<K, V> cache = getCacheManager().getCache( item.getCacheName() ); 164 165 boolean keepLocal = !remoteHttpCacheServerAttributes.isLocalClusterConsistency(); 166 if ( keepLocal ) 167 { 168 cache.localUpdate( item ); 169 } 170 else 171 { 172 cache.update( item ); 173 } 174 } 175 176 /** 177 * Processes a remove request. 178 * <p> 179 * If isLocalClusterConsistency is enabled we will treat this as a normal request of non-remote 180 * origination. 181 * <p> 182 * @param cacheName 183 * @param key 184 * @param requesterId 185 * @throws IOException 186 */ 187 @Override 188 public void processRemove( String cacheName, K key, long requesterId ) 189 throws IOException 190 { 191 CompositeCache<K, V> cache = getCacheManager().getCache( cacheName ); 192 193 boolean keepLocal = !remoteHttpCacheServerAttributes.isLocalClusterConsistency(); 194 if ( keepLocal ) 195 { 196 cache.localRemove( key ); 197 } 198 else 199 { 200 cache.remove( key ); 201 } 202 } 203 204 /** 205 * Processes a removeAll request. 206 * <p> 207 * If isLocalClusterConsistency is enabled we will treat this as a normal request of non-remote 208 * origination. 209 * <p> 210 * @param cacheName 211 * @param requesterId 212 * @throws IOException 213 */ 214 @Override 215 public void processRemoveAll( String cacheName, long requesterId ) 216 throws IOException 217 { 218 CompositeCache<K, V> cache = getCacheManager().getCache( cacheName ); 219 220 boolean keepLocal = !remoteHttpCacheServerAttributes.isLocalClusterConsistency(); 221 if ( keepLocal ) 222 { 223 cache.localRemoveAll(); 224 } 225 else 226 { 227 cache.removeAll(); 228 } 229 } 230 231 /** 232 * Processes a shutdown request. 233 * <p> 234 * @param cacheName 235 * @param requesterId 236 * @throws IOException 237 */ 238 @Override 239 public void processDispose( String cacheName, long requesterId ) 240 throws IOException 241 { 242 CompositeCache<K, V> cache = getCacheManager().getCache( cacheName ); 243 cache.dispose(); 244 } 245 246 /** 247 * This general method should be deprecated. 248 * <p> 249 * @throws IOException 250 */ 251 @Override 252 public void release() 253 throws IOException 254 { 255 //nothing. 256 } 257 258 /** 259 * This is called by the event log. 260 * <p> 261 * @param requesterId 262 * @return requesterId + "" 263 */ 264 @Override 265 protected String getExtraInfoForRequesterId( long requesterId ) 266 { 267 return requesterId + ""; 268 } 269}