1   package org.apache.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 java.io.IOException;
23  import java.io.Serializable;
24  import java.net.MalformedURLException;
25  import java.rmi.Naming;
26  import java.rmi.NotBoundException;
27  import java.rmi.registry.Registry;
28  import java.rmi.server.ExportException;
29  import java.rmi.server.UnicastRemoteObject;
30  
31  import org.apache.jcs.access.exception.CacheException;
32  import org.apache.jcs.access.exception.ObjectExistsException;
33  import org.apache.jcs.auxiliary.remote.behavior.IRemoteCacheConstants;
34  import org.apache.jcs.auxiliary.remote.behavior.IRemoteCacheListener;
35  import org.apache.jcs.engine.CacheElement;
36  import org.apache.jcs.engine.behavior.ICacheElement;
37  import org.apache.jcs.engine.behavior.ICacheObserver;
38  import org.apache.jcs.engine.behavior.ICacheService;
39  
40  /***
41   * Description of the Class
42   *
43   * @author asmuts
44   * @created January 15, 2002
45   */
46  public class RemoteCacheClientTester
47      implements IRemoteCacheListener, IRemoteCacheConstants
48  {
49  
50      ICacheObserver watch;
51  
52      ICacheService cache;
53  
54      /*** The registry host name. */
55      final String host;
56  
57      /*** The registry port number. */
58      final int port;
59  
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       *
68       * @return The remoteType value
69       */
70      public int getRemoteType()
71          throws IOException
72      {
73          return 0;
74      }
75  
76      /***
77       * Constructor for the RemoteCacheClientTest object
78       *
79       * @param count
80       * @exception MalformedURLException
81       * @exception NotBoundException
82       * @exception 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       *
93       * @param count
94       * @param write
95       * @param read
96       * @param delete
97       * @exception MalformedURLException
98       * @exception NotBoundException
99       * @exception IOException
100      */
101     public RemoteCacheClientTester( int count, boolean write, boolean read, boolean delete )
102         throws MalformedURLException, NotBoundException, IOException
103     {
104         this( "", Registry.REGISTRY_PORT, count, write, read, delete );
105     }
106 
107     /***
108      * Constructor for the RemoteCacheClientTest object
109      *
110      * @param host
111      * @param port
112      * @param count
113      * @param write
114      * @param read
115      * @param delete
116      * @exception MalformedURLException
117      * @exception NotBoundException
118      * @exception IOException
119      */
120     public RemoteCacheClientTester( String host, int port, int count, boolean write, boolean read, boolean delete )
121         throws MalformedURLException, NotBoundException, IOException
122     {
123         this.count = count;
124         this.host = host;
125         this.port = port;
126         // record export exception
127         Exception ee = null;
128 
129         try
130         {
131             // Export this remote object to make it available to receive
132             // incoming calls,
133             // using an anonymous port.
134             UnicastRemoteObject.exportObject( this );
135             ee = null;
136         }
137         catch ( ExportException e )
138         {
139             // use already exported object; remember exception
140             ee = e;
141             ee.printStackTrace();
142         }
143         String service = System.getProperty( REMOTE_CACHE_SERVICE_NAME );
144 
145         if ( service == null )
146         {
147             service = REMOTE_CACHE_SERVICE_VAL;
148         }
149         String registry = "//" + host + ":" + port + "/" + service;
150 
151         p( "looking up server " + registry );
152 
153         Object obj = Naming.lookup( registry );
154 
155         p( "server found" );
156 
157         cache = (ICacheService) obj;
158         watch = (ICacheObserver) obj;
159 
160         p( "subscribing to the server" );
161 
162         watch.addCacheListener( "testCache", this );
163         ICacheElement cb = new CacheElement( "testCache", "testKey", "testVal" );
164 
165         for ( int i = 0; i < count; i++ )
166         {
167             cb = new CacheElement( "testCache", "" + i, "" + i );
168 
169             if ( delete )
170             {
171                 p( "deleting a cache item from the server " + i );
172 
173                 cache.remove( cb.getCacheName(), cb.getKey() );
174             }
175             if ( write )
176             {
177                 p( "putting a cache bean to the server " + i );
178 
179                 try
180                 {
181                     cache.update( cb );
182                 }
183                 catch ( ObjectExistsException oee )
184                 {
185                     p( oee.toString() );
186                 }
187             }
188             if ( read )
189             {
190                 try
191                 {
192                     Object val = cache.get( cb.getCacheName(), cb.getKey() );
193                     p( "get " + cb.getKey() + " returns " + val );
194                 }
195                 catch ( CacheException onfe )
196                 {
197                     // nothing
198                 }
199             }
200         }
201     }
202 
203     /*** Description of the Method */
204     public void handlePut( ICacheElement cb )
205         throws IOException
206     {
207         p( "handlePut> cb=" + cb );
208     }
209 
210     /*** Description of the Method */
211     public void handleRemove( String cacheName, Serializable key )
212         throws IOException
213     {
214         p( "handleRemove> cacheName=" + cacheName + ", key=" + key );
215     }
216 
217     /*** Description of the Method */
218     public void handleRemoveAll( String cacheName )
219         throws IOException
220     {
221         p( "handleRemove> cacheName=" + cacheName );
222     }
223 
224     /*** Description of the Method */
225     public void handleDispose( String cacheName )
226         throws IOException
227     {
228         p( "handleDispose> cacheName=" + cacheName );
229     }
230 
231     /*
232      * public void handleRelease() throws IOException { p("handleRelease>"); }
233      */
234     /***
235      * The main program for the RemoteCacheClientTest class
236      *
237      * @param args
238      *            The command line arguments
239      * @throws Exception
240      */
241     public static void main( String[] args )
242         throws Exception
243     {
244         int count = 0;
245         boolean read = false;
246         boolean write = false;
247         boolean delete = false;
248 
249         for ( int i = 0; i < args.length; i++ )
250         {
251             if ( args[i].startsWith( "-" ) )
252             {
253                 if ( !read )
254                 {
255                     read = args[i].indexOf( "r" ) != -1;
256                 }
257                 if ( !write )
258                 {
259                     write = args[i].indexOf( "w" ) != -1;
260                 }
261                 if ( !delete )
262                 {
263                     delete = args[i].indexOf( "d" ) != -1;
264                 }
265             }
266             else
267             {
268                 count = Integer.parseInt( args[i] );
269             }
270         }
271         new RemoteCacheClientTester( count, write, read, delete );
272     }
273 
274     /***
275      * Sets the listenerId attribute of the RemoteCacheClientTest object
276      *
277      * @param id
278      *            The new listenerId value
279      */
280     public void setListenerId( long id )
281         throws IOException
282     {
283         listenerId = id;
284         p( "listenerId = " + id );
285     }
286 
287     /***
288      * Gets the listenerId attribute of the RemoteCacheClientTest object
289      *
290      * @return The listenerId value
291      */
292     public long getListenerId()
293         throws IOException
294     {
295         return listenerId;
296     }
297 
298     /*** Helper for output, this is an user run test class
299      * @param s
300      */
301     private static void p( String s )
302     {
303         System.out.println( s );
304     }
305 
306     public String getLocalHostAddress()
307         throws IOException
308     {
309         // TODO Auto-generated method stub
310         return null;
311     }
312 
313     /* (non-Javadoc)
314      * @see org.apache.jcs.auxiliary.remote.behavior.IRemoteCacheListener#dispose()
315      */
316     public void dispose()
317         throws IOException
318     {
319         // TODO Auto-generated method stub
320 
321     }
322 }