View Javadoc
1   package org.apache.commons.jcs3.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 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   * 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;
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( final 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( 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      * 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( 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         // 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 ( final 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         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                     // nothing
196                 }
197             }
198         }
199     }
200 
201     /**
202      * @param cb
203      * @throws IOException
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      * @param cacheName
214      * @param key
215      * @throws IOException
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      * @param cacheName
226      * @throws IOException
227      */
228     @Override
229     public void handleRemoveAll( final 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( final 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( 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      * Sets the listenerId attribute of the RemoteCacheClientTest object
288      * @param id The new listenerId value
289      * @throws IOException
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      * Gets the listenerId attribute of the RemoteCacheClientTest object
301      * @return The listenerId value
302      * @throws IOException
303      */
304     @Override
305     public long getListenerId()
306         throws IOException
307     {
308         return listenerId;
309     }
310 
311     /**
312      * Helper for output, this is an user run test class
313      * @param s
314      */
315     private static void p( final String s )
316     {
317         System.out.println( s );
318     }
319 
320     /**
321      * @return null
322      * @throws IOException
323      */
324     @Override
325     public String getLocalHostAddress()
326         throws IOException
327     {
328         // TODO Auto-generated method stub
329         return null;
330     }
331 
332     /**
333      * @throws IOException
334      */
335     @Override
336     public void dispose()
337         throws IOException
338     {
339         // TODO Auto-generated method stub
340     }
341 }