KickJava   Java API By Example, From Geeks To Geeks.

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


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.DenyAll;
35 import javax.annotation.security.RolesAllowed;
36
37 import org.jboss.annotation.ejb.RemoteBinding;
38 import org.jboss.annotation.security.SecurityDomain;
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="StatelessSessionInDomain")
48 @Remote JavaDoc(org.jboss.ejb3.test.security.StatelessSession.class)
49 @RemoteBinding(jndiBinding = "spec.StatelessSessionInDomain")
50 @SecurityDomain("spec-test-domain")
51 public class StatelessSessionBeanInDomain
52 {
53    private static final Logger log = Logger
54    .getLogger(StatelessSessionBean3.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  // e.fillInStackTrace();
83
throw new EJBException JavaDoc("Stateful.echo failed", e);
84         }
85         
86         return echo;
87     }
88
89     public String JavaDoc forward(String JavaDoc echoArg)
90     {
91         log.info("forward, echoArg="+echoArg);
92         String JavaDoc echo = null;
93         try
94         {
95             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
96             StatelessSession bean = (StatelessSession)ctx.lookup("java:comp/env/ejb/Session");
97             echo = bean.echo(echoArg);
98         }
99         catch(Exception JavaDoc e)
100         {
101             log.info("StatelessSession.echo failed", e);
102   // e.fillInStackTrace();
103
throw new EJBException JavaDoc("StatelessSession.echo failed", e);
104         }
105         return echo;
106     }
107
108     public void noop()
109     {
110         log.info("noop");
111     }
112
113     public void npeError()
114     {
115         log.info("npeError");
116         Object JavaDoc obj = null;
117         obj.toString();
118     }
119     public void unchecked()
120     {
121         Principal JavaDoc p = sessionContext.getCallerPrincipal();
122         log.info("StatelessSessionBean.unchecked, callerPrincipal="+p);
123     }
124
125     @DenyAll
126     public void excluded()
127     {
128         throw new EJBException JavaDoc("StatelessSessionBean.excluded, no access should be allowed");
129     }
130 }
131
Popular Tags