View Javadoc

1   package org.apache.jcs.auxiliary.remote.http.server;
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.io.IOException;
23  import java.io.Serializable;
24  import java.util.HashMap;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.jcs.auxiliary.remote.value.RemoteCacheRequest;
31  import org.apache.jcs.auxiliary.remote.value.RemoteCacheResponse;
32  import org.apache.jcs.engine.behavior.ICacheElement;
33  import org.apache.jcs.engine.behavior.ICacheServiceNonLocal;
34  import org.apache.jcs.engine.control.CompositeCacheManager;
35  
36  /**
37   * The Servlet deserializes the request object. The request object is passed to the processor. The
38   * processor then calls the service which does the work of talking to the cache.
39   * <p>
40   * This is essentially an adaptor on top of the service.
41   */
42  public class RemoteCacheServiceAdaptor<K extends Serializable, V extends Serializable>
43  {
44      /** The Logger. */
45      private final static Log log = LogFactory.getLog( RemoteCacheServiceAdaptor.class );
46  
47      /** The service that does the work. */
48      private ICacheServiceNonLocal<K, V> remoteCacheService;
49  
50      /** This is for testing without the factory. */
51      protected RemoteCacheServiceAdaptor()
52      {
53          // for testing.
54      }
55  
56      /**
57       * Create a process with a cache manager.
58       * <p>
59       * @param cacheManager
60       */
61      public RemoteCacheServiceAdaptor( CompositeCacheManager cacheManager )
62      {
63          ICacheServiceNonLocal<K, V> rcs = RemoteHttpCacheSeviceFactory.createRemoteHttpCacheService( cacheManager );
64          setRemoteCacheService( rcs );
65      }
66  
67      /**
68       * Processes the request. It will call the appropriate method on the service
69       * <p>
70       * @param request
71       * @return RemoteHttpCacheResponse, never null
72       */
73      @SuppressWarnings( "unchecked" ) // need to cast to correct return type
74      public <T> RemoteCacheResponse<T> processRequest( RemoteCacheRequest<K, V> request )
75      {
76          RemoteCacheResponse<Object> response = new RemoteCacheResponse<Object>();
77  
78          if ( request == null )
79          {
80              String message = "The request is null. Cannot process";
81              log.warn( message );
82              response.setSuccess( false );
83              response.setErrorMessage( message );
84          }
85          else
86          {
87              try
88              {
89                  switch ( request.getRequestType() )
90                  {
91                      case GET:
92                          ICacheElement<K, V> element = getRemoteCacheService().get( request.getCacheName(), request.getKey(),
93                                                                               request.getRequesterId() );
94                          response.setPayload(element);
95                          break;
96                      case GET_MULTIPLE:
97                          Map<K, ICacheElement<K, V>> elementMap = getRemoteCacheService().getMultiple( request.getCacheName(),
98                                                                                request.getKeySet(),
99                                                                                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 }