1
10package rbac;
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 rba.*;
22
23
35
36public class Client {
37
38 private static final String JNDI_NAME = "rba-TraderHome";
39
40 private String url;
41 String username = null;
42 String password = null;
43 private TraderHome home;
44
45 public Client(String url, String username, String password)
46 throws NamingException
47 {
48
49 this.url = url;
50 this.username = username;
51 this.password = password;
52
53 home = lookupHome();
54 }
55
56
66 public static void main(String[] args) throws Exception {
67
68 log("\nBeginning statelessSession.Client...\n");
69
70 String url = "t3://localhost:7001";
71 String username = null;
72 String password = null;
73
74 if (args.length != 3) {
76 System.out.println("Usage: java rbac.Client t3://hostname:port username password");
77 return;
78 } else {
79 url = args[0];
80 username = args[1];
81 password = args[2];
82 }
83
84 Client client = null;
85 try {
86 client = new Client(url, username, password);
87 } catch (NamingException ne) {
88 System.exit(1);
89 }
90
91 try {
92 client.example();
93 } catch (Exception e) {
94 log("There was an exception while creating and using the Trader.");
95 log("This indicates that there was a problem communicating with the server: "+e);
96 }
97
98 log("\nEnd statelessSession.Client...\n");
99 }
00
01
04 public void example()
05 throws CreateException, RemoteException, RemoveException
06 {
07
08 log("Creating a trader");
10 Trader trader = (Trader) narrow(home.create(), Trader.class);
11
12 String [] stocks = {"BEAS", "MSFT", "AMZN", "HWP" };
13
14 for (int i=0; i<stocks.length; i++) {
16 int shares = (i+1) * 100;
17 log("Buying "+shares+" shares of "+stocks[i]+".");
18 trader.buy(stocks[i], shares);
19 }
20
21 for (int i=0; i<stocks.length; i++) {
23 int shares = (i+1) * 100;
24 log("Selling "+shares+" shares of "+stocks[i]+".");
25 trader.sell(stocks[i], shares);
26 }
27
28 log("Removing the trader");
30 trader.remove();
31
32 }
33
34
37 private Object narrow(Object ref, Class c) {
38 return PortableRemoteObject.narrow(ref, c);
39 }
40
41
44 private TraderHome lookupHome()
45 throws NamingException
46 {
47 Context ctx = getInitialContext();
49
50 try {
51 Object home = ctx.lookup(JNDI_NAME);
52 return (TraderHome) narrow(home, TraderHome.class);
53 } catch (NamingException ne) {
54 log("The client was unable to lookup the EJBHome. Please make sure ");
55 log("that you have deployed the ejb with the JNDI name "+JNDI_NAME+" on the WebLogic server at "+url);
56 throw ne;
57 }
58 }
59
60
64 private Context getInitialContext() throws NamingException {
65
66 try {
67 Properties h = new Properties();
69 h.put(Context.INITIAL_CONTEXT_FACTORY,
70 "weblogic.jndi.WLInitialContextFactory");
71 h.put(Context.PROVIDER_URL, url);
72
73 h.put(Context.SECURITY_AUTHENTICATION, "simple");
74 h.put(Context.SECURITY_PRINCIPAL, username);
75 h.put(Context.SECURITY_CREDENTIALS, password);
76
77 return new InitialContext(h);
78 } catch (NamingException ne) {
79 log("We were unable to get a connection to the WebLogic server at "+url);
80 log("exception: " + ne);
81 log("Please make sure that the server is running.");
82 throw ne;
83 }
84 }
85
86
92
98 private static void log(String s) {
99 System.out.println(s);
00 }
01
02}
03