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.*;
22import gw.*;
23
24public class Client {
25
26    private static final String JNDI_ECHO_NAME = "ex3-echo-EchoHome";
27    private static final String JNDI_ECHOGW_NAME = "ex3-gw-EchoGWHome";
28
29    public static void main(String[] args) throws Exception {
30        String url = null;
31        String method = null;
32        String uname = null;
33        String passwd = null;
34        if (args.length > 3){
35            url = args[0];
36            method = args[1];
37            uname = args[2];
38            passwd = args[3];
39        } else if (args.length > 1){
40            url = args[0];
41            method = args[1];
42        } else {
43            System.out.println("Usage:: java client.Client <url> <method> <uname> <passwd>");
44            System.exit(0);
45        }
46
47        Properties h = new Properties();
48
49        //h.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
50        h.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
51
52        h.put(Context.PROVIDER_URL, url);
53        if (uname != null && passwd != null){
54            h.put(Context.SECURITY_PRINCIPAL, uname);
55            h.put(Context.SECURITY_CREDENTIALS, passwd);
56        }
57
58        Context ctx = new InitialContext(h);
59        String msg = "Hello, World!!";
60        String resp = null;
61
62        if (method.equals("gwEcho")){
63            Object home = ctx.lookup(JNDI_ECHOGW_NAME);
64            EchoGWHome ghome = (EchoGWHome) PortableRemoteObject.narrow(home, EchoGWHome.class);
65            EchoGW gstub = (EchoGW) PortableRemoteObject.narrow(ghome.create(), EchoGW.class);
66
67            System.out.println("Calling gstub.gwEcho(\"" + msg + "\")...");
68            resp = gstub.gwEcho(msg);
69        } else if (method.equals("echo") || method.equals("echo2") || method.equals("echo3") ||
70                    method.equals("echo4")){
71            Object home = ctx.lookup(JNDI_ECHO_NAME);
72            EchoHome ehome = (EchoHome) PortableRemoteObject.narrow(home, EchoHome.class);
73            Echo estub = (Echo) PortableRemoteObject.narrow(ehome.create(), Echo.class);
74
75            System.out.println("Calling estub." + method + "(\"" + msg + "\")...");
76            if (method.equals("echo")){
77                resp = estub.echo(msg);
78            } else  if (method.equals("echo2")){
79                resp = estub.echo2(msg);
80            } else if (method.equals("echo3")){
81                resp = estub.echo3(msg);
82            } else if (method.equals("echo4")){
83                resp = estub.echo4(msg);
84            }
85        } else {
86            System.out.println("No such method:: " + method);
87            return;
88        }
89        System.out.println("Returned String: " + resp);
90        System.out.println("... Client Executed successfully.");
91    }
92
93    private static Object narrow(Object o, Class cls){
94        return PortableRemoteObject.narrow(o, cls);
95    }
96}
97