1 /*
2  * @(#) $Id: Client.java,v 1.2 2003/07/08 08:13:52 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10package client;
11
12import javax.naming.Context;
13import echo.EchoHome;
14import echo.Echo;
15
16public class Client {
17    private static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
18    //private static String JNDI_FACTORY = "com.sun.jndi.cosnaming.CNCtxFactory";
19    public static void main(String[] args) throws Exception {
20        int count = 1;
21        if (args.length < 1){
22            System.out.println("Usage:: java client.Client <url>");
23            return;
24        }
25        String url = args[0];
26        if (args.length > 1){
27            count = Integer.parseInt(args[1]);
28        }
29
30        java.util.Properties h = new java.util.Properties();
31        h.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
32
33        h.put(Context.PROVIDER_URL, url);
34        Context ctx = new javax.naming.InitialContext(h);
35
36        Object home = ctx.lookup("ex1-echo-EchoHome");
37        EchoHome ehome = (EchoHome)narrow(home, EchoHome.class);
38        Echo estub = (Echo)narrow(ehome.create(), Echo.class);
39
40        String msg = "Hello, World!!";
41        System.out.println("Calling Echo.echo(\"" + msg + "\") ...");
42        String resp = null;
43        long ts = System.currentTimeMillis();
44        for (int i = 0; i < count; ++i){
45            resp = estub.echo(msg);
46        }
47        long et = System.currentTimeMillis() - ts;
48        System.out.println("... Echo.echo(\"" + msg + "\") = " + resp);
49
50        if (count > 1){
51            System.out.println("Elapsed Time for " + count + " invocations: " + et + " Milli Secs.");
52        }
53
54        System.out.println("Echo Client Executed successfully.");
55    }
56
57    private static Object narrow(Object o, Class c){
58        return javax.rmi.PortableRemoteObject.narrow(o, c);
59    }
60}
61