View Javadoc
1   package org.apache.commons.jcs.auxiliary.remote;
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.rmi.RemoteException;
24  import java.rmi.server.UnicastRemoteObject;
25  
26  import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheAttributes;
27  import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheConstants;
28  import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager;
29  import org.apache.commons.jcs.engine.behavior.IElementSerializer;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  /**
34   * Registered with RemoteCache server. The server updates the local caches via this listener. Each
35   * server assigns a unique listener id for a listener.
36   * <p>
37   * One listener is used per remote cache server. The same listener is used for all the regions that
38   * talk to a particular server.
39   */
40  public class RemoteCacheListener<K, V>
41      extends AbstractRemoteCacheListener<K, V>
42      implements IRemoteCacheConstants
43  {
44      /** The logger */
45      private static final Log log = LogFactory.getLog( RemoteCacheListener.class );
46  
47      /** Has this client been shutdown. */
48      private boolean disposed = false;
49  
50      /**
51       * Only need one since it does work for all regions, just reference by multiple region names.
52       * <p>
53       * The constructor exports this object, making it available to receive incoming calls. The
54       * callback port is anonymous unless a local port value was specified in the configuration.
55       * <p>
56       * @param irca cache configuration
57       * @param cacheMgr the cache hub
58       * @param elementSerializer a custom serializer
59       */
60      public RemoteCacheListener( IRemoteCacheAttributes irca, ICompositeCacheManager cacheMgr, IElementSerializer elementSerializer )
61      {
62          super( irca, cacheMgr, elementSerializer );
63  
64          // Export this remote object to make it available to receive incoming
65          // calls.
66          try
67          {
68              UnicastRemoteObject.exportObject( this, irca.getLocalPort() );
69          }
70          catch ( RemoteException ex )
71          {
72              log.error( "Problem exporting object.", ex );
73              throw new IllegalStateException( ex.getMessage() );
74          }
75      }
76  
77      /**
78       * Deregister itself.
79       * <p>
80       * @throws IOException
81       */
82      @Override
83      public synchronized void dispose()
84          throws IOException
85      {
86          if ( !disposed )
87          {
88              if ( log.isInfoEnabled() )
89              {
90                  log.info( "Unexporting listener." );
91              }
92              try
93              {
94                  UnicastRemoteObject.unexportObject( this, true );
95              }
96              catch ( RemoteException ex )
97              {
98                  log.error( "Problem unexporting the listener.", ex );
99                  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 }