1 package org.apache.jcs.auxiliary.lateral.socket.tcp;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStreamReader;
25 import java.io.Serializable;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.Set;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.jcs.auxiliary.lateral.LateralCacheInfo;
34 import org.apache.jcs.auxiliary.lateral.LateralElementDescriptor;
35 import org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheObserver;
36 import org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheService;
37 import org.apache.jcs.auxiliary.lateral.socket.tcp.behavior.ITCPLateralCacheAttributes;
38 import org.apache.jcs.engine.CacheElement;
39 import org.apache.jcs.engine.behavior.ICacheElement;
40 import org.apache.jcs.engine.behavior.ICacheListener;
41
42
43
44
45 public class LateralTCPService
46 implements ILateralCacheService, ILateralCacheObserver
47 {
48
49 private final static Log log = LogFactory.getLog( LateralTCPService.class );
50
51
52 private ITCPLateralCacheAttributes tcpLateralCacheAttributes;
53
54
55 private LateralTCPSender sender;
56
57
58 private long listenerId = LateralCacheInfo.listenerId;
59
60
61
62
63
64
65
66 public LateralTCPService( ITCPLateralCacheAttributes lca )
67 throws IOException
68 {
69 this.setTcpLateralCacheAttributes( lca );
70 try
71 {
72 log.debug( "creating sender, attributes = " + getTcpLateralCacheAttributes() );
73
74 sender = new LateralTCPSender( lca );
75
76 if ( log.isInfoEnabled() )
77 {
78 log.debug( "Created sender to [" + lca.getTcpServer() + "]" );
79 }
80 }
81 catch ( IOException e )
82 {
83
84
85
86 log.error( "Could not create sender to [" + lca.getTcpServer() + "] -- " + e.getMessage() );
87
88 throw e;
89 }
90 }
91
92
93
94
95
96 public void update( ICacheElement item )
97 throws IOException
98 {
99 update( item, getListenerId() );
100 }
101
102
103
104
105
106
107
108
109
110 public void update( ICacheElement item, long requesterId )
111 throws IOException
112 {
113
114 if ( !this.getTcpLateralCacheAttributes().isAllowPut() )
115 {
116
117 if ( !this.getTcpLateralCacheAttributes().isIssueRemoveOnPut() )
118 {
119 return;
120 }
121 }
122
123
124 if ( !this.getTcpLateralCacheAttributes().isIssueRemoveOnPut() )
125 {
126 LateralElementDescriptor led = new LateralElementDescriptor( item );
127 led.requesterId = requesterId;
128 led.command = LateralElementDescriptor.UPDATE;
129 sender.send( led );
130 }
131
132
133 else
134 {
135 if ( log.isDebugEnabled() )
136 {
137 log.debug( "Issuing a remove for a put" );
138 }
139
140 CacheElement ce = new CacheElement( item.getCacheName(), item.getKey(), null );
141 LateralElementDescriptor led = new LateralElementDescriptor( ce );
142 led.requesterId = requesterId;
143 led.command = LateralElementDescriptor.REMOVE;
144 led.valHashCode = item.getVal().hashCode();
145 sender.send( led );
146 }
147 }
148
149
150
151
152
153
154
155 public void remove( String cacheName, Serializable key )
156 throws IOException
157 {
158 remove( cacheName, key, getListenerId() );
159 }
160
161
162
163
164
165
166
167 public void remove( String cacheName, Serializable key, long requesterId )
168 throws IOException
169 {
170 CacheElement ce = new CacheElement( cacheName, key, null );
171 LateralElementDescriptor led = new LateralElementDescriptor( ce );
172 led.requesterId = requesterId;
173 led.command = LateralElementDescriptor.REMOVE;
174 sender.send( led );
175 }
176
177
178
179
180
181
182 public void release()
183 throws IOException
184 {
185
186 }
187
188
189
190
191
192
193
194 public void dispose( String cacheName )
195 throws IOException
196 {
197 sender.dispose( cacheName );
198 }
199
200
201
202
203
204
205
206
207 public Serializable get( String key )
208 throws IOException
209 {
210 if ( log.isDebugEnabled() )
211 {
212 log.debug( "balking at get for key [" + key + "]" );
213 }
214
215
216 return null;
217
218 }
219
220
221
222
223
224
225
226 public ICacheElement get( String cacheName, Serializable key )
227 throws IOException
228 {
229 return get( cacheName, key, getListenerId() );
230 }
231
232
233
234
235
236
237
238
239
240
241 public ICacheElement get( String cacheName, Serializable key, long requesterId )
242 throws IOException
243 {
244
245 if ( this.getTcpLateralCacheAttributes().isAllowGet() )
246 {
247 CacheElement ce = new CacheElement( cacheName, key, null );
248 LateralElementDescriptor led = new LateralElementDescriptor( ce );
249
250 led.command = LateralElementDescriptor.GET;
251 Object response = sender.sendAndReceive( led );
252 if ( response != null )
253 {
254 return (ICacheElement) response;
255 }
256 return null;
257 }
258 else
259 {
260
261 return null;
262 }
263 }
264
265
266
267
268
269
270
271
272
273
274 public Map<Serializable, ICacheElement> getMatching( String cacheName, String pattern )
275 throws IOException
276 {
277 return getMatching( cacheName, pattern, getListenerId() );
278 }
279
280
281
282
283
284
285
286
287
288
289
290 @SuppressWarnings("unchecked")
291 public Map<Serializable, ICacheElement> getMatching( String cacheName, String pattern, long requesterId )
292 throws IOException
293 {
294
295 if ( this.getTcpLateralCacheAttributes().isAllowGet() )
296 {
297 CacheElement ce = new CacheElement( cacheName, pattern, null );
298 LateralElementDescriptor led = new LateralElementDescriptor( ce );
299
300 led.command = LateralElementDescriptor.GET_MATCHING;
301
302 Object response = sender.sendAndReceive( led );
303 if ( response != null )
304 {
305 return (Map<Serializable, ICacheElement>) response;
306 }
307 return Collections.emptyMap();
308 }
309 else
310 {
311
312 return null;
313 }
314 }
315
316
317
318
319
320
321
322
323
324
325 public Map<Serializable, ICacheElement> getMultiple( String cacheName, Set<Serializable> keys )
326 throws IOException
327 {
328 return getMultiple( cacheName, keys, getListenerId() );
329 }
330
331
332
333
334
335
336
337
338
339
340
341
342
343 public Map<Serializable, ICacheElement> getMultiple( String cacheName, Set<Serializable> keys, long requesterId )
344 throws IOException
345 {
346 Map<Serializable, ICacheElement> elements = new HashMap<Serializable, ICacheElement>();
347
348 if ( keys != null && !keys.isEmpty() )
349 {
350 for (Serializable key : keys)
351 {
352 ICacheElement element = get( cacheName, key );
353
354 if ( element != null )
355 {
356 elements.put( key, element );
357 }
358 }
359 }
360 return elements;
361 }
362
363
364
365
366
367
368
369
370 public Set<Serializable> getGroupKeys( String cacheName, String group )
371 {
372 throw new UnsupportedOperationException( "Groups not implemented." );
373
374 }
375
376
377
378
379
380 public void removeAll( String cacheName )
381 throws IOException
382 {
383 removeAll( cacheName, getListenerId() );
384 }
385
386
387
388
389
390
391 public void removeAll( String cacheName, long requesterId )
392 throws IOException
393 {
394 CacheElement ce = new CacheElement( cacheName, "ALL", null );
395 LateralElementDescriptor led = new LateralElementDescriptor( ce );
396 led.requesterId = requesterId;
397 led.command = LateralElementDescriptor.REMOVEALL;
398 sender.send( led );
399 }
400
401
402
403
404 public static void main( String args[] )
405 {
406 try
407 {
408 LateralTCPSender sender = new LateralTCPSender( new TCPLateralCacheAttributes() );
409
410
411 boolean notDone = true;
412 String message = null;
413
414 BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
415
416 while ( notDone )
417 {
418 System.out.println( "enter mesage:" );
419 message = br.readLine();
420
421 if (message == null)
422 {
423 notDone = false;
424 continue;
425 }
426
427 CacheElement ce = new CacheElement( "test", "test", message );
428 LateralElementDescriptor led = new LateralElementDescriptor( ce );
429 sender.send( led );
430 }
431 }
432 catch ( IOException e )
433 {
434 System.out.println( e.toString() );
435 }
436 }
437
438
439
440
441
442
443
444
445
446
447 public void addCacheListener( String cacheName, ICacheListener obj )
448 throws IOException
449 {
450
451 }
452
453
454
455
456
457 public void addCacheListener( ICacheListener obj )
458 throws IOException
459 {
460
461 }
462
463
464
465
466
467
468 public void removeCacheListener( String cacheName, ICacheListener obj )
469 throws IOException
470 {
471
472 }
473
474
475
476
477
478 public void removeCacheListener( ICacheListener obj )
479 throws IOException
480 {
481
482 }
483
484
485
486
487 protected void setListenerId( long listernId )
488 {
489 this.listenerId = listernId;
490 }
491
492
493
494
495 protected long getListenerId()
496 {
497 return listenerId;
498 }
499
500
501
502
503 public void setTcpLateralCacheAttributes( ITCPLateralCacheAttributes tcpLateralCacheAttributes )
504 {
505 this.tcpLateralCacheAttributes = tcpLateralCacheAttributes;
506 }
507
508
509
510
511 public ITCPLateralCacheAttributes getTcpLateralCacheAttributes()
512 {
513 return tcpLateralCacheAttributes;
514 }
515 }