001package org.apache.commons.jcs3.auxiliary.remote.http.server;
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.io.IOException;
023import java.util.Map;
024import java.util.Set;
025
026import org.apache.commons.jcs3.engine.behavior.ICacheElement;
027import org.apache.commons.jcs3.engine.behavior.ICompositeCacheManager;
028import org.apache.commons.jcs3.engine.control.CompositeCache;
029import org.apache.commons.jcs3.engine.logging.behavior.ICacheEventLogger;
030
031/**
032 * This does the work. It's called by the processor. The base class wraps the processing calls in
033 * event logs, if an event logger is present.
034 * <p>
035 * For now we assume that all clients are non-cluster clients. And listener notification is not
036 * supported.
037 */
038public class RemoteHttpCacheService<K, V>
039    extends AbstractRemoteCacheService<K, V>
040{
041    /** The name used in the event logs. */
042    private static final String EVENT_LOG_SOURCE_NAME = "RemoteHttpCacheServer";
043
044    /** The configuration */
045    private final RemoteHttpCacheServerAttributes remoteHttpCacheServerAttributes;
046
047    /**
048     * Create a process with a cache manager.
049     * <p>
050     * @param cacheManager
051     * @param remoteHttpCacheServerAttributes
052     * @param cacheEventLogger
053     */
054    public RemoteHttpCacheService( final ICompositeCacheManager cacheManager,
055                                   final RemoteHttpCacheServerAttributes remoteHttpCacheServerAttributes,
056                                   final ICacheEventLogger cacheEventLogger )
057    {
058        super( cacheManager, cacheEventLogger );
059        setEventLogSourceName( EVENT_LOG_SOURCE_NAME );
060        this.remoteHttpCacheServerAttributes = remoteHttpCacheServerAttributes;
061    }
062
063    /**
064     * Processes a get request.
065     * <p>
066     * If isAllowClusterGet is enabled we will treat this as a normal request or non-remote origins.
067     * <p>
068     * @param cacheName
069     * @param key
070     * @param requesterId
071     * @return ICacheElement
072     * @throws IOException
073     */
074    @Override
075    public ICacheElement<K, V> processGet( final String cacheName, final K key, final long requesterId )
076        throws IOException
077    {
078        final CompositeCache<K, V> cache = getCacheManager().getCache( cacheName );
079
080        final boolean keepLocal = !remoteHttpCacheServerAttributes.isAllowClusterGet();
081        if ( keepLocal )
082        {
083            return cache.localGet( key );
084        }
085        return cache.get( key );
086    }
087
088    /**
089     * Processes a get request.
090     * <p>
091     * If isAllowClusterGet is enabled we will treat this as a normal request of non-remote
092     * origination.
093     * <p>
094     * @param cacheName
095     * @param keys
096     * @param requesterId
097     * @return Map
098     * @throws IOException
099     */
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}