001 package org.apache.jcs.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
022 import java.io.IOException;
023 import java.io.Serializable;
024 import java.util.HashMap;
025 import java.util.Map;
026 import java.util.Set;
027
028 import org.apache.commons.logging.Log;
029 import org.apache.commons.logging.LogFactory;
030 import org.apache.jcs.auxiliary.remote.value.RemoteCacheRequest;
031 import org.apache.jcs.auxiliary.remote.value.RemoteCacheResponse;
032 import org.apache.jcs.engine.behavior.ICacheElement;
033 import org.apache.jcs.engine.behavior.ICacheServiceNonLocal;
034 import org.apache.jcs.engine.control.CompositeCacheManager;
035
036 /**
037 * The Servlet deserializes the request object. The request object is passed to the processor. The
038 * processor then calls the service which does the work of talking to the cache.
039 * <p>
040 * This is essentially an adaptor on top of the service.
041 */
042 public class RemoteCacheServiceAdaptor<K extends Serializable, V extends Serializable>
043 {
044 /** The Logger. */
045 private final static Log log = LogFactory.getLog( RemoteCacheServiceAdaptor.class );
046
047 /** The service that does the work. */
048 private ICacheServiceNonLocal<K, V> remoteCacheService;
049
050 /** This is for testing without the factory. */
051 protected RemoteCacheServiceAdaptor()
052 {
053 // for testing.
054 }
055
056 /**
057 * Create a process with a cache manager.
058 * <p>
059 * @param cacheManager
060 */
061 public RemoteCacheServiceAdaptor( CompositeCacheManager cacheManager )
062 {
063 ICacheServiceNonLocal<K, V> rcs = RemoteHttpCacheSeviceFactory.createRemoteHttpCacheService( cacheManager );
064 setRemoteCacheService( rcs );
065 }
066
067 /**
068 * Processes the request. It will call the appropriate method on the service
069 * <p>
070 * @param request
071 * @return RemoteHttpCacheResponse, never null
072 */
073 @SuppressWarnings( "unchecked" ) // need to cast to correct return type
074 public <T> RemoteCacheResponse<T> processRequest( RemoteCacheRequest<K, V> request )
075 {
076 RemoteCacheResponse<Object> response = new RemoteCacheResponse<Object>();
077
078 if ( request == null )
079 {
080 String message = "The request is null. Cannot process";
081 log.warn( message );
082 response.setSuccess( false );
083 response.setErrorMessage( message );
084 }
085 else
086 {
087 try
088 {
089 switch ( request.getRequestType() )
090 {
091 case GET:
092 ICacheElement<K, V> element = getRemoteCacheService().get( request.getCacheName(), request.getKey(),
093 request.getRequesterId() );
094 response.setPayload(element);
095 break;
096 case GET_MULTIPLE:
097 Map<K, ICacheElement<K, V>> elementMap = getRemoteCacheService().getMultiple( request.getCacheName(),
098 request.getKeySet(),
099 request.getRequesterId() );
100 if ( elementMap != null )
101 {
102 Map<K, ICacheElement<K, V>> map = new HashMap<K, ICacheElement<K,V>>();
103 map.putAll(elementMap);
104 response.setPayload(map);
105 }
106 break;
107 case GET_MATCHING:
108 Map<K, ICacheElement<K, V>> elementMapMatching = getRemoteCacheService().getMatching( request.getCacheName(),
109 request.getPattern(),
110 request.getRequesterId() );
111 if ( elementMapMatching != null )
112 {
113 Map<K, ICacheElement<K, V>> map = new HashMap<K, ICacheElement<K,V>>();
114 map.putAll(elementMapMatching);
115 response.setPayload(map);
116 }
117 break;
118 case REMOVE:
119 getRemoteCacheService().remove( request.getCacheName(), request.getKey(),
120 request.getRequesterId() );
121 break;
122 case REMOVE_ALL:
123 getRemoteCacheService().removeAll( request.getCacheName(), request.getRequesterId() );
124 break;
125 case UPDATE:
126 getRemoteCacheService().update( request.getCacheElement(), request.getRequesterId() );
127 break;
128 case ALIVE_CHECK:
129 response.setSuccess( true );
130 break;
131 case DISPOSE:
132 response.setSuccess( true );
133 // DO NOTHING
134 break;
135 case GET_GROUP_KEYS:
136 Set<K> groupKeys = getRemoteCacheService().getGroupKeys( request.getCacheName(),
137 request.getKey() + "" );
138 response.setPayload( groupKeys );
139 break;
140 case GET_GROUP_NAMES:
141 Set<String> groupNames = getRemoteCacheService().getGroupNames( request.getCacheName() );
142 response.setPayload( groupNames );
143 break;
144 default:
145 String message = "Unknown event type. Cannot process " + request;
146 log.warn( message );
147 response.setSuccess( false );
148 response.setErrorMessage( message );
149 break;
150 }
151 }
152 catch ( IOException e )
153 {
154 String message = "Problem processing request. " + request + " Error: " + e.getMessage();
155 log.error( message, e );
156 response.setSuccess( false );
157 response.setErrorMessage( message );
158 }
159 }
160
161 return (RemoteCacheResponse<T>)response;
162 }
163
164 /**
165 * @param remoteHttpCacheService the remoteHttpCacheService to set
166 */
167 public void setRemoteCacheService( ICacheServiceNonLocal<K, V> remoteHttpCacheService )
168 {
169 this.remoteCacheService = remoteHttpCacheService;
170 }
171
172 /**
173 * @return the remoteHttpCacheService
174 */
175 public ICacheServiceNonLocal<K, V> getRemoteCacheService()
176 {
177 return remoteCacheService;
178 }
179 }