001package org.apache.commons.jcs3.auxiliary.remote.http.client;
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
022import java.io.IOException;
023
024import org.apache.commons.jcs3.auxiliary.remote.AbstractRemoteAuxiliaryCache;
025import org.apache.commons.jcs3.auxiliary.remote.behavior.IRemoteCacheListener;
026import org.apache.commons.jcs3.engine.ZombieCacheServiceNonLocal;
027import org.apache.commons.jcs3.engine.behavior.ICacheServiceNonLocal;
028import org.apache.commons.jcs3.log.Log;
029import org.apache.commons.jcs3.log.LogManager;
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 = LogManager.getLog( RemoteHttpCache.class );
039
040    /** for error notifications */
041    private final RemoteHttpCacheMonitor monitor;
042
043    /** Keep the child copy here for the restore process. */
044    private final 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( final RemoteHttpCacheAttributes remoteHttpCacheAttributes, final ICacheServiceNonLocal<K, V> remote,
058                            final IRemoteCacheListener<K, V> listener, final 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( final Exception ex, final String msg, final String eventName )
076        throws IOException
077    {
078        // we should not switch if the existing is a zombie.
079        if ( !( getRemoteCacheService() instanceof ZombieCacheServiceNonLocal ) )
080        {
081            final String message = "Disabling remote cache due to error: " + msg;
082            logError( cacheName, "", message );
083            log.error( message, ex );
084
085            setRemoteCacheService( new ZombieCacheServiceNonLocal<>( 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}