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