KickJava   Java API By Example, From Geeks To Geeks.

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


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.EJBException JavaDoc;
26 import javax.ejb.Remote JavaDoc;
27 import javax.ejb.SessionContext JavaDoc;
28 import javax.ejb.Stateless JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31
32 import javax.annotation.Resource;
33 import javax.annotation.security.RolesAllowed;
34 import javax.annotation.security.RunAs;
35
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="CallerBean")
47 @Remote JavaDoc(CalledSession.class)
48 @RemoteBinding(jndiBinding = "spec.CallerBean")
49 @RunAs("InternalRole")
50 @RolesAllowed({"Echo"})
51 @SecurityDomain("spec-test")
52 public class CallerBean implements CalledSession
53 {
54    private static Logger log = Logger.getLogger(CallerBean.class);
55    @Resource SessionContext JavaDoc sessionContext;
56
57    /**
58     * This method calls echo on a StatelessSessionLocal and asserts that the
59     * caller is in the EchoCaller role.
60     */

61    public String JavaDoc invokeEcho(String JavaDoc arg)
62    {
63       log.info("echo, arg=" + arg);
64       Principal JavaDoc p = sessionContext.getCallerPrincipal();
65       log.info("echo, callerPrincipal=" + p);
66       boolean isEchoCaller = sessionContext.isCallerInRole("Echo");
67       log.info("echo, isCallerInRole('Echo')=" + isEchoCaller);
68       boolean isInternalRole = sessionContext.isCallerInRole("InternalRole");
69       log.info("echo, isCallerInRole('InternalRole')=" + isInternalRole);
70       
71       if (isEchoCaller == false && isInternalRole == false)
72          throw new SecurityException JavaDoc("isEchoCaller == false && isInternalRole == false");
73     
74       try
75       {
76          InitialContext JavaDoc ic = new InitialContext JavaDoc();
77      // StatelessSessionLocal localBean = (StatelessSessionLocal)ic.lookup(Container.ENC_CTX_NAME + "/env/ejb/Callee");
78

79          StatelessSessionLocal localBean = (StatelessSessionLocal)ic.lookup("spec.CalleeBeanRemote");
80          
81          String JavaDoc echo2 = localBean.echo(arg);
82          log.info("echo#1, callee.echo=" + echo2);
83      // echo2 = localBean.echo(arg);
84
// log.info("echo#2, callee.echo=" + echo2);
85
}
86       catch (Exception JavaDoc e)
87       {
88          log.error("Failed to invoke Callee.echo", e);
89          throw new EJBException JavaDoc("Failed to invoke Callee.echo", e);
90       }
91
92       isEchoCaller = sessionContext.isCallerInRole("Echo");
93       log.info("echo, isCallerInRole#2('Echo')=" + isEchoCaller);
94       isInternalRole = sessionContext.isCallerInRole("InternalRole");
95       log.info("echo, isCallerInRole#2('InternalRole')=" + isInternalRole);
96
97       if (isEchoCaller == false && isInternalRole == false)
98          throw new SecurityException JavaDoc("isEchoCaller == false && isInternalRole == false post calls");
99       
100       return arg;
101    }
102
103    /**
104     * This method should call invokeEcho on another CalledSession
105     */

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

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