1 /*
2  * @(#) $Id: SampleAction.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.security.PrivilegedAction;
13import javax.naming.Context;
14import javax.naming.InitialContext;
15import javax.rmi.PortableRemoteObject;
16import java.util.Properties;
17import javax.ejb.CreateException;
18import javax.ejb.EJBException;
19import javax.ejb.FinderException;
20import javax.ejb.ObjectNotFoundException;
21import javax.ejb.RemoveException;
22import java.rmi.RemoteException;
23import javax.naming.NamingException;
24import echo.Echo;
25import echo.EchoHome;
26
27public class SampleAction implements PrivilegedAction {
28    private static final String JNDI_NAME = "ex2-echo-EchoHome";
29    private String url;
30
31    public SampleAction(String url){
32        this.url = url;
33    }
34
35    public Object run(){
36        Object obj = null;
37
38        try {
39            doit();
40        } catch(Exception e) {
41            e.printStackTrace();
42        }
43        return obj;
44    }
45
46    public void doit() throws NamingException, CreateException, RemoteException, RemoveException {
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        Context ctx = new InitialContext(h);
54
55        Object home = ctx.lookup("ex2-echo-EchoHome");
56        EchoHome ehome = (EchoHome)narrow(home, EchoHome.class);
57        Echo estub = (Echo)narrow(ehome.create(), Echo.class);
58
59        String msg = "Hello, World!!";
60        System.out.println("Calling Echo.echo(\"" + msg + "\") ...");
61        String resp = estub.echo(msg);
62        System.out.println("... Echo.echo(\"" + msg + "\") = " + resp);
63
64        System.out.println("... Echo Client Executed successfully.");
65    }
66    private Object narrow(Object o, Class c){
67        return javax.rmi.PortableRemoteObject.narrow(o, c);
68    }
69}
70
71