KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > ejb > CallerFacadeBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.security.ejb;
23
24 import java.security.Principal JavaDoc;
25 import javax.ejb.CreateException JavaDoc;
26 import javax.ejb.EJBException JavaDoc;
27 import javax.ejb.SessionBean JavaDoc;
28 import javax.ejb.SessionContext JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31 import javax.rmi.PortableRemoteObject JavaDoc;
32
33 import org.jboss.logging.Logger;
34 import org.jboss.test.security.interfaces.CalledSessionLocalHome;
35 import org.jboss.test.security.interfaces.CalledSessionLocal;
36
37 /**
38  * A simple session bean that calls the CalleeBean
39  * @author Scott.Stark@jboss.org
40  * @version $Revision: 58115 $
41  */

42 public class CallerFacadeBean implements SessionBean JavaDoc
43 {
44    private static Logger log = Logger.getLogger(CallerFacadeBean.class);
45    private SessionContext JavaDoc sessionContext;
46
47    public void ejbCreate() throws CreateException JavaDoc
48    {
49       log.debug("ejbCreate() called");
50    }
51
52    public void ejbActivate()
53    {
54       log.debug("ejbActivate() called");
55    }
56
57    public void ejbPassivate()
58    {
59       log.debug("ejbPassivate() called");
60    }
61
62    public void ejbRemove()
63    {
64       log.debug("ejbRemove() called");
65    }
66
67    public void setSessionContext(SessionContext JavaDoc context)
68    {
69       sessionContext = context;
70    }
71
72    /**
73     * This method calls echo on a StatelessSessionLocal and asserts that the
74     * caller is in the EchoCaller role.
75     */

76    public String JavaDoc invokeEcho(String JavaDoc arg)
77    {
78       log.debug("echo, arg=" + arg);
79       Principal JavaDoc p = sessionContext.getCallerPrincipal();
80       log.debug("echo, callerPrincipal=" + p);
81       boolean isEchoCaller = sessionContext.isCallerInRole("EchoCaller");
82       log.debug("echo, isCallerInRole('EchoCaller')=" + isEchoCaller);
83       boolean isInternalRole = sessionContext.isCallerInRole("InternalRole");
84       log.debug("echo, isCallerInRole('InternalRole')=" + isInternalRole);
85
86       try
87       {
88          InitialContext JavaDoc ic = new InitialContext JavaDoc();
89          Context JavaDoc enc = (Context JavaDoc) ic.lookup("java:comp/env");
90          Object JavaDoc ref = enc.lookup("ejb/CalledSessionLocalHome");
91          CalledSessionLocalHome home = (CalledSessionLocalHome) PortableRemoteObject.narrow(ref,
92             CalledSessionLocalHome.class);
93          CalledSessionLocal localBean = home.create();
94          String JavaDoc echo2 = localBean.invokeEcho(arg);
95          log.debug("echo#1, callee.invokeEcho=" + echo2);
96          echo2 = localBean.invokeEcho(arg);
97          log.debug("echo#2, callee.invokeEcho=" + echo2);
98       }
99       catch (Exception JavaDoc e)
100       {
101          log.error("Failed to invoke CalledSession.invokeEcho", e);
102          throw new EJBException JavaDoc("Failed to invoke Callee.echo", e);
103       }
104       return arg;
105    }
106
107    /**
108     * This method should call invokeEcho on another CalledSession
109     */

110    public void callEcho()
111    {
112       try
113       {
114          InitialContext JavaDoc ic = new InitialContext JavaDoc();
115          Context JavaDoc enc = (Context JavaDoc) ic.lookup("java:comp/env");
116          Object JavaDoc ref = enc.lookup("ejb/CalledSessionLocalHome");
117          CalledSessionLocalHome home = (CalledSessionLocalHome) PortableRemoteObject.narrow(ref,
118             CalledSessionLocalHome.class);
119          CalledSessionLocal bean = home.create();
120          bean.callLocalEcho("callEcho#2");
121          log.debug("echo, callee.callLocalEcho#1");
122          bean.callLocalEcho("callEcho#2");
123          log.debug("echo, callee.callLocalEcho#2");
124       }
125       catch (Exception JavaDoc e)
126       {
127          log.error("Failed to invoke Callee.invokeEcho", e);
128          throw new EJBException JavaDoc("Failed to invoke Callee.invokeEcho", e);
129       }
130
131    }
132
133    public void noop()
134    {
135       log.debug("noop");
136    }
137
138 }
139
Popular Tags