View Javadoc
1   package org.apache.commons.jcs.auxiliary.remote.http.client;
2   
3   import java.io.IOException;
4   
5   /*
6    * Licensed to the Apache Software Foundation (ASF) under one
7    * or more contributor license agreements.  See the NOTICE file
8    * distributed with this work for additional information
9    * regarding copyright ownership.  The ASF licenses this file
10   * to you under the Apache License, Version 2.0 (the
11   * "License"); you may not use this file except in compliance
12   * with the License.  You may obtain a copy of the License at
13   *
14   *   http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing,
17   * software distributed under the License is distributed on an
18   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19   * KIND, either express or implied.  See the License for the
20   * specific language governing permissions and limitations
21   * under the License.
22   */
23  
24  import org.apache.commons.jcs.auxiliary.remote.AbstractRemoteAuxiliaryCache;
25  import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheListener;
26  import org.apache.commons.jcs.engine.ZombieCacheServiceNonLocal;
27  import org.apache.commons.jcs.engine.behavior.ICacheServiceNonLocal;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  /**
32   * This uses an http client as the service.
33   */
34  public class RemoteHttpCache<K, V>
35      extends AbstractRemoteAuxiliaryCache<K, V>
36  {
37      /** The logger. */
38      private static final Log log = LogFactory.getLog( RemoteHttpCache.class );
39  
40      /** for error notifications */
41      private RemoteHttpCacheMonitor monitor;
42  
43      /** Keep the child copy here for the restore process. */
44      private RemoteHttpCacheAttributes remoteHttpCacheAttributes;
45  
46      /**
47       * Constructor for the RemoteCache object. This object communicates with a remote cache server.
48       * One of these exists for each region. This also holds a reference to a listener. The same
49       * listener is used for all regions for one remote server. Holding a reference to the listener
50       * allows this object to know the listener id assigned by the remote cache.
51       * <p>
52       * @param remoteHttpCacheAttributes
53       * @param remote
54       * @param listener
55       * @param monitor the cache monitor
56       */
57      public RemoteHttpCache( RemoteHttpCacheAttributes remoteHttpCacheAttributes, ICacheServiceNonLocal<K, V> remote,
58                              IRemoteCacheListener<K, V> listener, RemoteHttpCacheMonitor monitor )
59      {
60          super( remoteHttpCacheAttributes, remote, listener );
61  
62          this.remoteHttpCacheAttributes = remoteHttpCacheAttributes;
63          this.monitor = monitor;
64      }
65  
66      /**
67       * Nothing right now. This should setup a zombie and initiate recovery.
68       * <p>
69       * @param ex
70       * @param msg
71       * @param eventName
72       * @throws IOException
73       */
74      @Override
75      protected void handleException( Exception ex, String msg, String eventName )
76          throws IOException
77      {
78          // we should not switch if the existing is a zombie.
79          if ( !( getRemoteCacheService() instanceof ZombieCacheServiceNonLocal ) )
80          {
81              String message = "Disabling remote cache due to error: " + msg;
82              logError( cacheName, "", message );
83              log.error( message, ex );
84  
85              setRemoteCacheService( new ZombieCacheServiceNonLocal<K, V>( getRemoteCacheAttributes().getZombieQueueMaxSize() ) );
86  
87              monitor.notifyError( this );
88          }
89  
90          if ( ex instanceof IOException )
91          {
92              throw (IOException) ex;
93          }
94          throw new IOException( ex.getMessage() );
95      }
96  
97      /**
98       * @return url of service
99       */
100     @Override
101     public String getEventLoggingExtraInfo()
102     {
103         return null;
104     }
105 
106     /**
107      * @return the remoteHttpCacheAttributes
108      */
109     public RemoteHttpCacheAttributes getRemoteHttpCacheAttributes()
110     {
111         return remoteHttpCacheAttributes;
112     }
113 }