001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.security.ejb;
023:
024: import java.security.Principal;
025: import javax.ejb.CreateException;
026: import javax.ejb.EJBException;
027: import javax.ejb.SessionBean;
028: import javax.ejb.SessionContext;
029: import javax.naming.Context;
030: import javax.naming.InitialContext;
031: import javax.rmi.PortableRemoteObject;
032:
033: import org.apache.log4j.Category;
034: import org.jboss.test.security.interfaces.StatelessSessionLocal;
035: import org.jboss.test.security.interfaces.StatelessSessionLocalHome;
036: import org.jboss.test.security.interfaces.CalledSessionHome;
037: import org.jboss.test.security.interfaces.CalledSession;
038: import org.jboss.test.security.interfaces.CalledSessionLocalHome;
039: import org.jboss.test.security.interfaces.CalledSessionLocal;
040:
041: /**
042: * A simple session bean that calls the CalleeBean
043: * @author Scott.Stark@jboss.org
044: * @version $Revision: 57211 $
045: */
046: public class CallerBean implements SessionBean {
047: private static Category log = Category
048: .getInstance(CallerBean.class);
049: private SessionContext sessionContext;
050:
051: public void ejbCreate() throws CreateException {
052: log.debug("ejbCreate() called");
053: }
054:
055: public void ejbActivate() {
056: log.debug("ejbActivate() called");
057: }
058:
059: public void ejbPassivate() {
060: log.debug("ejbPassivate() called");
061: }
062:
063: public void ejbRemove() {
064: log.debug("ejbRemove() called");
065: }
066:
067: public void setSessionContext(SessionContext context) {
068: sessionContext = context;
069: }
070:
071: /**
072: * This method calls echo on a StatelessSessionLocal and asserts that the
073: * caller is in the EchoCaller role.
074: */
075: public String invokeEcho(String arg) {
076: log.debug("echo, arg=" + arg);
077: Principal p = sessionContext.getCallerPrincipal();
078: log.debug("echo, callerPrincipal=" + p);
079: boolean isEchoCaller = sessionContext
080: .isCallerInRole("EchoCaller");
081: log.debug("echo, isCallerInRole('EchoCaller')=" + isEchoCaller);
082: boolean isInternalRole = sessionContext
083: .isCallerInRole("InternalRole");
084: log.debug("echo, isCallerInRole('InternalRole')="
085: + isInternalRole);
086:
087: if (isEchoCaller == false && isInternalRole == false)
088: throw new SecurityException(
089: "isEchoCaller == false && isInternalRole == false");
090: try {
091: InitialContext ic = new InitialContext();
092: Context enc = (Context) ic.lookup("java:comp/env");
093: Object ref = enc.lookup("ejb/local/CalleeHome");
094: StatelessSessionLocalHome localHome = (StatelessSessionLocalHome) PortableRemoteObject
095: .narrow(ref, StatelessSessionLocalHome.class);
096: StatelessSessionLocal localBean = localHome.create();
097: String echo2 = localBean.echo(arg);
098: log.debug("echo#1, callee.echo=" + echo2);
099: echo2 = localBean.echo(arg);
100: log.debug("echo#2, callee.echo=" + echo2);
101: } catch (Exception e) {
102: log.error("Failed to invoke Callee.echo", e);
103: throw new EJBException("Failed to invoke Callee.echo", e);
104: }
105:
106: isEchoCaller = sessionContext.isCallerInRole("EchoCaller");
107: log.debug("echo, isCallerInRole#2('EchoCaller')="
108: + isEchoCaller);
109: isInternalRole = sessionContext.isCallerInRole("InternalRole");
110: log.debug("echo, isCallerInRole#2('InternalRole')="
111: + isInternalRole);
112:
113: if (isEchoCaller == false && isInternalRole == false)
114: throw new SecurityException(
115: "isEchoCaller == false && isInternalRole == false post calls");
116:
117: return arg;
118: }
119:
120: /**
121: * This method should call invokeEcho on another CalledSession
122: */
123: public void callEcho() {
124: try {
125: InitialContext ic = new InitialContext();
126: Context enc = (Context) ic.lookup("java:comp/env");
127: Object ref = enc.lookup("ejb/CallerHome");
128: CalledSessionHome home = (CalledSessionHome) PortableRemoteObject
129: .narrow(ref, CalledSessionHome.class);
130: CalledSession bean = home.create();
131: String echo = bean.invokeEcho("Level1");
132: log.debug("echo, callee.invokeEcho=" + echo);
133: } catch (Exception e) {
134: log.error("Failed to invoke Callee.invokeEcho", e);
135: throw new EJBException(
136: "Failed to invoke Callee.invokeEcho", e);
137: }
138:
139: }
140:
141: /**
142: * This method should call invokeEcho on a CalledSession
143: */
144: public String callLocalEcho(String arg) {
145: try {
146: InitialContext ic = new InitialContext();
147: Context enc = (Context) ic.lookup("java:comp/env");
148: Object ref = enc.lookup("ejb/CallerHome");
149: CalledSessionLocalHome home = (CalledSessionLocalHome) PortableRemoteObject
150: .narrow(ref, CalledSessionLocalHome.class);
151: CalledSessionLocal bean = home.create();
152: String echo2 = bean.invokeEcho(arg + "Level1");
153: log.debug("echo, callee.invokeEcho=" + echo2);
154: return echo2;
155: } catch (Exception e) {
156: log.error("Failed to invoke Callee.invokeEcho", e);
157: throw new EJBException(
158: "Failed to invoke Callee.invokeEcho", e);
159: }
160: }
161:
162: public void noop() {
163: log.debug("noop");
164: }
165:
166: }
|