1 package org.apache.commons.jcs3.auxiliary.remote;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
36
37
38
39
40
41 public class RemoteCacheListener<K, V>
42 extends AbstractRemoteCacheListener<K, V>
43 implements IRemoteCacheConstants
44 {
45
46 private static final Log log = LogManager.getLog( RemoteCacheListener.class );
47
48
49 private AtomicBoolean disposed;
50
51
52
53
54
55
56
57
58
59
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
69
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
83
84
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
107
108
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 }