1 /*
2  * @(#) $Id: EchoGWBean.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 gw;
11
12import java.util.Properties;
13import javax.ejb.CreateException;
14import javax.ejb.SessionBean;
15import javax.ejb.SessionContext;
16import javax.naming.InitialContext;
17import javax.naming.Context;
18import javax.naming.NamingException;
19import javax.rmi.PortableRemoteObject;
20import echo.EchoHome;
21import echo.Echo;
22
23public class EchoGWBean implements SessionBean {
24    private SessionContext ctx;
25    private String targetUrl;
26    private String targetJNDIName;
27
28    public void ejbActivate() {
29        System.out.println("EchoGWBean.ejbActivate called");
30    }
31
32    public void ejbRemove() {
33        System.out.println("EchoGWBean.ejbRemove called");
34    }
35
36    public void ejbPassivate() {
37        System.out.println("EchoGWBean.ejbPassivate called");
38    }
39
40    public void setSessionContext(SessionContext ctx) {
41        System.out.println("EchoGWBean.setSessionContext called");
42        this.ctx = ctx;
43    }
44
45    public void ejbCreate () throws CreateException {
46        System.out.println("EchoGWBean.ejbCreate called");
47    }
48
49    public String gwEcho(String arg) {
50        System.out.println("----- BEGIN EchoGWBean.gwEcho(\"" + arg + "\") -----");
51        printCallerInfo();
52
53        String retval = "Cannot Forward";
54
55        try {
56            getEnvEntries();
57            Properties h = new Properties();
58            h.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
59            h.put(Context.PROVIDER_URL, targetUrl);
60            InitialContext ic = new InitialContext(h);
61            Object home = ic.lookup(targetJNDIName);
62            EchoHome echoHome = (EchoHome)PortableRemoteObject.narrow(home, EchoHome.class);
63            Echo echo = (Echo) PortableRemoteObject.narrow(echoHome.create(), Echo.class);
64            System.out.println("----- END EchoGWBean.gwEcho() -----");
65            retval = echo.echo("gwEcho:: " + arg);
66        } catch (Exception e){
67            System.err.println("gwEcho:: Exception -- " + e);
68        }
69        return retval;
70    }
71
72    private void printCallerInfo(){
73        java.security.Principal caller = ctx.getCallerPrincipal();
74        boolean inRole = ctx.isCallerInRole("echomanager");
75        System.out.println("Caller Name: " + caller.getName());
76        System.out.println("Caller in role \"echomanager\"? " + inRole);
77    }
78
79    public void getEnvEntries() throws javax.naming.NamingException {
80        InitialContext ic = new InitialContext();
81        targetUrl = (String)ic.lookup("java:comp/env/target_url");
82        targetJNDIName = (String)ic.lookup("java:comp/env/target_jndi_name");
83    }
84}
85
86
87
88
89
90
91
92
93