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 org.apache.commons.jcs.access.exception.CacheException;
23 import org.apache.commons.jcs.access.exception.ObjectExistsException;
24 import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheConstants;
25 import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheListener;
26 import org.apache.commons.jcs.auxiliary.remote.server.behavior.RemoteType;
27 import org.apache.commons.jcs.engine.CacheElement;
28 import org.apache.commons.jcs.engine.behavior.ICacheElement;
29 import org.apache.commons.jcs.engine.behavior.ICacheObserver;
30 import org.apache.commons.jcs.engine.behavior.ICacheService;
31
32 import java.io.IOException;
33 import java.net.MalformedURLException;
34 import java.rmi.Naming;
35 import java.rmi.NotBoundException;
36 import java.rmi.Remote;
37 import java.rmi.registry.Registry;
38 import java.rmi.server.ExportException;
39 import java.rmi.server.UnicastRemoteObject;
40
41
42
43
44 public class RemoteCacheClientTester
45 implements IRemoteCacheListener<String, String>, IRemoteCacheConstants, Remote
46 {
47
48 protected ICacheObserver watch;
49
50
51 protected ICacheService<String, String> cache;
52
53
54 final String host;
55
56
57 final int port;
58
59
60 final int count;
61
62
63 protected static long listenerId = 0;
64
65
66
67
68
69
70 @Override
71 public RemoteType getRemoteType()
72 throws IOException
73 {
74 return RemoteType.LOCAL;
75 }
76
77
78
79
80
81
82
83
84 public RemoteCacheClientTester( int count )
85 throws MalformedURLException, NotBoundException, IOException
86 {
87 this( count, true, true, false );
88 }
89
90
91
92
93
94
95
96
97
98
99
100 public RemoteCacheClientTester( int count, boolean write, boolean read, boolean delete )
101 throws MalformedURLException, NotBoundException, IOException
102 {
103 this( "", Registry.REGISTRY_PORT, count, write, read, delete );
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117
118 @SuppressWarnings("unchecked")
119 public RemoteCacheClientTester( String host, int port, int count, boolean write, boolean read, boolean delete )
120 throws MalformedURLException, NotBoundException, IOException
121 {
122 this.count = count;
123 this.host = host;
124 this.port = port;
125
126 Exception ee = null;
127
128 try
129 {
130
131
132
133 UnicastRemoteObject.exportObject( this );
134 }
135 catch ( ExportException e )
136 {
137
138 ee = e;
139 ee.printStackTrace();
140 }
141 String service = System.getProperty( REMOTE_CACHE_SERVICE_NAME );
142
143 if ( service == null )
144 {
145 service = REMOTE_CACHE_SERVICE_VAL;
146 }
147 String registry = RemoteUtils.getNamingURL(host, port, service);
148
149 p( "looking up server " + registry );
150
151 Object obj = Naming.lookup( registry );
152
153 p( "server found" );
154
155 cache = (ICacheService<String, String>) obj;
156 watch = (ICacheObserver) obj;
157
158 p( "subscribing to the server" );
159
160 watch.addCacheListener( "testCache", this );
161 ICacheElement<String, String> cb = new CacheElement<String, String>( "testCache", "testKey", "testVal" );
162
163 for ( int i = 0; i < count; i++ )
164 {
165 cb = new CacheElement<String, String>( "testCache", "" + i, "" + i );
166
167 if ( delete )
168 {
169 p( "deleting a cache item from the server " + i );
170
171 cache.remove( cb.getCacheName(), cb.getKey() );
172 }
173 if ( write )
174 {
175 p( "putting a cache bean to the server " + i );
176
177 try
178 {
179 cache.update( cb );
180 }
181 catch ( ObjectExistsException oee )
182 {
183 p( oee.toString() );
184 }
185 }
186 if ( read )
187 {
188 try
189 {
190 Object val = cache.get( cb.getCacheName(), cb.getKey() );
191 p( "get " + cb.getKey() + " returns " + val );
192 }
193 catch ( CacheException onfe )
194 {
195
196 }
197 }
198 }
199 }
200
201
202
203
204
205 @Override
206 public void handlePut( ICacheElement<String, String> cb )
207 throws IOException
208 {
209 p( "handlePut> cb=" + cb );
210 }
211
212
213
214
215
216
217 @Override
218 public void handleRemove( String cacheName, String key )
219 throws IOException
220 {
221 p( "handleRemove> cacheName=" + cacheName + ", key=" + key );
222 }
223
224
225
226
227
228 @Override
229 public void handleRemoveAll( String cacheName )
230 throws IOException
231 {
232 p( "handleRemove> cacheName=" + cacheName );
233 }
234
235
236
237
238
239 @Override
240 public void handleDispose( String cacheName )
241 throws IOException
242 {
243 p( "handleDispose> cacheName=" + cacheName );
244 }
245
246
247
248
249
250
251
252
253
254 public static void main( String[] args )
255 throws Exception
256 {
257 int count = 0;
258 boolean read = false;
259 boolean write = false;
260 boolean delete = false;
261
262 for ( int i = 0; i < args.length; i++ )
263 {
264 if ( args[i].startsWith( "-" ) )
265 {
266 if ( !read )
267 {
268 read = args[i].indexOf( "r" ) != -1;
269 }
270 if ( !write )
271 {
272 write = args[i].indexOf( "w" ) != -1;
273 }
274 if ( !delete )
275 {
276 delete = args[i].indexOf( "d" ) != -1;
277 }
278 }
279 else
280 {
281 count = Integer.parseInt( args[i] );
282 }
283 }
284 new RemoteCacheClientTester( count, write, read, delete );
285 }
286
287
288
289
290
291
292 @Override
293 public void setListenerId( long id )
294 throws IOException
295 {
296 listenerId = id;
297 p( "listenerId = " + id );
298 }
299
300
301
302
303
304
305 @Override
306 public long getListenerId()
307 throws IOException
308 {
309 return listenerId;
310 }
311
312
313
314
315
316 private static void p( String s )
317 {
318 System.out.println( s );
319 }
320
321
322
323
324
325 @Override
326 public String getLocalHostAddress()
327 throws IOException
328 {
329
330 return null;
331 }
332
333
334
335
336 @Override
337 public void dispose()
338 throws IOException
339 {
340
341 }
342 }