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.MalformedURLException;
24 import java.rmi.Naming;
25 import java.rmi.NotBoundException;
26 import java.rmi.Remote;
27 import java.rmi.registry.Registry;
28 import java.rmi.server.ExportException;
29 import java.rmi.server.UnicastRemoteObject;
30
31 import org.apache.commons.jcs3.access.exception.CacheException;
32 import org.apache.commons.jcs3.access.exception.ObjectExistsException;
33 import org.apache.commons.jcs3.auxiliary.remote.behavior.IRemoteCacheConstants;
34 import org.apache.commons.jcs3.auxiliary.remote.behavior.IRemoteCacheListener;
35 import org.apache.commons.jcs3.auxiliary.remote.server.behavior.RemoteType;
36 import org.apache.commons.jcs3.engine.CacheElement;
37 import org.apache.commons.jcs3.engine.behavior.ICacheElement;
38 import org.apache.commons.jcs3.engine.behavior.ICacheObserver;
39 import org.apache.commons.jcs3.engine.behavior.ICacheService;
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;
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( final 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( final int count, final boolean write, final boolean read, final 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( final String host, final int port, final int count, final boolean write, final boolean read, final 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 ( final 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 final String registry = RemoteUtils.getNamingURL(host, port, service);
148
149 p( "looking up server " + registry );
150
151 final 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<>( "testCache", "testKey", "testVal" );
162
163 for ( int i = 0; i < count; i++ )
164 {
165 cb = new CacheElement<>( "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 ( final ObjectExistsException oee )
182 {
183 p( oee.toString() );
184 }
185 }
186 if ( read )
187 {
188 try
189 {
190 final Object val = cache.get( cb.getCacheName(), cb.getKey() );
191 p( "get " + cb.getKey() + " returns " + val );
192 }
193 catch ( final CacheException onfe )
194 {
195
196 }
197 }
198 }
199 }
200
201
202
203
204
205 @Override
206 public void handlePut( final 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( final String cacheName, final 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( final String cacheName )
230 throws IOException
231 {
232 p( "handleRemove> cacheName=" + cacheName );
233 }
234
235
236
237
238
239 @Override
240 public void handleDispose( final 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( final 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 (final String arg : args) {
263 if ( arg.startsWith( "-" ) )
264 {
265 if ( !read )
266 {
267 read = arg.indexOf( "r" ) != -1;
268 }
269 if ( !write )
270 {
271 write = arg.indexOf( "w" ) != -1;
272 }
273 if ( !delete )
274 {
275 delete = arg.indexOf( "d" ) != -1;
276 }
277 }
278 else
279 {
280 count = Integer.parseInt( arg );
281 }
282 }
283 new RemoteCacheClientTester( count, write, read, delete );
284 }
285
286
287
288
289
290
291 @Override
292 public void setListenerId( final long id )
293 throws IOException
294 {
295 listenerId = id;
296 p( "listenerId = " + id );
297 }
298
299
300
301
302
303
304 @Override
305 public long getListenerId()
306 throws IOException
307 {
308 return listenerId;
309 }
310
311
312
313
314
315 private static void p( final String s )
316 {
317 System.out.println( s );
318 }
319
320
321
322
323
324 @Override
325 public String getLocalHostAddress()
326 throws IOException
327 {
328
329 return null;
330 }
331
332
333
334
335 @Override
336 public void dispose()
337 throws IOException
338 {
339
340 }
341 }