View Javadoc

1   package org.apache.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.io.Serializable;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import org.apache.jcs.engine.behavior.ICacheElement;
28  import org.apache.jcs.engine.behavior.ICompositeCacheManager;
29  import org.apache.jcs.engine.control.CompositeCache;
30  import org.apache.jcs.engine.logging.behavior.ICacheEventLogger;
31  
32  /**
33   * This does the work. It's called by the processor. The base class wraps the processing calls in
34   * event logs, if an event logger is present.
35   * <p>
36   * For now we assume that all clients are non-cluster clients. And listener notification is not
37   * supported.
38   */
39  public class RemoteHttpCacheService<K extends Serializable, V extends Serializable>
40      extends AbstractRemoteCacheService<K, V>
41  {
42      /** The name used in the event logs. */
43      private static final String EVENT_LOG_SOURCE_NAME = "RemoteHttpCacheServer";
44  
45      /** The configuration */
46      private final RemoteHttpCacheServerAttributes remoteHttpCacheServerAttributes;
47  
48      /**
49       * Create a process with a cache manager.
50       * <p>
51       * @param cacheManager
52       * @param remoteHttpCacheServerAttributes
53       * @param cacheEventLogger
54       */
55      public RemoteHttpCacheService( ICompositeCacheManager cacheManager,
56                                     RemoteHttpCacheServerAttributes remoteHttpCacheServerAttributes,
57                                     ICacheEventLogger cacheEventLogger )
58      {
59          super( cacheManager, cacheEventLogger );
60          setEventLogSourceName( EVENT_LOG_SOURCE_NAME );
61          this.remoteHttpCacheServerAttributes = remoteHttpCacheServerAttributes;
62      }
63  
64      /**
65       * Processes a get request.
66       * <p>
67       * If isAllowClusterGet is enabled we will treat this as a normal request or non-remote origins.
68       * <p>
69       * @param cacheName
70       * @param key
71       * @param requesterId
72       * @return ICacheElement
73       * @throws IOException
74       */
75      @Override
76      public ICacheElement<K, V> processGet( String cacheName, K key, long requesterId )
77          throws IOException
78      {
79          CompositeCache<K, V> cache = getCacheManager().getCache( cacheName );
80  
81          boolean keepLocal = !remoteHttpCacheServerAttributes.isAllowClusterGet();
82          if ( keepLocal )
83          {
84              return cache.localGet( key );
85          }
86          else
87          {
88              return cache.get( key );
89          }
90      }
91  
92      /**
93       * Processes a get request.
94       * <p>
95       * If isAllowClusterGet is enabled we will treat this as a normal request of non-remote
96       * origination.
97       * <p>
98       * @param cacheName
99       * @param keys
100      * @param requesterId
101      * @return Map
102      * @throws IOException
103      */
104     @Override
105     public Map<K, ICacheElement<K, V>> processGetMultiple( String cacheName, Set<K> keys, long requesterId )
106         throws IOException
107     {
108         CompositeCache<K, V> cache = getCacheManager().getCache( cacheName );
109 
110         boolean keepLocal = !remoteHttpCacheServerAttributes.isAllowClusterGet();
111         if ( keepLocal )
112         {
113             return cache.localGetMultiple( keys );
114         }
115         else
116         {
117             return cache.getMultiple( keys );
118         }
119     }
120 
121     /**
122      * Processes a get request.
123      * <p>
124      * If isAllowClusterGet is enabled we will treat this as a normal request of non-remote
125      * origination.
126      * <p>
127      * @param cacheName
128      * @param pattern
129      * @param requesterId
130      * @return Map
131      * @throws IOException
132      */
133     @Override
134     public Map<K, ICacheElement<K, V>> processGetMatching( String cacheName, String pattern, long requesterId )
135         throws IOException
136     {
137         CompositeCache<K, V> cache = getCacheManager().getCache( cacheName );
138 
139         boolean keepLocal = !remoteHttpCacheServerAttributes.isAllowClusterGet();
140         if ( keepLocal )
141         {
142             return cache.localGetMatching( pattern );
143         }
144         else
145         {
146             return cache.getMatching( pattern );
147         }
148     }
149 
150     /**
151      * Processes an update request.
152      * <p>
153      * If isLocalClusterConsistency is enabled we will treat this as a normal request of non-remote
154      * origination.
155      * <p>
156      * @param item
157      * @param requesterId
158      * @throws IOException
159      */
160     @Override
161     public void processUpdate( ICacheElement<K, V> item, long requesterId )
162         throws IOException
163     {
164         CompositeCache<K, V> cache = getCacheManager().getCache( item.getCacheName() );
165 
166         boolean keepLocal = !remoteHttpCacheServerAttributes.isLocalClusterConsistency();
167         if ( keepLocal )
168         {
169             cache.localUpdate( item );
170         }
171         else
172         {
173             cache.update( item );
174         }
175     }
176 
177     /**
178      * Processes a remove request.
179      * <p>
180      * If isLocalClusterConsistency is enabled we will treat this as a normal request of non-remote
181      * origination.
182      * <p>
183      * @param cacheName
184      * @param key
185      * @param requesterId
186      * @throws IOException
187      */
188     @Override
189     public void processRemove( String cacheName, K key, long requesterId )
190         throws IOException
191     {
192         CompositeCache<K, V> cache = getCacheManager().getCache( cacheName );
193 
194         boolean keepLocal = !remoteHttpCacheServerAttributes.isLocalClusterConsistency();
195         if ( keepLocal )
196         {
197             cache.localRemove( key );
198         }
199         else
200         {
201             cache.remove( key );
202         }
203     }
204 
205     /**
206      * Processes a removeAll request.
207      * <p>
208      * If isLocalClusterConsistency is enabled we will treat this as a normal request of non-remote
209      * origination.
210      * <p>
211      * @param cacheName
212      * @param requesterId
213      * @throws IOException
214      */
215     @Override
216     public void processRemoveAll( String cacheName, long requesterId )
217         throws IOException
218     {
219         CompositeCache<K, V> cache = getCacheManager().getCache( cacheName );
220 
221         boolean keepLocal = !remoteHttpCacheServerAttributes.isLocalClusterConsistency();
222         if ( keepLocal )
223         {
224             cache.localRemoveAll();
225         }
226         else
227         {
228             cache.removeAll();
229         }
230     }
231 
232     /**
233      * Processes a shutdown request.
234      * <p>
235      * @param cacheName
236      * @param requesterId
237      * @throws IOException
238      */
239     @Override
240     public void processDispose( String cacheName, long requesterId )
241         throws IOException
242     {
243         CompositeCache<K, V> cache = getCacheManager().getCache( cacheName );
244         cache.dispose();
245     }
246 
247     /**
248      * This general method should be deprecated.
249      * <p>
250      * @throws IOException
251      */
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 }