KickJava   Java API By Example, From Geeks To Geeks.

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


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
40 /** A SessionBean that access the Entity bean to test Principal
41 identity propagation.
42
43 @author Scott.Stark@jboss.org
44 @version $Revision: 58110 $
45 */

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