1 package org.apache.commons.jcs.auxiliary.remote.http.server;
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.io.IOException;
23 import java.util.Map;
24 import java.util.Set;
25
26 import org.apache.commons.jcs.engine.behavior.ICacheElement;
27 import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager;
28 import org.apache.commons.jcs.engine.control.CompositeCache;
29 import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger;
30
31 /**
32 * This does the work. It's called by the processor. The base class wraps the processing calls in
33 * event logs, if an event logger is present.
34 * <p>
35 * For now we assume that all clients are non-cluster clients. And listener notification is not
36 * supported.
37 */
38 public class RemoteHttpCacheService<K, V>
39 extends AbstractRemoteCacheService<K, V>
40 {
41 /** The name used in the event logs. */
42 private static final String EVENT_LOG_SOURCE_NAME = "RemoteHttpCacheServer";
43
44 /** The configuration */
45 private final RemoteHttpCacheServerAttributes remoteHttpCacheServerAttributes;
46
47 /**
48 * Create a process with a cache manager.
49 * <p>
50 * @param cacheManager
51 * @param remoteHttpCacheServerAttributes
52 * @param cacheEventLogger
53 */
54 public RemoteHttpCacheService( ICompositeCacheManager cacheManager,
55 RemoteHttpCacheServerAttributes remoteHttpCacheServerAttributes,
56 ICacheEventLogger cacheEventLogger )
57 {
58 super( cacheManager, cacheEventLogger );
59 setEventLogSourceName( EVENT_LOG_SOURCE_NAME );
60 this.remoteHttpCacheServerAttributes = remoteHttpCacheServerAttributes;
61 }
62
63 /**
64 * Processes a get request.
65 * <p>
66 * If isAllowClusterGet is enabled we will treat this as a normal request or non-remote origins.
67 * <p>
68 * @param cacheName
69 * @param key
70 * @param requesterId
71 * @return ICacheElement
72 * @throws IOException
73 */
74 @Override
75 public ICacheElement<K, V> processGet( String cacheName, K key, long requesterId )
76 throws IOException
77 {
78 CompositeCache<K, V> cache = getCacheManager().getCache( cacheName );
79
80 boolean keepLocal = !remoteHttpCacheServerAttributes.isAllowClusterGet();
81 if ( keepLocal )
82 {
83 return cache.localGet( key );
84 }
85 else
86 {
87 return cache.get( key );
88 }
89 }
90
91 /**
92 * Processes a get request.
93 * <p>
94 * If isAllowClusterGet is enabled we will treat this as a normal request of non-remote
95 * origination.
96 * <p>
97 * @param cacheName
98 * @param keys
99 * @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 }