KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > security > FacadeTargetBean


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.ejb.EJBException JavaDoc;
29 import javax.ejb.Local JavaDoc;
30 import javax.ejb.Remote JavaDoc;
31 import javax.ejb.SessionContext JavaDoc;
32 import javax.ejb.Stateful 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 @Stateful JavaDoc(name="CallerFacadeTargetSFSB")
45 @Local JavaDoc(org.jboss.ejb3.test.security.CalledSessionLocal.class)
46 @Remote JavaDoc(org.jboss.ejb3.test.security.CalledSession.class)
47 @RemoteBinding(jndiBinding="spec.CallerFacadeTargetSFSB")
48 @SecurityDomain("spec-test")
49 @EJBs JavaDoc({@EJB JavaDoc(name="StatelessSessionLocal", beanInterface=org.jboss.ejb3.test.security.StatelessSessionLocal.class, beanName="CalleeBean")})
50 public class FacadeTargetBean
51 {
52    private static Logger log = Logger.getLogger(FacadeTargetBean.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("Echo");
65       log.debug("echo, isCallerInRole('Echo')=" + isEchoCaller);
66
67       if (isEchoCaller == false)
68          throw new SecurityException JavaDoc("isEchoCaller == false");
69       try
70       {
71          InitialContext JavaDoc ic = new InitialContext JavaDoc();
72          StatelessSessionLocal localBean = (StatelessSessionLocal)ic.lookup(Container.ENC_CTX_NAME + "/env/StatelessSessionLocal");
73          String JavaDoc echo2 = localBean.echo(arg);
74          log.debug("echo#1, callee.echo=" + echo2);
75          echo2 = localBean.echo(arg);
76          log.debug("echo#2, callee.echo=" + echo2);
77       }
78       catch (Exception JavaDoc e)
79       {
80          log.error("Failed to invoke Callee.echo", e);
81          throw new EJBException JavaDoc("Failed to invoke Callee.echo", e);
82       }
83
84       isEchoCaller = sessionContext.isCallerInRole("Echo");
85       log.debug("echo, post calls isCallerInRole('Echo')=" + isEchoCaller);
86       if (isEchoCaller == false)
87          throw new SecurityException JavaDoc("isEchoCaller == false post calls");
88
89       return arg;
90    }
91
92    /**
93     * Unused
94     */

95    public String JavaDoc callLocalEcho(String JavaDoc arg)
96    {
97       return arg;
98    }
99
100 }
101
Popular Tags