KickJava   Java API By Example, From Geeks To Geeks.

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


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.ejb.EJB JavaDoc;
33 import javax.ejb.EJBs JavaDoc;
34 import javax.annotation.Resource;
35 import javax.annotation.security.RolesAllowed;
36
37 import org.jboss.logging.Logger;
38 import org.jboss.annotation.ejb.RemoteBinding;
39 import org.jboss.annotation.security.SecurityDomain;
40 import org.jboss.ejb3.Container;
41
42 /**
43  * A simple session bean that calls the CalleeBean
44  * @author Scott.Stark@jboss.org
45  * @version $Revision: 58110 $
46  */

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

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

103    public String JavaDoc callEcho()
104    {
105       try
106       {
107          InitialContext JavaDoc ic = new InitialContext JavaDoc();
108        
109          javax.naming.NamingEnumeration JavaDoc list = ic.list(Container.ENC_CTX_NAME + "/env/ejb");
110      
111          CalledSession bean = (CalledSession)ic.lookup(Container.ENC_CTX_NAME + "/env/ejb/Caller");
112          String JavaDoc echo = bean.invokeEcho("Level2");
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