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.CalledSessionLocalHome;
035: import org.jboss.test.security.interfaces.CalledSessionLocal;
036:
037: /**
038: * A simple session bean that calls the CalleeBean
039: * @author Scott.Stark@jboss.org
040: * @version $Revision: 57211 $
041: */
042: public class CallerFacadeBean implements SessionBean {
043: private static Category log = Category
044: .getInstance(CallerFacadeBean.class);
045: private SessionContext sessionContext;
046:
047: public void ejbCreate() throws CreateException {
048: log.debug("ejbCreate() called");
049: }
050:
051: public void ejbActivate() {
052: log.debug("ejbActivate() called");
053: }
054:
055: public void ejbPassivate() {
056: log.debug("ejbPassivate() called");
057: }
058:
059: public void ejbRemove() {
060: log.debug("ejbRemove() called");
061: }
062:
063: public void setSessionContext(SessionContext context) {
064: sessionContext = context;
065: }
066:
067: /**
068: * This method calls echo on a StatelessSessionLocal and asserts that the
069: * caller is in the EchoCaller role.
070: */
071: public String invokeEcho(String arg) {
072: log.debug("echo, arg=" + arg);
073: Principal p = sessionContext.getCallerPrincipal();
074: log.debug("echo, callerPrincipal=" + p);
075: boolean isEchoCaller = sessionContext
076: .isCallerInRole("EchoCaller");
077: log.debug("echo, isCallerInRole('EchoCaller')=" + isEchoCaller);
078: boolean isInternalRole = sessionContext
079: .isCallerInRole("InternalRole");
080: log.debug("echo, isCallerInRole('InternalRole')="
081: + isInternalRole);
082:
083: try {
084: InitialContext ic = new InitialContext();
085: Context enc = (Context) ic.lookup("java:comp/env");
086: Object ref = enc.lookup("ejb/CalledSessionLocalHome");
087: CalledSessionLocalHome home = (CalledSessionLocalHome) PortableRemoteObject
088: .narrow(ref, CalledSessionLocalHome.class);
089: CalledSessionLocal localBean = home.create();
090: String echo2 = localBean.invokeEcho(arg);
091: log.debug("echo#1, callee.invokeEcho=" + echo2);
092: echo2 = localBean.invokeEcho(arg);
093: log.debug("echo#2, callee.invokeEcho=" + echo2);
094: } catch (Exception e) {
095: log.error("Failed to invoke CalledSession.invokeEcho", e);
096: throw new EJBException("Failed to invoke Callee.echo", e);
097: }
098: return arg;
099: }
100:
101: /**
102: * This method should call invokeEcho on another CalledSession
103: */
104: public void callEcho() {
105: try {
106: InitialContext ic = new InitialContext();
107: Context enc = (Context) ic.lookup("java:comp/env");
108: Object ref = enc.lookup("ejb/CalledSessionLocalHome");
109: CalledSessionLocalHome home = (CalledSessionLocalHome) PortableRemoteObject
110: .narrow(ref, CalledSessionLocalHome.class);
111: CalledSessionLocal bean = home.create();
112: bean.callLocalEcho("callEcho#2");
113: log.debug("echo, callee.callLocalEcho#1");
114: bean.callLocalEcho("callEcho#2");
115: log.debug("echo, callee.callLocalEcho#2");
116: } catch (Exception e) {
117: log.error("Failed to invoke Callee.invokeEcho", e);
118: throw new EJBException(
119: "Failed to invoke Callee.invokeEcho", e);
120: }
121:
122: }
123:
124: public void noop() {
125: log.debug("noop");
126: }
127:
128: }
|