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:
037: /**
038: * A simple session bean that calls the CalleeBean
039: * @author Scott.Stark@jboss.org
040: * @version $Revision: 57211 $
041: */
042: public class FacadeTargetBean implements SessionBean {
043: private static Category log = Category
044: .getInstance(FacadeTargetBean.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:
079: if (isEchoCaller == false)
080: throw new SecurityException("isEchoCaller == false");
081: try {
082: InitialContext ic = new InitialContext();
083: Context enc = (Context) ic.lookup("java:comp/env");
084: Object ref = enc
085: .lookup("ejb/local/StatelessSessionLocalHome");
086: StatelessSessionLocalHome localHome = (StatelessSessionLocalHome) PortableRemoteObject
087: .narrow(ref, StatelessSessionLocalHome.class);
088: StatelessSessionLocal localBean = localHome.create();
089: String echo2 = localBean.echo(arg);
090: log.debug("echo#1, callee.echo=" + echo2);
091: echo2 = localBean.echo(arg);
092: log.debug("echo#2, callee.echo=" + echo2);
093: } catch (Exception e) {
094: log.error("Failed to invoke Callee.echo", e);
095: throw new EJBException("Failed to invoke Callee.echo", e);
096: }
097:
098: isEchoCaller = sessionContext.isCallerInRole("EchoCaller");
099: log.debug("echo, post calls isCallerInRole('EchoCaller')="
100: + isEchoCaller);
101: if (isEchoCaller == false)
102: throw new SecurityException(
103: "isEchoCaller == false post calls");
104:
105: return arg;
106: }
107:
108: /**
109: * Unused
110: */
111: public String callLocalEcho(String arg) {
112: return arg;
113: }
114:
115: }
|