KickJava   Java API By Example, From Geeks To Geeks.

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


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.annotation.ejb.RemoteBinding;
35 import org.jboss.annotation.security.SecurityDomain;
36 import org.jboss.ejb3.Container;
37 import org.jboss.logging.Logger;
38
39 /** A SessionBean that access the Entity bean to test Principal
40 identity propagation.
41
42 @author Scott.Stark@jboss.org
43 @version $Revision: 46096 $
44 */

45 @Stateless JavaDoc(name="StatelessSession2")
46 @Remote JavaDoc(org.jboss.ejb3.test.security.StatelessSession.class)
47 @RemoteBinding(jndiBinding = "spec.StatelessSession2")
48 @RolesAllowed({"Echo"})
49 @SecurityDomain("spec-test")
50 @EJBs JavaDoc({@EJB JavaDoc(name="Session", beanInterface=org.jboss.ejb3.test.security.StatelessSession.class, beanName="StatelessSession")})
51 public class StatelessSessionBean2
52 {
53    private static final Logger log = Logger
54    .getLogger(StatelessSessionBean2.class);
55    
56    @Resource SessionContext JavaDoc sessionContext;
57     
58     public void testGetBusinessObject()
59     {
60        StatelessSession ss = (StatelessSession)sessionContext.getBusinessObject(org.jboss.ejb3.test.security.StatelessSession.class);
61        ss.noop();
62     }
63
64     public String JavaDoc echo(String JavaDoc arg)
65     {
66         log.info("echo, arg="+arg);
67  
68         // This call should fail if the bean is not secured
69
Principal JavaDoc p = sessionContext.getCallerPrincipal();
70         log.info("echo, callerPrincipal="+p);
71         
72         String JavaDoc echo = null;
73         try
74         {
75             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
76             StatefulSession bean = (StatefulSession) ctx.lookup("spec.StatefulSession");
77             echo = bean.echo(arg);
78         }
79         catch(Exception JavaDoc e)
80         {
81             throw new EJBException JavaDoc("Stateful.echo failed", e);
82         }
83         
84         return echo;
85     }
86
87     public String JavaDoc forward(String JavaDoc echoArg)
88     {
89         log.info("forward, echoArg="+echoArg);
90         String JavaDoc echo = null;
91         try
92         {
93             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
94             StatelessSession bean = (StatelessSession)ctx.lookup(Container.ENC_CTX_NAME + "/env/ejb/Session");
95             echo = bean.echo(echoArg);
96         }
97         catch(Exception JavaDoc e)
98         {
99             log.info("StatelessSession.echo failed", e);
100             throw new EJBException JavaDoc("StatelessSession.echo failed", e);
101         }
102         return echo;
103     }
104
105     public void noop()
106     {
107         log.info("noop");
108     }
109
110     public void npeError()
111     {
112         log.info("npeError");
113         Object JavaDoc obj = null;
114         obj.toString();
115     }
116     public void unchecked()
117     {
118         Principal JavaDoc p = sessionContext.getCallerPrincipal();
119         log.info("StatelessSessionBean.unchecked, callerPrincipal="+p);
120     }
121
122     public void excluded()
123     {
124         throw new EJBException JavaDoc("StatelessSessionBean.excluded, no access should be allowed");
125     }
126 }
127
Popular Tags