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.net.UnknownHostException;
24
25 import org.apache.commons.jcs3.auxiliary.remote.behavior.IRemoteCacheAttributes;
26 import org.apache.commons.jcs3.auxiliary.remote.behavior.IRemoteCacheListener;
27 import org.apache.commons.jcs3.auxiliary.remote.server.behavior.RemoteType;
28 import org.apache.commons.jcs3.engine.behavior.ICacheElement;
29 import org.apache.commons.jcs3.engine.behavior.ICacheElementSerialized;
30 import org.apache.commons.jcs3.engine.behavior.ICompositeCacheManager;
31 import org.apache.commons.jcs3.engine.behavior.IElementSerializer;
32 import org.apache.commons.jcs3.log.Log;
33 import org.apache.commons.jcs3.log.LogManager;
34 import org.apache.commons.jcs3.utils.net.HostNameUtil;
35 import org.apache.commons.jcs3.utils.serialization.SerializationConversionUtil;
36
37
38 public abstract class AbstractRemoteCacheListener<K, V>
39 implements IRemoteCacheListener<K, V>
40 {
41
42 private static final Log log = LogManager.getLog( AbstractRemoteCacheListener.class );
43
44
45 private static String localHostName;
46
47
48
49
50
51 private final ICompositeCacheManager cacheMgr;
52
53
54 private final IRemoteCacheAttributes irca;
55
56
57 private long listenerId;
58
59
60 private final IElementSerializer elementSerializer;
61
62
63
64
65
66
67
68
69
70
71
72 public AbstractRemoteCacheListener( final IRemoteCacheAttributes irca, final ICompositeCacheManager cacheMgr, final IElementSerializer elementSerializer )
73 {
74 this.irca = irca;
75 this.cacheMgr = cacheMgr;
76 this.elementSerializer = elementSerializer;
77 }
78
79
80
81
82
83
84
85
86
87 @Override
88 public void setListenerId( final long id )
89 throws IOException
90 {
91 listenerId = id;
92 log.info( "set listenerId = [{0}]", id );
93 }
94
95
96
97
98
99
100
101
102 @Override
103 public long getListenerId()
104 throws IOException
105 {
106 log.debug( "get listenerId = [{0}]", listenerId );
107 return listenerId;
108
109 }
110
111
112
113
114
115
116
117 @Override
118 public RemoteType getRemoteType()
119 throws IOException
120 {
121 log.debug( "getRemoteType = [{0}]", irca::getRemoteType);
122 return irca.getRemoteType();
123 }
124
125
126
127
128
129
130
131
132
133
134
135 @Override
136 public void handlePut( ICacheElement<K, V> cb )
137 throws IOException
138 {
139 if ( irca.getRemoveUponRemotePut() )
140 {
141 log.debug( "PUTTING ELEMENT FROM REMOTE, ( invalidating ) " );
142 handleRemove( cb.getCacheName(), cb.getKey() );
143 }
144 else
145 {
146 log.debug( "PUTTING ELEMENT FROM REMOTE, ( updating ) " );
147 log.debug( "cb = {0}", cb );
148
149
150 if ( cb instanceof ICacheElementSerialized )
151 {
152 log.debug( "Object needs to be deserialized." );
153 try
154 {
155 cb = SerializationConversionUtil.getDeSerializedCacheElement(
156 (ICacheElementSerialized<K, V>) cb, this.elementSerializer );
157 log.debug( "Deserialized result = {0}", cb );
158 }
159 catch ( final IOException e )
160 {
161 throw e;
162 }
163 catch ( final ClassNotFoundException e )
164 {
165 log.error( "Received a serialized version of a class that we don't know about.", e );
166 }
167 }
168
169 getCacheManager().<K, V>getCache( cb.getCacheName() ).localUpdate( cb );
170 }
171 }
172
173
174
175
176
177
178
179
180 @Override
181 public void handleRemove( final String cacheName, final K key )
182 throws IOException
183 {
184 log.debug( "handleRemove> cacheName={0}, key={1}", cacheName, key );
185
186 getCacheManager().<K, V>getCache( cacheName ).localRemove( key );
187 }
188
189
190
191
192
193
194
195 @Override
196 public void handleRemoveAll( final String cacheName )
197 throws IOException
198 {
199 log.debug( "handleRemoveAll> cacheName={0}", cacheName );
200
201 getCacheManager().<K, V>getCache( cacheName ).localRemoveAll();
202 }
203
204
205
206
207
208 @Override
209 public void handleDispose( final String cacheName )
210 throws IOException
211 {
212 log.debug( "handleDispose> cacheName={0}", cacheName );
213
214
215
216
217 }
218
219
220
221
222
223 protected ICompositeCacheManager getCacheManager()
224 {
225 return cacheMgr;
226 }
227
228
229
230
231
232
233
234 @Override
235 public synchronized String getLocalHostAddress()
236 throws IOException
237 {
238 if ( localHostName == null )
239 {
240 try
241 {
242 localHostName = HostNameUtil.getLocalHostAddress();
243 }
244 catch ( final UnknownHostException uhe )
245 {
246 localHostName = "unknown";
247 }
248 }
249 return localHostName;
250 }
251
252
253
254
255
256
257 @Override
258 public String toString()
259 {
260 final StringBuilder buf = new StringBuilder();
261 buf.append( "\n AbstractRemoteCacheListener: " )
262 .append( "\n RemoteHost = ").append(irca.getRemoteLocation())
263 .append( "\n ListenerId = ").append(listenerId);
264 return buf.toString();
265 }
266 }