1
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.Context;
24import javax.naming.InitialContext;
25import javax.naming.NamingException;
26import echo.*;
27
28public class SampleAction implements PrivilegedAction {
29 private static final String JNDI_NAME = "ex3-echo-EchoHome";
30 private String url;
31
32 public SampleAction(String url){
33 this.url = url;
34 }
35
36 public Object run(){
37 Object obj = null;
38
39 try {
40 doit();
41 } catch(Exception e) {
42 e.printStackTrace();
43 }
44 return obj;
45 }
46
47 public void doit() throws NamingException, CreateException, RemoteException, RemoveException {
48 System.out.println("Looking for EchoHome ...");
49 EchoHome home = lookupHome();
50 System.out.println("... EchoHome Found.");
51
52 System.out.println("Creating a Echo stub ...");
53 Echo estub = (Echo) PortableRemoteObject.narrow(home.create(), Echo.class);
54 System.out.println("... Echo stub created.");
55
56 String msg = "Hello, World!!";
57 System.out.println("Calling estub.echo(\"" + msg + "\")...");
58 String resp = estub.echo(msg);
59 System.out.println("Returned String -- " + resp);
60
61 System.out.println("... Echo Client Executed successfully.");
62 }
63
64 private EchoHome lookupHome() throws NamingException {
65 Context ctx = getInitialContext();
66
67 try {
68 Object home = ctx.lookup(JNDI_NAME);
69 return (EchoHome) PortableRemoteObject.narrow(home, EchoHome.class);
70 } catch (NamingException ne) {
71 System.out.println("The client was unable to lookup the EJBHome. Please make sure ");
72 System.out.println("that you have deployed the ejb with the JNDI name ");
73 System.out.println(JNDI_NAME + " on the WebLogic server at " + url);
74 throw ne;
75 }
76 }
77
78 private Context getInitialContext() throws NamingException {
79 try {
80 Properties h = new Properties();
82 h.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
83
85 h.put(Context.PROVIDER_URL, url);
86 return new InitialContext(h);
87 } catch (NamingException ne) {
88 System.out.println("Unable to get a connection to the server at " + url);
89 System.out.println("exception: " + ne);
90 throw ne;
91 }
92 }
93}
94
95