KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > security > 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.ejb3.test.security;
23
24 import java.security.Principal JavaDoc;
25 import javax.ejb.EJB JavaDoc;
26 import javax.ejb.EJBs JavaDoc;
27 import javax.annotation.Resource;
28 import javax.annotation.security.RolesAllowed;
29 import javax.ejb.EJBException JavaDoc;
30 import javax.ejb.Remote JavaDoc;
31 import javax.ejb.SessionContext JavaDoc;
32 import javax.ejb.Stateless JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import org.jboss.logging.Logger;
35 import org.jboss.annotation.ejb.RemoteBinding;
36 import org.jboss.annotation.security.SecurityDomain;
37 import org.jboss.ejb3.Container;
38
39 /**
40  * A simple session bean that calls the CalleeBean
41  * @author Scott.Stark@jboss.org
42  * @version $Revision: 58110 $
43  */

44 @Stateless JavaDoc(name="CallerFacadeBean-testRunAsSFSB")
45 @Remote JavaDoc(org.jboss.ejb3.test.security.CalledSession.class)
46 @RemoteBinding(jndiBinding = "spec.CallerFacadeBean-testRunAsSFSB")
47 @SecurityDomain("spec-test")
48 @RolesAllowed({"Echo"})
49 @EJBs JavaDoc({@EJB JavaDoc(name="CalledSessionLocal", beanInterface=org.jboss.ejb3.test.security.CalledSessionLocal.class, beanName="CallerFacadeTargetSFSB")})
50 public class CallerFacadeBean
51 {
52    private static Logger log = Logger.getLogger(CallerFacadeBean.class);
53    @Resource SessionContext JavaDoc sessionContext;
54
55    /**
56     * This method calls echo on a StatelessSessionLocal and asserts that the
57     * caller is in the EchoCaller role.
58     */

59    public String JavaDoc invokeEcho(String JavaDoc arg)
60    {
61       log.debug("echo, arg=" + arg);
62       Principal JavaDoc p = sessionContext.getCallerPrincipal();
63       log.debug("echo, callerPrincipal=" + p);
64       boolean isEchoCaller = sessionContext.isCallerInRole("EchoCaller");
65       log.debug("echo, isCallerInRole('EchoCaller')=" + isEchoCaller);
66       boolean isInternalRole = sessionContext.isCallerInRole("InternalRole");
67       log.debug("echo, isCallerInRole('InternalRole')=" + isInternalRole);
68
69       try
70       {
71          InitialContext JavaDoc ic = new InitialContext JavaDoc();
72          CalledSessionLocal localBean = (CalledSessionLocal)ic.lookup(Container.ENC_CTX_NAME + "/env/CalledSessionLocal");
73          String JavaDoc echo2 = localBean.invokeEcho(arg);
74          log.debug("echo#1, callee.invokeEcho=" + echo2);
75          echo2 = localBean.invokeEcho(arg);
76          log.debug("echo#2, callee.invokeEcho=" + echo2);
77       }
78       catch (Exception JavaDoc e)
79       {
80          e.printStackTrace();
81          log.error("Failed to invoke CalledSession.invokeEcho", e);
82          throw new EJBException JavaDoc("Failed to invoke Callee.echo", e);
83       }
84       return arg;
85    }
86
87    /**
88     * This method should call invokeEcho on another CalledSession
89     */

90    public void callEcho()
91    {
92       try
93       {
94          InitialContext JavaDoc ic = new InitialContext JavaDoc();
95          CalledSessionLocal bean = (CalledSessionLocal)ic.lookup(Container.ENC_CTX_NAME + "/env/CalledSessionLocal");
96          bean.callLocalEcho("callEcho#2");
97          log.debug("echo, callee.callLocalEcho#1");
98          bean.callLocalEcho("callEcho#2");
99          log.debug("echo, callee.callLocalEcho#2");
100       }
101       catch (Exception JavaDoc e)
102       {
103          log.error("Failed to invoke Callee.invokeEcho", e);
104          throw new EJBException JavaDoc("Failed to invoke Callee.invokeEcho", e);
105       }
106
107    }
108
109    public void noop()
110    {
111       log.debug("noop");
112    }
113
114 }
115
Popular Tags