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