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.io.IOException;
023import java.rmi.RemoteException;
024import java.rmi.server.UnicastRemoteObject;
025
026import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheAttributes;
027import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheConstants;
028import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager;
029import org.apache.commons.jcs.engine.behavior.IElementSerializer;
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032
033/**
034 * Registered with RemoteCache server. The server updates the local caches via this listener. Each
035 * server assigns a unique listener id for a listener.
036 * <p>
037 * One listener is used per remote cache server. The same listener is used for all the regions that
038 * talk to a particular server.
039 */
040public class RemoteCacheListener<K, V>
041    extends AbstractRemoteCacheListener<K, V>
042    implements IRemoteCacheConstants
043{
044    /** The logger */
045    private static final Log log = LogFactory.getLog( RemoteCacheListener.class );
046
047    /** Has this client been shutdown. */
048    private boolean disposed = false;
049
050    /**
051     * Only need one since it does work for all regions, just reference by multiple region names.
052     * <p>
053     * The constructor exports this object, making it available to receive incoming calls. The
054     * callback port is anonymous unless a local port value was specified in the configuration.
055     * <p>
056     * @param irca cache configuration
057     * @param cacheMgr the cache hub
058     * @param elementSerializer a custom serializer
059     */
060    public RemoteCacheListener( IRemoteCacheAttributes irca, ICompositeCacheManager cacheMgr, IElementSerializer elementSerializer )
061    {
062        super( irca, cacheMgr, elementSerializer );
063
064        // Export this remote object to make it available to receive incoming
065        // calls.
066        try
067        {
068            UnicastRemoteObject.exportObject( this, irca.getLocalPort() );
069        }
070        catch ( RemoteException ex )
071        {
072            log.error( "Problem exporting object.", ex );
073            throw new IllegalStateException( ex.getMessage() );
074        }
075    }
076
077    /**
078     * Deregister itself.
079     * <p>
080     * @throws IOException
081     */
082    @Override
083    public synchronized void dispose()
084        throws IOException
085    {
086        if ( !disposed )
087        {
088            if ( log.isInfoEnabled() )
089            {
090                log.info( "Unexporting listener." );
091            }
092            try
093            {
094                UnicastRemoteObject.unexportObject( this, true );
095            }
096            catch ( RemoteException ex )
097            {
098                log.error( "Problem unexporting the listener.", ex );
099                throw new IllegalStateException( ex.getMessage() );
100            }
101            disposed = true;
102        }
103    }
104
105    /**
106     * For easier debugging.
107     * <p>
108     * @return Basic info on this listener.
109     */
110    @Override
111    public String toString()
112    {
113        StringBuilder buf = new StringBuilder();
114        buf.append( "\n RemoteCacheListener: " );
115        buf.append( super.toString() );
116        return buf.toString();
117    }
118}