001package org.apache.commons.jcs.auxiliary.remote;
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.util.List;
023
024import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheAttributes;
025import org.apache.commons.jcs.auxiliary.remote.server.behavior.RemoteType;
026import org.apache.commons.jcs.engine.CacheStatus;
027import org.apache.commons.jcs.engine.behavior.ICache;
028import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager;
029import org.apache.commons.jcs.engine.behavior.IElementSerializer;
030import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger;
031import org.apache.commons.logging.Log;
032import org.apache.commons.logging.LogFactory;
033
034/**
035 * Used to provide access to multiple services under nowait protection. Factory should construct
036 * NoWaitFacade to give to the composite cache out of caches it constructs from the varies manager
037 * to lateral services.
038 * <p>
039 * Typically, we only connect to one remote server per facade. We use a list of one
040 * RemoteCacheNoWait.
041 */
042public class RemoteCacheNoWaitFacade<K, V>
043    extends AbstractRemoteCacheNoWaitFacade<K, V>
044{
045    /** log instance */
046    private static final Log log = LogFactory.getLog( RemoteCacheNoWaitFacade.class );
047
048    /** Provide factory instance to RemoteCacheFailoverRunner */
049    private final RemoteCacheFactory cacheFactory;
050
051    /**
052     * Constructs with the given remote cache, and fires events to any listeners.
053     * <p>
054     * @param noWaits
055     * @param rca
056     * @param cacheEventLogger
057     * @param elementSerializer
058     * @param cacheFactory
059     */
060    public RemoteCacheNoWaitFacade( List<RemoteCacheNoWait<K,V>> noWaits,
061                                    IRemoteCacheAttributes rca,
062                                    ICacheEventLogger cacheEventLogger,
063                                    IElementSerializer elementSerializer,
064                                    RemoteCacheFactory cacheFactory)
065    {
066        super( noWaits, rca, cacheEventLogger, elementSerializer );
067        this.cacheFactory = cacheFactory;
068    }
069
070    /**
071     * Constructs with the given remote cache, and fires events to any listeners.
072     * <p>
073     * @param noWaits
074     * @param rca
075     * @param cacheMgr
076     * @param cacheEventLogger
077     * @param elementSerializer
078     * @param cacheFactory
079     * @deprecated Unused parameter cacheMgr scheduled for removal
080     */
081    @Deprecated
082    public RemoteCacheNoWaitFacade( List<ICache<K, V>> noWaits,
083                                    RemoteCacheAttributes rca,
084                                    ICompositeCacheManager cacheMgr,
085                                    ICacheEventLogger cacheEventLogger,
086                                    IElementSerializer elementSerializer,
087                                    RemoteCacheFactory cacheFactory)
088    {
089        super( noWaits, rca, cacheMgr, cacheEventLogger, elementSerializer );
090        this.cacheFactory = cacheFactory;
091    }
092
093    /**
094     * Begin the failover process if this is a local cache. Clustered remote caches do not failover.
095     * <p>
096     * @param rcnw The no wait in error.
097     */
098    @Override
099    protected void failover( RemoteCacheNoWait<K, V> rcnw )
100    {
101        if ( log.isDebugEnabled() )
102        {
103            log.debug( "in failover for " + rcnw );
104        }
105
106        if ( getAuxiliaryCacheAttributes().getRemoteType() == RemoteType.LOCAL )
107        {
108            if ( rcnw.getStatus() == CacheStatus.ERROR )
109            {
110                // start failover, primary recovery process
111                RemoteCacheFailoverRunner<K, V> runner = new RemoteCacheFailoverRunner<K, V>( this, this.cacheFactory );
112                runner.setDaemon( true );
113                runner.start();
114                runner.notifyError();
115
116                if ( getCacheEventLogger() != null )
117                {
118                    getCacheEventLogger().logApplicationEvent( "RemoteCacheNoWaitFacade", "InitiatedFailover",
119                                                               rcnw + " was in error." );
120                }
121            }
122            else
123            {
124                if ( log.isInfoEnabled() )
125                {
126                    log.info( "The noWait is not in error" );
127                }
128            }
129        }
130    }
131
132}