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 java.rmi.RemoteException;
13import java.util.Properties;
14
15import javax.ejb.CreateException;
16import javax.ejb.RemoveException;
17import javax.naming.Context;
18import javax.naming.InitialContext;
19import javax.naming.NamingException;
20import javax.rmi.PortableRemoteObject;
21import echo.*;
22
23public class Client {
24    public static void main(String[] args) throws Exception {
25        String url = null;
26        String uname = null;
27        String passwd = null;
28        if (args.length > 2){
29            url = args[0];
30            uname = args[1];
31            passwd = args[2];
32        } else {
33            System.out.println("Usage:: java client.Client <url> <uname> <passwd>");
34            System.exit(0);
35        }
36
37        Properties h = new Properties();
38
39        //h.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
40        h.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
41
42        h.put(Context.PROVIDER_URL, url);
43        h.put(Context.SECURITY_PRINCIPAL, uname);
44        h.put(Context.SECURITY_CREDENTIALS, passwd);
45        Context ctx = new InitialContext(h);
46
47        Object home = ctx.lookup("ex2-echo-EchoHome");
48        EchoHome ehome = (EchoHome)narrow(home, EchoHome.class);
49        Echo estub = (Echo)narrow(ehome.create(), Echo.class);
50
51        String msg = "Hello, World!!";
52        System.out.println("Calling Echo.echo(\"" + msg + "\") ...");
53        String resp = estub.echo(msg);
54        System.out.println("... Echo.echo(\"" + msg + "\") = " + resp);
55
56        System.out.println("... Echo Client Executed successfully.");
57    }
58    private static Object narrow(Object o, Class c){
59        return javax.rmi.PortableRemoteObject.narrow(o, c);
60    }
61}
62