View Javadoc
1   package org.apache.commons.jcs3.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.jcs3.engine.behavior.ICacheElement;
27  import org.apache.commons.jcs3.engine.behavior.ICompositeCacheManager;
28  import org.apache.commons.jcs3.engine.control.CompositeCache;
29  import org.apache.commons.jcs3.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( final ICompositeCacheManager cacheManager,
55                                     final RemoteHttpCacheServerAttributes remoteHttpCacheServerAttributes,
56                                     final 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( final String cacheName, final K key, final long requesterId )
76          throws IOException
77      {
78          final CompositeCache<K, V> cache = getCacheManager().getCache( cacheName );
79  
80          final boolean keepLocal = !remoteHttpCacheServerAttributes.isAllowClusterGet();
81          if ( keepLocal )
82          {
83              return cache.localGet( key );
84          }
85          return cache.get( key );
86      }
87  
88      /**
89       * Processes a get request.
90       * <p>
91       * If isAllowClusterGet is enabled we will treat this as a normal request of non-remote
92       * origination.
93       * <p>
94       * @param cacheName
95       * @param keys
96       * @param requesterId
97       * @return Map
98       * @throws IOException
99       */
100     @Override
101     public Map<K, ICacheElement<K, V>> processGetMultiple( final String cacheName, final Set<K> keys, final long requesterId )
102         throws IOException
103     {
104         final CompositeCache<K, V> cache = getCacheManager().getCache( cacheName );
105 
106         final boolean keepLocal = !remoteHttpCacheServerAttributes.isAllowClusterGet();
107         if ( keepLocal )
108         {
109             return cache.localGetMultiple( keys );
110         }
111         return cache.getMultiple( keys );
112     }
113 
114     /**
115      * Processes a get request.
116      * <p>
117      * If isAllowClusterGet is enabled we will treat this as a normal request of non-remote
118      * origination.
119      * <p>
120      * @param cacheName
121      * @param pattern
122      * @param requesterId
123      * @return Map
124      * @throws IOException
125      */
126     @Override
127     public Map<K, ICacheElement<K, V>> processGetMatching( final String cacheName, final String pattern, final long requesterId )
128         throws IOException
129     {
130         final CompositeCache<K, V> cache = getCacheManager().getCache( cacheName );
131 
132         final boolean keepLocal = !remoteHttpCacheServerAttributes.isAllowClusterGet();
133         if ( keepLocal )
134         {
135             return cache.localGetMatching( pattern );
136         }
137         return cache.getMatching( pattern );
138     }
139 
140     /**
141      * Processes an update request.
142      * <p>
143      * If isLocalClusterConsistency is enabled we will treat this as a normal request of non-remote
144      * origination.
145      * <p>
146      * @param item
147      * @param requesterId
148      * @throws IOException
149      */
150     @Override
151     public void processUpdate( final ICacheElement<K, V> item, final long requesterId )
152         throws IOException
153     {
154         final CompositeCache<K, V> cache = getCacheManager().getCache( item.getCacheName() );
155 
156         final boolean keepLocal = !remoteHttpCacheServerAttributes.isLocalClusterConsistency();
157         if ( keepLocal )
158         {
159             cache.localUpdate( item );
160         }
161         else
162         {
163             cache.update( item );
164         }
165     }
166 
167     /**
168      * Processes a remove request.
169      * <p>
170      * If isLocalClusterConsistency is enabled we will treat this as a normal request of non-remote
171      * origination.
172      * <p>
173      * @param cacheName
174      * @param key
175      * @param requesterId
176      * @throws IOException
177      */
178     @Override
179     public void processRemove( final String cacheName, final K key, final long requesterId )
180         throws IOException
181     {
182         final CompositeCache<K, V> cache = getCacheManager().getCache( cacheName );
183 
184         final boolean keepLocal = !remoteHttpCacheServerAttributes.isLocalClusterConsistency();
185         if ( keepLocal )
186         {
187             cache.localRemove( key );
188         }
189         else
190         {
191             cache.remove( key );
192         }
193     }
194 
195     /**
196      * Processes a removeAll request.
197      * <p>
198      * If isLocalClusterConsistency is enabled we will treat this as a normal request of non-remote
199      * origination.
200      * <p>
201      * @param cacheName
202      * @param requesterId
203      * @throws IOException
204      */
205     @Override
206     public void processRemoveAll( final String cacheName, final long requesterId )
207         throws IOException
208     {
209         final CompositeCache<K, V> cache = getCacheManager().getCache( cacheName );
210 
211         final boolean keepLocal = !remoteHttpCacheServerAttributes.isLocalClusterConsistency();
212         if ( keepLocal )
213         {
214             cache.localRemoveAll();
215         }
216         else
217         {
218             cache.removeAll();
219         }
220     }
221 
222     /**
223      * Processes a shutdown request.
224      * <p>
225      * @param cacheName
226      * @param requesterId
227      * @throws IOException
228      */
229     @Override
230     public void processDispose( final String cacheName, final long requesterId )
231         throws IOException
232     {
233         final CompositeCache<K, V> cache = getCacheManager().getCache( cacheName );
234         cache.dispose();
235     }
236 
237     /**
238      * This general method should be deprecated.
239      * <p>
240      * @throws IOException
241      */
242     @Override
243     public void release()
244         throws IOException
245     {
246         //nothing.
247     }
248 
249     /**
250      * This is called by the event log.
251      * <p>
252      * @param requesterId
253      * @return requesterId + ""
254      */
255     @Override
256     protected String getExtraInfoForRequesterId( final long requesterId )
257     {
258         return requesterId + "";
259     }
260 }