View Javadoc

1   package org.apache.jcs.auxiliary.remote.http.client;
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  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.jcs.auxiliary.remote.AbstractRemoteAuxiliaryCache;
28  import org.apache.jcs.auxiliary.remote.behavior.IRemoteCacheListener;
29  import org.apache.jcs.engine.ZombieCacheServiceNonLocal;
30  import org.apache.jcs.engine.behavior.ICacheServiceNonLocal;
31  
32  /**
33   * This uses an http client as the service.
34   */
35  public class RemoteHttpCache<K extends Serializable, V extends Serializable>
36      extends AbstractRemoteAuxiliaryCache<K, V>
37  {
38      /** Don't change. */
39      private static final long serialVersionUID = -5329231850422826461L;
40  
41      /** The logger. */
42      private final static Log log = LogFactory.getLog( RemoteHttpCache.class );
43  
44      /** Keep the child copy here for the restore process. */
45      private RemoteHttpCacheAttributes remoteHttpCacheAttributes;
46  
47      /**
48       * Constructor for the RemoteCache object. This object communicates with a remote cache server.
49       * One of these exists for each region. This also holds a reference to a listener. The same
50       * listener is used for all regions for one remote server. Holding a reference to the listener
51       * allows this object to know the listener id assigned by the remote cache.
52       * <p>
53       * @param remoteHttpCacheAttributes
54       * @param remote
55       * @param listener
56       */
57      public RemoteHttpCache( RemoteHttpCacheAttributes remoteHttpCacheAttributes, ICacheServiceNonLocal<K, V> remote,
58                              IRemoteCacheListener<K, V> listener )
59      {
60          super( remoteHttpCacheAttributes, remote, listener );
61  
62          setRemoteHttpCacheAttributes( remoteHttpCacheAttributes );
63      }
64  
65      /**
66       * Nothing right now. This should setup a zombie and initiate recovery.
67       * <p>
68       * @param ex
69       * @param msg
70       * @param eventName
71       * @throws IOException
72       */
73      @Override
74      protected void handleException( Exception ex, String msg, String eventName )
75          throws IOException
76      {
77          // we should not switch if the existing is a zombie.
78          if ( !( getRemoteCacheService() instanceof ZombieCacheServiceNonLocal ) )
79          {
80              String message = "Disabling remote cache due to error: " + msg;
81              logError( cacheName, "", message );
82              log.error( message, ex );
83  
84              setRemoteCacheService( new ZombieCacheServiceNonLocal<K, V>( getRemoteCacheAttributes().getZombieQueueMaxSize() ) );
85  
86              RemoteHttpCacheMonitor.getInstance().notifyError( this );
87          }
88  
89          if ( ex instanceof IOException )
90          {
91              throw (IOException) ex;
92          }
93          throw new IOException( ex.getMessage() );
94      }
95  
96      /**
97       * @return url of service
98       */
99      @Override
100     public String getEventLoggingExtraInfo()
101     {
102         return null;
103     }
104 
105     /**
106      * @param remoteHttpCacheAttributes the remoteHttpCacheAttributes to set
107      */
108     public void setRemoteHttpCacheAttributes( RemoteHttpCacheAttributes remoteHttpCacheAttributes )
109     {
110         this.remoteHttpCacheAttributes = remoteHttpCacheAttributes;
111     }
112 
113     /**
114      * @return the remoteHttpCacheAttributes
115      */
116     public RemoteHttpCacheAttributes getRemoteHttpCacheAttributes()
117     {
118         return remoteHttpCacheAttributes;
119     }
120 }