001package org.apache.commons.jcs.auxiliary.remote.util;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Set;
023
024import org.apache.commons.jcs.auxiliary.remote.value.RemoteCacheRequest;
025import org.apache.commons.jcs.auxiliary.remote.value.RemoteRequestType;
026import org.apache.commons.jcs.engine.behavior.ICacheElement;
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029
030/**
031 * This creates request objects. You could write your own client and use the objects from this
032 * factory.
033 */
034public class RemoteCacheRequestFactory
035{
036    /** The Logger. */
037    private static final Log log = LogFactory.getLog( RemoteCacheRequestFactory.class );
038
039    /**
040     * Create generic request
041     * @param cacheName cache name
042     * @param requestType type of request
043     * @param requesterId id of requester
044     * @return the request
045     */
046    private static <K, V> RemoteCacheRequest<K, V> createRequest(String cacheName, RemoteRequestType requestType, long requesterId)
047    {
048        RemoteCacheRequest<K, V> request = new RemoteCacheRequest<K, V>();
049        request.setCacheName( cacheName );
050        request.setRequestType( requestType );
051        request.setRequesterId( requesterId );
052
053        if ( log.isDebugEnabled() )
054        {
055            log.debug( "Created: " + request );
056        }
057
058        return request;
059    }
060
061    /**
062     * Creates a get Request.
063     * <p>
064     * @param cacheName
065     * @param key
066     * @param requesterId
067     * @return RemoteHttpCacheRequest
068     */
069    public static <K, V> RemoteCacheRequest<K, V> createGetRequest( String cacheName, K key, long requesterId )
070    {
071        RemoteCacheRequest<K, V> request = createRequest(cacheName, RemoteRequestType.GET, requesterId);
072        request.setKey( key );
073
074        return request;
075    }
076
077    /**
078     * Creates a getMatching Request.
079     * <p>
080     * @param cacheName
081     * @param pattern
082     * @param requesterId
083     * @return RemoteHttpCacheRequest
084     */
085    public static <K, V> RemoteCacheRequest<K, V> createGetMatchingRequest( String cacheName, String pattern, long requesterId )
086    {
087        RemoteCacheRequest<K, V> request = createRequest(cacheName, RemoteRequestType.GET_MATCHING, requesterId);
088        request.setPattern( pattern );
089
090        return request;
091    }
092
093    /**
094     * Creates a getMultiple Request.
095     * <p>
096     * @param cacheName
097     * @param keys
098     * @param requesterId
099     * @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}