KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > ejb > CallerBean


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.test.security.ejb;
23
24 import java.security.Principal JavaDoc;
25 import javax.ejb.CreateException JavaDoc;
26 import javax.ejb.EJBException JavaDoc;
27 import javax.ejb.SessionBean JavaDoc;
28 import javax.ejb.SessionContext JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31 import javax.rmi.PortableRemoteObject JavaDoc;
32
33 import org.jboss.logging.Logger;
34 import org.jboss.test.security.interfaces.StatelessSessionLocal;
35 import org.jboss.test.security.interfaces.StatelessSessionLocalHome;
36 import org.jboss.test.security.interfaces.CalledSessionHome;
37 import org.jboss.test.security.interfaces.CalledSession;
38 import org.jboss.test.security.interfaces.CalledSessionLocalHome;
39 import org.jboss.test.security.interfaces.CalledSessionLocal;
40
41 /**
42  * A simple session bean that calls the CalleeBean
43  * @author Scott.Stark@jboss.org
44  * @version $Revision: 58115 $
45  */

46 public class CallerBean implements SessionBean JavaDoc
47 {
48    private static Logger log = Logger.getLogger(CallerBean.class);
49    private SessionContext JavaDoc sessionContext;
50
51    public void ejbCreate() throws CreateException JavaDoc
52    {
53       log.debug("ejbCreate() called");
54    }
55
56    public void ejbActivate()
57    {
58       log.debug("ejbActivate() called");
59    }
60
61    public void ejbPassivate()
62    {
63       log.debug("ejbPassivate() called");
64    }
65
66    public void ejbRemove()
67    {
68       log.debug("ejbRemove() called");
69    }
70
71    public void setSessionContext(SessionContext JavaDoc context)
72    {
73       sessionContext = context;
74    }
75
76    /**
77     * This method calls echo on a StatelessSessionLocal and asserts that the
78     * caller is in the EchoCaller role.
79     */

80    public String JavaDoc invokeEcho(String JavaDoc arg)
81    {
82       log.debug("echo, arg=" + arg);
83       Principal JavaDoc p = sessionContext.getCallerPrincipal();
84       log.debug("echo, callerPrincipal=" + p);
85       boolean isEchoCaller = sessionContext.isCallerInRole("EchoCaller");
86       log.debug("echo, isCallerInRole('EchoCaller')=" + isEchoCaller);
87       boolean isInternalRole = sessionContext.isCallerInRole("InternalRole");
88       log.debug("echo, isCallerInRole('InternalRole')=" + isInternalRole);
89
90       if (isEchoCaller == false && isInternalRole == false)
91          throw new SecurityException JavaDoc("isEchoCaller == false && isInternalRole == false");
92       try
93       {
94          InitialContext JavaDoc ic = new InitialContext JavaDoc();
95          Context JavaDoc enc = (Context JavaDoc) ic.lookup("java:comp/env");
96          Object JavaDoc ref = enc.lookup("ejb/local/CalleeHome");
97          StatelessSessionLocalHome localHome = (StatelessSessionLocalHome) PortableRemoteObject.narrow(ref,
98             StatelessSessionLocalHome.class);
99          StatelessSessionLocal localBean = localHome.create();
100          String JavaDoc echo2 = localBean.echo(arg);
101          log.debug("echo#1, callee.echo=" + echo2);
102          echo2 = localBean.echo(arg);
103          log.debug("echo#2, callee.echo=" + echo2);
104       }
105       catch (Exception JavaDoc e)
106       {
107          log.error("Failed to invoke Callee.echo", e);
108          throw new EJBException JavaDoc("Failed to invoke Callee.echo", e);
109       }
110
111       isEchoCaller = sessionContext.isCallerInRole("EchoCaller");
112       log.debug("echo, isCallerInRole#2('EchoCaller')=" + isEchoCaller);
113       isInternalRole = sessionContext.isCallerInRole("InternalRole");
114       log.debug("echo, isCallerInRole#2('InternalRole')=" + isInternalRole);
115
116       if (isEchoCaller == false && isInternalRole == false)
117          throw new SecurityException JavaDoc("isEchoCaller == false && isInternalRole == false post calls");
118
119       return arg;
120    }
121
122    /**
123     * This method should call invokeEcho on another CalledSession
124     */

125    public void callEcho()
126    {
127       try
128       {
129          InitialContext JavaDoc ic = new InitialContext JavaDoc();
130          Context JavaDoc enc = (Context JavaDoc) ic.lookup("java:comp/env");
131          Object JavaDoc ref = enc.lookup("ejb/CallerHome");
132          CalledSessionHome home = (CalledSessionHome) PortableRemoteObject.narrow(ref,
133             CalledSessionHome.class);
134          CalledSession bean = home.create();
135          String JavaDoc echo = bean.invokeEcho("Level1");
136          log.debug("echo, callee.invokeEcho=" + echo);
137       }
138       catch (Exception JavaDoc e)
139       {
140          log.error("Failed to invoke Callee.invokeEcho", e);
141          throw new EJBException JavaDoc("Failed to invoke Callee.invokeEcho", e);
142       }
143
144    }
145
146    /**
147     * This method should call invokeEcho on a CalledSession
148     */

149    public String JavaDoc callLocalEcho(String JavaDoc arg)
150    {
151       try
152       {
153          InitialContext JavaDoc ic = new InitialContext JavaDoc();
154          Context JavaDoc enc = (Context JavaDoc) ic.lookup("java:comp/env");
155          Object JavaDoc ref = enc.lookup("ejb/CallerHome");
156          CalledSessionLocalHome home = (CalledSessionLocalHome) PortableRemoteObject.narrow(ref,
157             CalledSessionLocalHome.class);
158          CalledSessionLocal bean = home.create();
159          String JavaDoc echo2 = bean.invokeEcho(arg + "Level1");
160          log.debug("echo, callee.invokeEcho=" + echo2);
161          return echo2;
162       }
163       catch (Exception JavaDoc e)
164       {
165          log.error("Failed to invoke Callee.invokeEcho", e);
166          throw new EJBException JavaDoc("Failed to invoke Callee.invokeEcho", e);
167       }
168    }
169
170    public void noop()
171    {
172       log.debug("noop");
173    }
174
175 }
176
Popular Tags