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.rmi.RemoteException;
025: import java.security.Principal;
026: import javax.ejb.CreateException;
027: import javax.ejb.EJBException;
028: import javax.ejb.SessionBean;
029: import javax.ejb.SessionContext;
030: import javax.naming.InitialContext;
031:
032: import org.jboss.test.security.interfaces.Entity;
033: import org.jboss.test.security.interfaces.EntityHome;
034: import org.jboss.test.security.interfaces.StatelessSession;
035: import org.jboss.test.security.interfaces.StatelessSessionHome;
036:
037: /** A SessionBean that access the Entity bean to test Principal
038: identity propagation.
039:
040: @author Scott.Stark@jboss.org
041: @version $Revision: 57211 $
042: */
043: public class StatelessSessionBean2 implements SessionBean {
044: org.apache.log4j.Category log = org.apache.log4j.Category
045: .getInstance(getClass());
046:
047: private SessionContext sessionContext;
048:
049: public void ejbCreate() throws RemoteException, CreateException {
050: log.debug("ejbCreate() called");
051: }
052:
053: public void ejbActivate() throws RemoteException {
054: log.debug("ejbActivate() called");
055: }
056:
057: public void ejbPassivate() throws RemoteException {
058: log.debug("ejbPassivate() called");
059: }
060:
061: public void ejbRemove() throws RemoteException {
062: log.debug("ejbRemove() called");
063: }
064:
065: public void setSessionContext(SessionContext context)
066: throws RemoteException {
067: sessionContext = context;
068: }
069:
070: public String echo(String arg) {
071: log.debug("echo, arg=" + arg);
072: // This call should fail if the bean is not secured
073: Principal p = sessionContext.getCallerPrincipal();
074: log.debug("echo, callerPrincipal=" + p);
075: String echo = null;
076: try {
077: InitialContext ctx = new InitialContext();
078: EntityHome home = (EntityHome) ctx
079: .lookup("java:comp/env/ejb/Entity");
080: Entity bean = home.findByPrimaryKey(arg);
081: echo = bean.echo(arg);
082: } catch (Exception e) {
083: log.debug("Entity.echo failed", e);
084: e.fillInStackTrace();
085: throw new EJBException("Entity.echo failed", e);
086: }
087: return echo;
088: }
089:
090: public String forward(String echoArg) {
091: log.debug("forward, echoArg=" + echoArg);
092: String echo = null;
093: try {
094: InitialContext ctx = new InitialContext();
095: StatelessSessionHome home = (StatelessSessionHome) ctx
096: .lookup("java:comp/env/ejb/Session");
097: StatelessSession bean = home.create();
098: echo = bean.echo(echoArg);
099: } catch (Exception e) {
100: log.debug("StatelessSession.echo failed", e);
101: e.fillInStackTrace();
102: throw new EJBException("StatelessSession.echo failed", e);
103: }
104: return echo;
105: }
106:
107: public void noop() {
108: log.debug("noop");
109: }
110:
111: public void npeError() {
112: log.debug("npeError");
113: Object obj = null;
114: obj.toString();
115: }
116:
117: public void unchecked() {
118: Principal p = sessionContext.getCallerPrincipal();
119: log.debug("StatelessSessionBean.unchecked, callerPrincipal="
120: + p);
121: }
122:
123: public void excluded() {
124: throw new EJBException(
125: "StatelessSessionBean.excluded, no access should be allowed");
126: }
127: }
|