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.Set;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.jcs.auxiliary.lateral.LateralCacheInfo;
31 import org.apache.jcs.auxiliary.lateral.LateralElementDescriptor;
32 import org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheObserver;
33 import org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheService;
34 import org.apache.jcs.auxiliary.lateral.socket.tcp.behavior.ITCPLateralCacheAttributes;
35 import org.apache.jcs.engine.CacheElement;
36 import org.apache.jcs.engine.behavior.ICacheElement;
37 import org.apache.jcs.engine.behavior.ICacheListener;
38
39 /***
40 * A lateral cache service implementation. Does not implement getGroupKey
41 * <p>
42 * @version $Id: LateralTCPService.java 536904 2007-05-10 16:03:42Z tv $
43 */
44 public class LateralTCPService
45 implements ILateralCacheService, ILateralCacheObserver
46 {
47 private final static Log log = LogFactory.getLog( LateralTCPService.class );
48
49 private ITCPLateralCacheAttributes tcpLateralCacheAttributes;
50
51 private LateralTCPSender sender;
52
53 /***
54 * use the vmid by default
55 */
56 private long listenerId = LateralCacheInfo.listenerId;
57
58 /***
59 * Constructor for the LateralTCPService object
60 * <p>
61 * @param lca ITCPLateralCacheAttributes
62 * @exception IOException
63 */
64 public LateralTCPService( ITCPLateralCacheAttributes lca )
65 throws IOException
66 {
67 this.setTcpLateralCacheAttributes( lca );
68 try
69 {
70 log.debug( "creating sender, attributes = " + getTcpLateralCacheAttributes() );
71
72 sender = new LateralTCPSender( lca );
73
74 if ( log.isInfoEnabled() )
75 {
76 log.debug( "Created sender to [" + lca.getTcpServer() + "]" );
77 }
78 }
79 catch ( IOException e )
80 {
81
82
83
84 log.error( "Could not create sender to [" + lca.getTcpServer() + "] -- " + e.getMessage() );
85
86 throw e;
87 }
88 }
89
90 /***
91 * @param item
92 * @throws IOException
93 */
94 public void update( ICacheElement item )
95 throws IOException
96 {
97 update( item, getListenerId() );
98 }
99
100 /***
101 * If put is allowed, we will issue a put. If issue put on remove is configured, we will issue a
102 * remove. Either way, we create a lateral element descriptor, which is essentially a JCS TCP
103 * packet. It describes what operation the receiver should take when it gets the packet.
104 * <p>
105 * @see org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheService#update(org.apache.jcs.engine.behavior.ICacheElement,
106 * long)
107 */
108 public void update( ICacheElement item, long requesterId )
109 throws IOException
110 {
111
112 if ( !this.getTcpLateralCacheAttributes().isAllowPut() )
113 {
114
115 if ( !this.getTcpLateralCacheAttributes().isIssueRemoveOnPut() )
116 {
117 return;
118 }
119 }
120
121
122 if ( !this.getTcpLateralCacheAttributes().isIssueRemoveOnPut() )
123 {
124 LateralElementDescriptor led = new LateralElementDescriptor( item );
125 led.requesterId = requesterId;
126 led.command = LateralElementDescriptor.UPDATE;
127 sender.send( led );
128 }
129
130
131 else
132 {
133 if ( log.isDebugEnabled() )
134 {
135 log.debug( "Issuing a remove for a put" );
136 }
137
138 CacheElement ce = new CacheElement( item.getCacheName(), item.getKey(), null );
139 LateralElementDescriptor led = new LateralElementDescriptor( ce );
140 led.requesterId = requesterId;
141 led.command = LateralElementDescriptor.REMOVE;
142 led.valHashCode = item.getVal().hashCode();
143 sender.send( led );
144 }
145 }
146
147 /***
148 * Uses the default listener id and calls the next remove method.
149 * <p>
150 * @see org.apache.jcs.engine.behavior.ICacheService#remove(java.lang.String,
151 * java.io.Serializable)
152 */
153 public void remove( String cacheName, Serializable key )
154 throws IOException
155 {
156 remove( cacheName, key, getListenerId() );
157 }
158
159 /***
160 * Wraps the key in a LateralElementDescriptor.
161 * <p>
162 * @see org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheService#remove(java.lang.String,
163 * java.io.Serializable, long)
164 */
165 public void remove( String cacheName, Serializable key, long requesterId )
166 throws IOException
167 {
168 CacheElement ce = new CacheElement( cacheName, key, null );
169 LateralElementDescriptor led = new LateralElementDescriptor( ce );
170 led.requesterId = requesterId;
171 led.command = LateralElementDescriptor.REMOVE;
172 sender.send( led );
173 }
174
175
176
177
178
179 public void release()
180 throws IOException
181 {
182
183 }
184
185 /***
186 * Will close the connection.
187 * <p>
188 * @param cacheName
189 * @throws IOException
190 */
191 public void dispose( String cacheName )
192 throws IOException
193 {
194 sender.dispose( cacheName );
195 }
196
197 /***
198 * The service does not get via this method, so this return null.
199 * <p>
200 * @param key
201 * @return always null.
202 * @throws IOException
203 */
204 public Serializable get( String key )
205 throws IOException
206 {
207 if ( log.isDebugEnabled() )
208 {
209 log.debug( "balking at get for key [" + key + "]" );
210 }
211
212
213 return null;
214
215 }
216
217
218
219
220
221 public ICacheElement get( String cacheName, Serializable key )
222 throws IOException
223 {
224
225 if ( this.getTcpLateralCacheAttributes().isAllowGet() )
226 {
227 CacheElement ce = new CacheElement( cacheName, key, null );
228 LateralElementDescriptor led = new LateralElementDescriptor( ce );
229
230 led.command = LateralElementDescriptor.GET;
231 return sender.sendAndReceive( led );
232 }
233 else
234 {
235
236 return null;
237 }
238 }
239
240 /***
241 * Gets the set of keys of objects currently in the group throws UnsupportedOperationException
242 * <p>
243 * @param cacheName
244 * @param group
245 * @return Set
246 */
247 public Set getGroupKeys( String cacheName, String group )
248 {
249 if ( true )
250 {
251 throw new UnsupportedOperationException( "Groups not implemented." );
252 }
253 return null;
254 }
255
256
257
258
259
260 public void removeAll( String cacheName )
261 throws IOException
262 {
263 removeAll( cacheName, getListenerId() );
264 }
265
266
267
268
269
270
271 public void removeAll( String cacheName, long requesterId )
272 throws IOException
273 {
274 CacheElement ce = new CacheElement( cacheName, "ALL", null );
275 LateralElementDescriptor led = new LateralElementDescriptor( ce );
276 led.requesterId = requesterId;
277 led.command = LateralElementDescriptor.REMOVEALL;
278 sender.send( led );
279 }
280
281 /***
282 * @param args
283 */
284 public static void main( String args[] )
285 {
286 try
287 {
288 LateralTCPSender sender = new LateralTCPSender( new TCPLateralCacheAttributes() );
289
290
291 boolean notDone = true;
292 String message = null;
293
294 BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
295
296 while ( notDone )
297 {
298 System.out.println( "enter mesage:" );
299 message = br.readLine();
300 CacheElement ce = new CacheElement( "test", "test", message );
301 LateralElementDescriptor led = new LateralElementDescriptor( ce );
302 sender.send( led );
303 }
304 }
305 catch ( Exception e )
306 {
307 System.out.println( e.toString() );
308 }
309 }
310
311
312
313
314
315
316
317
318
319
320 public void addCacheListener( String cacheName, ICacheListener obj )
321 throws IOException
322 {
323
324 }
325
326
327
328
329
330 public void addCacheListener( ICacheListener obj )
331 throws IOException
332 {
333
334 }
335
336
337
338
339
340
341 public void removeCacheListener( String cacheName, ICacheListener obj )
342 throws IOException
343 {
344
345 }
346
347
348
349
350
351 public void removeCacheListener( ICacheListener obj )
352 throws IOException
353 {
354
355 }
356
357 /***
358 * @param listernId The listernId to set.
359 */
360 protected void setListenerId( long listernId )
361 {
362 this.listenerId = listernId;
363 }
364
365 /***
366 * @return Returns the listernId.
367 */
368 protected long getListenerId()
369 {
370 return listenerId;
371 }
372
373 /***
374 * @param tcpLateralCacheAttributes The tcpLateralCacheAttributes to set.
375 */
376 public void setTcpLateralCacheAttributes( ITCPLateralCacheAttributes tcpLateralCacheAttributes )
377 {
378 this.tcpLateralCacheAttributes = tcpLateralCacheAttributes;
379 }
380
381 /***
382 * @return Returns the tcpLateralCacheAttributes.
383 */
384 public ITCPLateralCacheAttributes getTcpLateralCacheAttributes()
385 {
386 return tcpLateralCacheAttributes;
387 }
388
389 }