001package org.apache.commons.jcs.auxiliary.remote.http.client;
002
003import java.io.IOException;
004
005/*
006 * Licensed to the Apache Software Foundation (ASF) under one
007 * or more contributor license agreements.  See the NOTICE file
008 * distributed with this work for additional information
009 * regarding copyright ownership.  The ASF licenses this file
010 * to you under the Apache License, Version 2.0 (the
011 * "License"); you may not use this file except in compliance
012 * with the License.  You may obtain a copy of the License at
013 *
014 *   http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing,
017 * software distributed under the License is distributed on an
018 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
019 * KIND, either express or implied.  See the License for the
020 * specific language governing permissions and limitations
021 * under the License.
022 */
023
024import org.apache.commons.jcs.auxiliary.remote.AbstractRemoteAuxiliaryCache;
025import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheListener;
026import org.apache.commons.jcs.engine.ZombieCacheServiceNonLocal;
027import org.apache.commons.jcs.engine.behavior.ICacheServiceNonLocal;
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030
031/**
032 * This uses an http client as the service.
033 */
034public class RemoteHttpCache<K, V>
035    extends AbstractRemoteAuxiliaryCache<K, V>
036{
037    /** The logger. */
038    private static final Log log = LogFactory.getLog( RemoteHttpCache.class );
039
040    /** for error notifications */
041    private RemoteHttpCacheMonitor monitor;
042
043    /** Keep the child copy here for the restore process. */
044    private RemoteHttpCacheAttributes remoteHttpCacheAttributes;
045
046    /**
047     * Constructor for the RemoteCache object. This object communicates with a remote cache server.
048     * One of these exists for each region. This also holds a reference to a listener. The same
049     * listener is used for all regions for one remote server. Holding a reference to the listener
050     * allows this object to know the listener id assigned by the remote cache.
051     * <p>
052     * @param remoteHttpCacheAttributes
053     * @param remote
054     * @param listener
055     * @param monitor the cache monitor
056     */
057    public RemoteHttpCache( RemoteHttpCacheAttributes remoteHttpCacheAttributes, ICacheServiceNonLocal<K, V> remote,
058                            IRemoteCacheListener<K, V> listener, RemoteHttpCacheMonitor monitor )
059    {
060        super( remoteHttpCacheAttributes, remote, listener );
061
062        this.remoteHttpCacheAttributes = remoteHttpCacheAttributes;
063        this.monitor = monitor;
064    }
065
066    /**
067     * Nothing right now. This should setup a zombie and initiate recovery.
068     * <p>
069     * @param ex
070     * @param msg
071     * @param eventName
072     * @throws IOException
073     */
074    @Override
075    protected void handleException( Exception ex, String msg, String eventName )
076        throws IOException
077    {
078        // we should not switch if the existing is a zombie.
079        if ( !( getRemoteCacheService() instanceof ZombieCacheServiceNonLocal ) )
080        {
081            String message = "Disabling remote cache due to error: " + msg;
082            logError( cacheName, "", message );
083            log.error( message, ex );
084
085            setRemoteCacheService( new ZombieCacheServiceNonLocal<K, V>( getRemoteCacheAttributes().getZombieQueueMaxSize() ) );
086
087            monitor.notifyError( this );
088        }
089
090        if ( ex instanceof IOException )
091        {
092            throw (IOException) ex;
093        }
094        throw new IOException( ex.getMessage() );
095    }
096
097    /**
098     * @return url of service
099     */
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}