View Javadoc
1   package org.apache.commons.jcs.auxiliary.remote.util;
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.util.Set;
23  
24  import org.apache.commons.jcs.auxiliary.remote.value.RemoteCacheRequest;
25  import org.apache.commons.jcs.auxiliary.remote.value.RemoteRequestType;
26  import org.apache.commons.jcs.engine.behavior.ICacheElement;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  /**
31   * This creates request objects. You could write your own client and use the objects from this
32   * factory.
33   */
34  public class RemoteCacheRequestFactory
35  {
36      /** The Logger. */
37      private static final Log log = LogFactory.getLog( RemoteCacheRequestFactory.class );
38  
39      /**
40       * Create generic request
41       * @param cacheName cache name
42       * @param requestType type of request
43       * @param requesterId id of requester
44       * @return the request
45       */
46      private static <K, V> RemoteCacheRequest<K, V> createRequest(String cacheName, RemoteRequestType requestType, long requesterId)
47      {
48          RemoteCacheRequest<K, V> request = new RemoteCacheRequest<K, V>();
49          request.setCacheName( cacheName );
50          request.setRequestType( requestType );
51          request.setRequesterId( requesterId );
52  
53          if ( log.isDebugEnabled() )
54          {
55              log.debug( "Created: " + request );
56          }
57  
58          return request;
59      }
60  
61      /**
62       * Creates a get Request.
63       * <p>
64       * @param cacheName
65       * @param key
66       * @param requesterId
67       * @return RemoteHttpCacheRequest
68       */
69      public static <K, V> RemoteCacheRequest<K, V> createGetRequest( String cacheName, K key, long requesterId )
70      {
71          RemoteCacheRequest<K, V> request = createRequest(cacheName, RemoteRequestType.GET, requesterId);
72          request.setKey( key );
73  
74          return request;
75      }
76  
77      /**
78       * Creates a getMatching Request.
79       * <p>
80       * @param cacheName
81       * @param pattern
82       * @param requesterId
83       * @return RemoteHttpCacheRequest
84       */
85      public static <K, V> RemoteCacheRequest<K, V> createGetMatchingRequest( String cacheName, String pattern, long requesterId )
86      {
87          RemoteCacheRequest<K, V> request = createRequest(cacheName, RemoteRequestType.GET_MATCHING, requesterId);
88          request.setPattern( pattern );
89  
90          return request;
91      }
92  
93      /**
94       * Creates a getMultiple Request.
95       * <p>
96       * @param cacheName
97       * @param keys
98       * @param requesterId
99       * @return RemoteHttpCacheRequest
100      */
101     public static <K, V> RemoteCacheRequest<K, V> createGetMultipleRequest( String cacheName, Set<K> keys, long requesterId )
102     {
103         RemoteCacheRequest<K, V> request = createRequest(cacheName, RemoteRequestType.GET_MULTIPLE, requesterId);
104         request.setKeySet(keys);
105 
106         return request;
107     }
108 
109     /**
110      * Creates a remove Request.
111      * <p>
112      * @param cacheName
113      * @param key
114      * @param requesterId
115      * @return RemoteHttpCacheRequest
116      */
117     public static <K, V> RemoteCacheRequest<K, V> createRemoveRequest( String cacheName, K key, long requesterId )
118     {
119         RemoteCacheRequest<K, V> request = createRequest(cacheName, RemoteRequestType.REMOVE, requesterId);
120         request.setKey( key );
121 
122         return request;
123     }
124 
125     /**
126      * Creates a GetKeySet Request.
127      * <p>
128      * @param cacheName
129      * @param requesterId
130      * @return RemoteHttpCacheRequest
131      */
132     public static RemoteCacheRequest<String, String> createGetKeySetRequest( String cacheName, long requesterId )
133     {
134         RemoteCacheRequest<String, String> request = createRequest(cacheName, RemoteRequestType.GET_KEYSET, requesterId);
135         request.setKey( cacheName );
136 
137         return request;
138     }
139 
140     /**
141      * Creates a removeAll Request.
142      * <p>
143      * @param cacheName
144      * @param requesterId
145      * @return RemoteHttpCacheRequest
146      */
147     public static <K, V> RemoteCacheRequest<K, V> createRemoveAllRequest( String cacheName, long requesterId )
148     {
149         RemoteCacheRequest<K, V> request = createRequest(cacheName, RemoteRequestType.REMOVE_ALL, requesterId);
150 
151         return request;
152     }
153 
154     /**
155      * Creates a dispose Request.
156      * <p>
157      * @param cacheName
158      * @param requesterId
159      * @return RemoteHttpCacheRequest
160      */
161     public static <K, V> RemoteCacheRequest<K, V> createDisposeRequest( String cacheName, long requesterId )
162     {
163         RemoteCacheRequest<K, V> request = createRequest(cacheName, RemoteRequestType.DISPOSE, requesterId);
164 
165         return request;
166     }
167 
168     /**
169      * Creates an Update Request.
170      * <p>
171      * @param cacheElement
172      * @param requesterId
173      * @return RemoteHttpCacheRequest
174      */
175     public static <K, V> RemoteCacheRequest<K, V> createUpdateRequest( ICacheElement<K, V> cacheElement, long requesterId )
176     {
177         RemoteCacheRequest<K, V> request = createRequest(null, RemoteRequestType.UPDATE, requesterId);
178         if ( cacheElement != null )
179         {
180             request.setCacheName( cacheElement.getCacheName() );
181             request.setCacheElement( cacheElement );
182             request.setKey( cacheElement.getKey() );
183         }
184         else
185         {
186             log.error( "Can't create a proper update request for a null cache element." );
187         }
188 
189         return request;
190     }
191 
192     /**
193      * Creates an alive check Request.
194      * <p>
195      * @param requesterId
196      * @return RemoteHttpCacheRequest
197      */
198     public static <K, V> RemoteCacheRequest<K, V> createAliveCheckRequest( long requesterId )
199     {
200         RemoteCacheRequest<K, V> request = createRequest(null, RemoteRequestType.ALIVE_CHECK, requesterId);
201 
202         return request;
203     }
204 }