1
10package rba;
11
12import javax.ejb.CreateException;
13import javax.ejb.SessionBean;
14import javax.ejb.SessionContext;
15import javax.naming.InitialContext;
16import javax.naming.NamingException;
17import javax.rmi.PortableRemoteObject;
18import java.util.Properties;
19import javax.naming.Context;
20
21public class TraderBean implements SessionBean {
22
23 private SessionContext ctx;
24 private int tradeLimit;
25
26 private void log(String s) {
27 System.out.println(s);
28 }
29
30 public void ejbActivate() {
31 log("ejbActivate called");
32 }
33
34 public void ejbRemove() {
35 log("ejbRemove called");
36 }
37
38 public void ejbPassivate() {
39 log("ejbPassivate called");
40 }
41
42 public void setSessionContext(SessionContext ctx) {
43 log("setSessionContext called");
44 this.ctx = ctx;
45
46 }
47
48 public void ejbCreate () throws CreateException {
49 log("ejbCreate called");
50
51 try {
52 InitialContext ic = new InitialContext();
53
54 Integer tl = (Integer) ic.lookup("java:/comp/env/tradeLimit");
55
56 tradeLimit = tl.intValue();
57 } catch (NamingException ne) {
58 throw new CreateException("Failed to find environment value "+ne);
59 }
60 }
61
62 public TradeResult buy(String stockSymbol, int shares) {
63 log("Prinicpal = " + ctx.getCallerPrincipal());
64 java.security.AccessControlContext acc = java.security.AccessController.getContext();
65 if (acc != null){
66 log("Subject = " + javax.security.auth.Subject.getSubject(acc));
67 } else {
68 log("acc = " + acc);
69 }
70 if (shares > tradeLimit) {
71 log("Attempt to buy "+shares+" is greater than limit of "+tradeLimit);
72 shares = tradeLimit;
73 }
74
75 log("Buying "+shares+" shares of "+stockSymbol);
76 try {
77 Properties h = new Properties();
78 h.put(Context.INITIAL_CONTEXT_FACTORY,
79 "weblogic.jndi.WLInitialContextFactory");
80 h.put(Context.PROVIDER_URL, "t3://localhost:7005");
81 InitialContext ic = new InitialContext(h);
82 Object home = ic.lookup("rba-HelloHome");
83 HelloHome helloHome = (HelloHome)PortableRemoteObject.narrow(home, HelloHome.class);
84 Hello hello = (Hello) PortableRemoteObject.narrow(helloHome.create(), Hello.class);
85 log("hello.hello(" + stockSymbol + ") = " + hello.hello(stockSymbol));
86 } catch (Exception e){
87 log("IC Exception:: " + e);
88 }
89
90 return new TradeResult(shares, stockSymbol);
91 }
92 public TradeResult sell(String stockSymbol, int shares) {
93
94 if (shares > tradeLimit) {
95 log("Attempt to sell "+shares+" is greater than limit of "+tradeLimit);
96 shares = tradeLimit;
97 }
98
99 log("Selling "+shares+" shares of "+stockSymbol);
00
01 return new TradeResult(shares, stockSymbol);
02 }
03
04}
05
06
07
08
09
10
11
12
13