KickJava   Java API By Example, From Geeks To Geeks.

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


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
26 import javax.annotation.Resource;
27 import javax.ejb.EJBException JavaDoc;
28 import javax.ejb.SessionContext JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30
31 import javax.ejb.Remote JavaDoc;
32 import javax.ejb.Stateless JavaDoc;
33
34 import javax.annotation.security.RolesAllowed;
35
36 import org.jboss.annotation.ejb.RemoteBinding;
37 import org.jboss.annotation.security.SecurityDomain;
38
39 import org.jboss.logging.Logger;
40
41 /** A SessionBean that access the Entity bean to test Principal
42 identity propagation.
43
44 @author Scott.Stark@jboss.org
45 @version $Revision: 45758 $
46 */

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