View Javadoc
1   package org.apache.commons.jcs.auxiliary.remote.http.client;
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 org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheDispatcher;
23  import org.apache.commons.jcs.auxiliary.remote.http.client.behavior.IRemoteHttpCacheClient;
24  import org.apache.commons.jcs.auxiliary.remote.util.RemoteCacheRequestFactory;
25  import org.apache.commons.jcs.auxiliary.remote.value.RemoteCacheRequest;
26  import org.apache.commons.jcs.auxiliary.remote.value.RemoteCacheResponse;
27  import org.apache.commons.jcs.engine.behavior.ICacheElement;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  import java.io.IOException;
32  import java.io.Serializable;
33  import java.util.Collections;
34  import java.util.Map;
35  import java.util.Set;
36  
37  /** This is the service used by the remote http auxiliary cache. */
38  public class RemoteHttpCacheClient<K, V>
39      implements IRemoteHttpCacheClient<K, V>
40  {
41      /** The Logger. */
42      private static final Log log = LogFactory.getLog( RemoteHttpCacheClient.class );
43  
44      /** The internal client. */
45      private IRemoteCacheDispatcher remoteDispatcher;
46  
47      /** The remote attributes */
48      private RemoteHttpCacheAttributes remoteHttpCacheAttributes;
49  
50      /** Set to true when initialize is called */
51      private boolean initialized = false;
52  
53      /** For factory construction. */
54      public RemoteHttpCacheClient()
55      {
56          // does nothing
57      }
58  
59      /**
60       * Constructs a client.
61       * <p>
62       * @param attributes
63       */
64      public RemoteHttpCacheClient( RemoteHttpCacheAttributes attributes )
65      {
66          setRemoteHttpCacheAttributes( attributes );
67          initialize( attributes );
68      }
69  
70      /**
71       * The provides an extension point. If you want to extend this and use a special dispatcher,
72       * here is the place to do it.
73       * <p>
74       * @param attributes
75       */
76      @Override
77      public void initialize( RemoteHttpCacheAttributes attributes )
78      {
79          setRemoteDispatcher( new RemoteHttpCacheDispatcher( attributes ) );
80  
81          if ( log.isInfoEnabled() )
82          {
83              log.info( "Created remote Dispatcher." + getRemoteDispatcher() );
84          }
85          setInitialized( true );
86      }
87  
88      /**
89       * Create a request, process, extract the payload.
90       * <p>
91       * @param cacheName
92       * @param key
93       * @return ICacheElement
94       * @throws IOException
95       */
96      @Override
97      public ICacheElement<K, V> get( String cacheName, K key )
98          throws IOException
99      {
100         return get( cacheName, key, 0 );
101     }
102 
103     /**
104      * Create a request, process, extract the payload.
105      * <p>
106      * @param cacheName
107      * @param key
108      * @param requesterId
109      * @return ICacheElement
110      * @throws IOException
111      */
112     @Override
113     public ICacheElement<K, V> get( String cacheName, K key, long requesterId )
114         throws IOException
115     {
116         if ( !isInitialized() )
117         {
118             String message = "The Remote Http Client is not initialized.  Cannot process request.";
119             log.warn( message );
120             throw new IOException( message );
121         }
122         RemoteCacheRequest<K, Serializable> remoteHttpCacheRequest =
123             RemoteCacheRequestFactory.createGetRequest( cacheName, key, requesterId );
124 
125         RemoteCacheResponse<ICacheElement<K, V>> remoteHttpCacheResponse =
126             getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest );
127 
128         if ( log.isDebugEnabled() )
129         {
130             log.debug( "Get [" + key + "] = " + remoteHttpCacheResponse );
131         }
132 
133         if ( remoteHttpCacheResponse != null)
134         {
135             return remoteHttpCacheResponse.getPayload();
136         }
137 
138         return null;
139     }
140 
141     /**
142      * Gets multiple items from the cache matching the pattern.
143      * <p>
144      * @param cacheName
145      * @param pattern
146      * @return a map of K key to ICacheElement&lt;K, V&gt; element, or an empty map if there is no
147      *         data in cache matching the pattern.
148      * @throws IOException
149      */
150     @Override
151     public Map<K, ICacheElement<K, V>> getMatching( String cacheName, String pattern )
152         throws IOException
153     {
154         return getMatching( cacheName, pattern, 0 );
155     }
156 
157     /**
158      * Gets multiple items from the cache matching the pattern.
159      * <p>
160      * @param cacheName
161      * @param pattern
162      * @param requesterId
163      * @return a map of K key to ICacheElement&lt;K, V&gt; element, or an empty map if there is no
164      *         data in cache matching the pattern.
165      * @throws IOException
166      */
167     @Override
168     public Map<K, ICacheElement<K, V>> getMatching( String cacheName, String pattern, long requesterId )
169         throws IOException
170     {
171         if ( !isInitialized() )
172         {
173             String message = "The Remote Http Client is not initialized.  Cannot process request.";
174             log.warn( message );
175             throw new IOException( message );
176         }
177 
178         RemoteCacheRequest<K, V> remoteHttpCacheRequest =
179             RemoteCacheRequestFactory.createGetMatchingRequest( cacheName, pattern, requesterId );
180 
181         RemoteCacheResponse<Map<K, ICacheElement<K, V>>> remoteHttpCacheResponse =
182             getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest );
183 
184         if ( log.isDebugEnabled() )
185         {
186             log.debug( "GetMatching [" + pattern + "] = " + remoteHttpCacheResponse );
187         }
188 
189         return remoteHttpCacheResponse.getPayload();
190     }
191 
192     /**
193      * Gets multiple items from the cache based on the given set of keys.
194      * <p>
195      * @param cacheName
196      * @param keys
197      * @return a map of K key to ICacheElement&lt;K, V&gt; element, or an empty map if there is no
198      *         data in cache for any of these keys
199      * @throws IOException
200      */
201     @Override
202     public Map<K, ICacheElement<K, V>> getMultiple( String cacheName, Set<K> keys )
203         throws IOException
204     {
205         return getMultiple( cacheName, keys, 0 );
206     }
207 
208     /**
209      * Gets multiple items from the cache based on the given set of keys.
210      * <p>
211      * @param cacheName
212      * @param keys
213      * @param requesterId
214      * @return a map of K key to ICacheElement&lt;K, V&gt; element, or an empty map if there is no
215      *         data in cache for any of these keys
216      * @throws IOException
217      */
218     @Override
219     public Map<K, ICacheElement<K, V>> getMultiple( String cacheName, Set<K> keys, long requesterId )
220         throws IOException
221     {
222         if ( !isInitialized() )
223         {
224             String message = "The Remote Http Client is not initialized.  Cannot process request.";
225             log.warn( message );
226             throw new IOException( message );
227         }
228 
229         RemoteCacheRequest<K, V> remoteHttpCacheRequest =
230             RemoteCacheRequestFactory.createGetMultipleRequest( cacheName, keys, requesterId );
231 
232         RemoteCacheResponse<Map<K, ICacheElement<K, V>>> remoteHttpCacheResponse =
233             getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest );
234 
235         if ( log.isDebugEnabled() )
236         {
237             log.debug( "GetMultiple [" + keys + "] = " + remoteHttpCacheResponse );
238         }
239 
240         return remoteHttpCacheResponse.getPayload();
241     }
242 
243     /**
244      * Removes the given key from the specified cache.
245      * <p>
246      * @param cacheName
247      * @param key
248      * @throws IOException
249      */
250     @Override
251     public void remove( String cacheName, K key )
252         throws IOException
253     {
254         remove( cacheName, key, 0 );
255     }
256 
257     /**
258      * Removes the given key from the specified cache.
259      * <p>
260      * @param cacheName
261      * @param key
262      * @param requesterId
263      * @throws IOException
264      */
265     @Override
266     public void remove( String cacheName, K key, long requesterId )
267         throws IOException
268     {
269         if ( !isInitialized() )
270         {
271             String message = "The Remote Http Client is not initialized.  Cannot process request.";
272             log.warn( message );
273             throw new IOException( message );
274         }
275 
276         RemoteCacheRequest<K, V> remoteHttpCacheRequest =
277             RemoteCacheRequestFactory.createRemoveRequest( cacheName, key, requesterId );
278 
279         getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest );
280     }
281 
282     /**
283      * Remove all keys from the specified cache.
284      * <p>
285      * @param cacheName
286      * @throws IOException
287      */
288     @Override
289     public void removeAll( String cacheName )
290         throws IOException
291     {
292         removeAll( cacheName, 0 );
293     }
294 
295     /**
296      * Remove all keys from the sepcified cache.
297      * <p>
298      * @param cacheName
299      * @param requesterId
300      * @throws IOException
301      */
302     @Override
303     public void removeAll( String cacheName, long requesterId )
304         throws IOException
305     {
306         if ( !isInitialized() )
307         {
308             String message = "The Remote Http Client is not initialized.  Cannot process request.";
309             log.warn( message );
310             throw new IOException( message );
311         }
312 
313         RemoteCacheRequest<K, V> remoteHttpCacheRequest =
314             RemoteCacheRequestFactory.createRemoveAllRequest( cacheName, requesterId );
315 
316         getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest );
317     }
318 
319     /**
320      * Puts a cache item to the cache.
321      * <p>
322      * @param item
323      * @throws IOException
324      */
325     @Override
326     public void update( ICacheElement<K, V> item )
327         throws IOException
328     {
329         update( item, 0 );
330     }
331 
332     /**
333      * Puts a cache item to the cache.
334      * <p>
335      * @param cacheElement
336      * @param requesterId
337      * @throws IOException
338      */
339     @Override
340     public void update( ICacheElement<K, V> cacheElement, long requesterId )
341         throws IOException
342     {
343         if ( !isInitialized() )
344         {
345             String message = "The Remote Http Client is not initialized.  Cannot process request.";
346             log.warn( message );
347             throw new IOException( message );
348         }
349 
350         RemoteCacheRequest<K, V> remoteHttpCacheRequest =
351             RemoteCacheRequestFactory.createUpdateRequest( cacheElement, requesterId );
352 
353         getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest );
354     }
355 
356     /**
357      * Frees the specified cache.
358      * <p>
359      * @param cacheName
360      * @throws IOException
361      */
362     @Override
363     public void dispose( String cacheName )
364         throws IOException
365     {
366         if ( !isInitialized() )
367         {
368             String message = "The Remote Http Client is not initialized.  Cannot process request.";
369             log.warn( message );
370             throw new IOException( message );
371         }
372 
373         RemoteCacheRequest<K, V> remoteHttpCacheRequest =
374             RemoteCacheRequestFactory.createDisposeRequest( cacheName, 0 );
375 
376         getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest );
377     }
378 
379     /**
380      * Frees the specified cache.
381      * <p>
382      * @throws IOException
383      */
384     @Override
385     public void release()
386         throws IOException
387     {
388         // noop
389     }
390 
391     /**
392      * Return the keys in this cache.
393      * <p>
394      * @param cacheName the name of the cache
395      * @see org.apache.commons.jcs.auxiliary.AuxiliaryCache#getKeySet()
396      */
397     @Override
398     public Set<K> getKeySet( String cacheName ) throws IOException
399     {
400         if ( !isInitialized() )
401         {
402             String message = "The Remote Http Client is not initialized.  Cannot process request.";
403             log.warn( message );
404             throw new IOException( message );
405         }
406 
407         RemoteCacheRequest<String, String> remoteHttpCacheRequest =
408             RemoteCacheRequestFactory.createGetKeySetRequest(cacheName, 0 );
409 
410         RemoteCacheResponse<Set<K>> remoteHttpCacheResponse = getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest );
411 
412         if ( remoteHttpCacheResponse != null && remoteHttpCacheResponse.getPayload() != null )
413         {
414             return remoteHttpCacheResponse.getPayload();
415         }
416 
417         return Collections.emptySet();
418     }
419 
420     /**
421      * Make and alive request.
422      * <p>
423      * @return true if we make a successful alive request.
424      * @throws IOException
425      */
426     @Override
427     public boolean isAlive()
428         throws IOException
429     {
430         if ( !isInitialized() )
431         {
432             String message = "The Remote Http Client is not initialized.  Cannot process request.";
433             log.warn( message );
434             throw new IOException( message );
435         }
436 
437         RemoteCacheRequest<K, V> remoteHttpCacheRequest = RemoteCacheRequestFactory.createAliveCheckRequest( 0 );
438         RemoteCacheResponse<String> remoteHttpCacheResponse =
439             getRemoteDispatcher().dispatchRequest( remoteHttpCacheRequest );
440 
441         if ( remoteHttpCacheResponse != null )
442         {
443             return remoteHttpCacheResponse.isSuccess();
444         }
445 
446         return false;
447     }
448 
449     /**
450      * @param remoteDispatcher the remoteDispatcher to set
451      */
452     public void setRemoteDispatcher( IRemoteCacheDispatcher remoteDispatcher )
453     {
454         this.remoteDispatcher = remoteDispatcher;
455     }
456 
457     /**
458      * @return the remoteDispatcher
459      */
460     public IRemoteCacheDispatcher getRemoteDispatcher()
461     {
462         return remoteDispatcher;
463     }
464 
465     /**
466      * @param remoteHttpCacheAttributes the remoteHttpCacheAttributes to set
467      */
468     public void setRemoteHttpCacheAttributes( RemoteHttpCacheAttributes remoteHttpCacheAttributes )
469     {
470         this.remoteHttpCacheAttributes = remoteHttpCacheAttributes;
471     }
472 
473     /**
474      * @return the remoteHttpCacheAttributes
475      */
476     public RemoteHttpCacheAttributes getRemoteHttpCacheAttributes()
477     {
478         return remoteHttpCacheAttributes;
479     }
480 
481     /**
482      * @param initialized the initialized to set
483      */
484     protected void setInitialized( boolean initialized )
485     {
486         this.initialized = initialized;
487     }
488 
489     /**
490      * @return the initialized
491      */
492     protected boolean isInitialized()
493     {
494         return initialized;
495     }
496 }