KickJava   Java API By Example, From Geeks To Geeks.

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


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.annotation.security.RunAs;
30 import javax.ejb.EJBException JavaDoc;
31 import javax.ejb.Remote JavaDoc;
32 import javax.ejb.SessionContext JavaDoc;
33 import javax.ejb.Stateless JavaDoc;
34 import javax.naming.Context JavaDoc;
35 import javax.naming.InitialContext JavaDoc;
36 import org.jboss.logging.Logger;
37 import org.jboss.annotation.ejb.RemoteBinding;
38 import org.jboss.annotation.security.SecurityDomain;
39 import org.jboss.ejb3.Container;
40
41 /**
42  * A simple session bean that calls the CalleeBean
43  * @author Scott.Stark@jboss.org
44  * @version $Revision: 58110 $
45  */

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

63    public String JavaDoc invokeEcho(String JavaDoc arg)
64    {
65       log.info("echo, arg=" + arg);
66       Principal JavaDoc p = sessionContext.getCallerPrincipal();
67       log.info("echo, callerPrincipal=" + p);
68       boolean isEchoCaller = sessionContext.isCallerInRole("Echo");
69       log.info("echo, isCallerInRole('Echo')=" + isEchoCaller);
70       boolean isInternalRole = sessionContext.isCallerInRole("InternalRole");
71       log.info("echo, isCallerInRole('InternalRole')=" + isInternalRole);
72       
73       if (isEchoCaller == false && isInternalRole == false)
74          throw new SecurityException JavaDoc("isEchoCaller == false && isInternalRole == false");
75     
76       try
77       {
78          InitialContext JavaDoc ic = new InitialContext JavaDoc();
79          StatelessSessionLocal localBean = (StatelessSessionLocal)ic.lookup(Container.ENC_CTX_NAME + "/env/Callee");
80          String JavaDoc echo2 = localBean.echo(arg);
81          log.info("echo#1, callee.echo=" + echo2);
82          echo2 = localBean.echo(arg);
83          log.info("echo#2, callee.echo=" + echo2);
84       }
85       catch (Exception JavaDoc e)
86       {
87          log.error("Failed to invoke Callee.echo", e);
88          throw new EJBException JavaDoc("Failed to invoke Callee.echo", e);
89       }
90
91       isEchoCaller = sessionContext.isCallerInRole("Echo");
92       log.info("echo, isCallerInRole#2('Echo')=" + isEchoCaller);
93       isInternalRole = sessionContext.isCallerInRole("InternalRole");
94       log.info("echo, isCallerInRole#2('InternalRole')=" + isInternalRole);
95
96       if (isEchoCaller == false && isInternalRole == false)
97          throw new SecurityException JavaDoc("isEchoCaller == false && isInternalRole == false post calls");
98       
99       return arg;
100    }
101
102    /**
103     * This method should call invokeEcho on another CalledSession
104     */

105    public String JavaDoc callEcho()
106    {
107       try
108       {
109          InitialContext JavaDoc ic = new InitialContext JavaDoc();
110        
111          CalledSession bean = (CalledSession)ic.lookup(Container.ENC_CTX_NAME + "/env/Caller2");
112          String JavaDoc echo = bean.invokeEcho("Level1");
113          log.info("echo, callee.invokeEcho=" + echo);
114          
115          String JavaDoc principal = sessionContext.getCallerPrincipal().getName();
116          return principal;
117       }
118       catch (Exception JavaDoc e)
119       {
120          log.error("Failed to invoke Callee.invokeEcho", e);
121          throw new EJBException JavaDoc("Failed to invoke Callee.invokeEcho", e);
122       }
123
124    }
125
126    /**
127     * This method should call invokeEcho on a CalledSession
128     */

129    public String JavaDoc callLocalEcho(String JavaDoc arg)
130    {
131       try
132       {
133          InitialContext JavaDoc ic = new InitialContext JavaDoc();
134          Context JavaDoc enc = (Context JavaDoc) ic.lookup("java:comp/env");
135          CalledSessionLocal bean = (CalledSessionLocal)enc.lookup("ejb/Caller");
136          String JavaDoc echo2 = bean.invokeEcho(arg + "Level1");
137          log.info("echo, callee.invokeEcho=" + echo2);
138          return echo2;
139       }
140       catch (Exception JavaDoc e)
141       {
142          log.error("Failed to invoke Callee.invokeEcho", e);
143          throw new EJBException JavaDoc("Failed to invoke Callee.invokeEcho", e);
144       }
145    }
146
147    public void noop()
148    {
149       log.info("noop");
150    }
151
152 }
153
Popular Tags