1 /*
2  * @(#) $Id: EchoBean.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 echo;
11
12import javax.ejb.CreateException;
13import javax.ejb.SessionBean;
14import javax.ejb.SessionContext;
15import javax.naming.InitialContext;
16import javax.naming.NamingException;
17
18public class EchoBean implements SessionBean {
19
20  private SessionContext ctx;
21
22  public void ejbActivate() {
23    System.out.println("EchoBean.ejbActivate called");
24  }
25
26  public void ejbRemove() {
27    System.out.println("EchoBean.ejbRemove called");
28  }
29
30  public void ejbPassivate() {
31    System.out.println("EchoBean.ejbPassivate called");
32  }
33
34  public void setSessionContext(SessionContext ctx) {
35    System.out.println("EchoBean.setSessionContext called");
36    this.ctx = ctx;
37
38  }
39
40  public void ejbCreate () throws CreateException {
41    System.out.println("EchoBean.ejbCreate called");
42  }
43
44  public String echo(String arg) {
45    System.out.println("----- BEGIN EchoBean.echo(\"" + arg + "\") ----- ");
46    printCallerInfo();
47    System.out.println("----- END EchoBean.echo() ----- ");
48
49    return arg;
50  }
51
52  public String echo2(String arg) {
53    System.out.println("----- BEGIN EchoBean.echo2(\"" + arg + "\") ----- ");
54    printCallerInfo();
55    System.out.println("----- END EchoBean.echo2() ----- ");
56
57    return arg;
58  }
59
60  public String echo3(String arg) {
61    System.out.println("----- BEGIN EchoBean.echo3(\"" + arg + "\") ----- ");
62    printCallerInfo();
63    if (!ctx.isCallerInRole("echomanager"))
64        throw new java.security.AccessControlException("Caller not in proper role.");
65    System.out.println("----- END EchoBean.echo3() ----- ");
66
67    return arg;
68  }
69
70  public String echo4(String arg) {
71    System.out.println("----- BEGIN EchoBean.echo4(\"" + arg + "\") -----");
72    printCallerInfo();
73    System.out.println("----- END EchoBean.echo4() ----- ");
74
75    return arg;
76  }
77
78  private void printCallerInfo(){
79    java.security.Principal caller = ctx.getCallerPrincipal();
80    boolean inRole = ctx.isCallerInRole("echomanager");
81    System.out.println("Caller Name: " + caller.getName());
82    System.out.println("Caller in role \"echomanager\"? " + inRole);
83  }
84}
85
86
87
88
89
90
91
92
93