View Javadoc
1   package org.apache.commons.jcs.auxiliary.remote;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Manual tester.
43   */
44  public class RemoteCacheClientTester
45      implements IRemoteCacheListener<String, String>, IRemoteCacheConstants, Remote
46  {
47      /** the observer */
48      protected ICacheObserver watch;
49  
50      /** the service */
51      protected ICacheService<String, String> cache;
52  
53      /** The registry host name. */
54      final String host;
55  
56      /** The registry port number. */
57      final int port;
58  
59      /** call count */
60      final int count;
61  
62      /** Description of the Field */
63      protected static long listenerId = 0;
64  
65      /**
66       * Gets the remoteType attribute of the RemoteCacheClientTest object
67       * @return The remoteType value
68       * @throws IOException
69       */
70      @Override
71      public RemoteType getRemoteType()
72          throws IOException
73      {
74          return RemoteType.LOCAL;
75      }
76  
77      /**
78       * Constructor for the RemoteCacheClientTest object
79       * @param count
80       * @throws MalformedURLException
81       * @throws NotBoundException
82       * @throws IOException
83       */
84      public RemoteCacheClientTester( int count )
85          throws MalformedURLException, NotBoundException, IOException
86      {
87          this( count, true, true, false );
88      }
89  
90      /**
91       * Constructor for the RemoteCacheClientTest object
92       * @param count
93       * @param write
94       * @param read
95       * @param delete
96       * @throws MalformedURLException
97       * @throws NotBoundException
98       * @throws IOException
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      * Constructor for the RemoteCacheClientTest object
108      * @param host
109      * @param port
110      * @param count
111      * @param write
112      * @param read
113      * @param delete
114      * @throws MalformedURLException
115      * @throws NotBoundException
116      * @throws IOException
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         // record export exception
126         Exception ee = null;
127 
128         try
129         {
130             // Export this remote object to make it available to receive
131             // incoming calls,
132             // using an anonymous port.
133             UnicastRemoteObject.exportObject( this );
134         }
135         catch ( ExportException e )
136         {
137             // use already exported object; remember exception
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                     // nothing
196                 }
197             }
198         }
199     }
200 
201     /**
202      * @param cb
203      * @throws IOException
204      */
205     @Override
206     public void handlePut( ICacheElement<String, String> cb )
207         throws IOException
208     {
209         p( "handlePut> cb=" + cb );
210     }
211 
212     /**
213      * @param cacheName
214      * @param key
215      * @throws IOException
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      * @param cacheName
226      * @throws IOException
227      */
228     @Override
229     public void handleRemoveAll( String cacheName )
230         throws IOException
231     {
232         p( "handleRemove> cacheName=" + cacheName );
233     }
234 
235     /**
236      * @param cacheName
237      * @throws IOException
238      */
239     @Override
240     public void handleDispose( String cacheName )
241         throws IOException
242     {
243         p( "handleDispose> cacheName=" + cacheName );
244     }
245 
246     /*
247      * public void handleRelease() throws IOException { p("handleRelease>"); }
248      */
249     /**
250      * The main program for the RemoteCacheClientTest class
251      * @param args The command line arguments
252      * @throws Exception
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      * Sets the listenerId attribute of the RemoteCacheClientTest object
289      * @param id The new listenerId value
290      * @throws IOException
291      */
292     @Override
293     public void setListenerId( long id )
294         throws IOException
295     {
296         listenerId = id;
297         p( "listenerId = " + id );
298     }
299 
300     /**
301      * Gets the listenerId attribute of the RemoteCacheClientTest object
302      * @return The listenerId value
303      * @throws IOException
304      */
305     @Override
306     public long getListenerId()
307         throws IOException
308     {
309         return listenerId;
310     }
311 
312     /**
313      * Helper for output, this is an user run test class
314      * @param s
315      */
316     private static void p( String s )
317     {
318         System.out.println( s );
319     }
320 
321     /**
322      * @return null
323      * @throws IOException
324      */
325     @Override
326     public String getLocalHostAddress()
327         throws IOException
328     {
329         // TODO Auto-generated method stub
330         return null;
331     }
332 
333     /**
334      * @throws IOException
335      */
336     @Override
337     public void dispose()
338         throws IOException
339     {
340         // TODO Auto-generated method stub
341     }
342 }