001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2007, 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.ejb.SessionSynchronization;
031: import javax.naming.InitialContext;
032: import javax.rmi.PortableRemoteObject;
033:
034: import org.apache.log4j.Logger;
035: import org.jboss.test.security.interfaces.StatefulSession;
036: import org.jboss.test.security.interfaces.StatefulSessionHome;
037:
038: /**
039: * SFSB that has the session synchronization methods that invoke
040: * the getCallerPrincipal (Reference: JBAS-4087)
041: *
042: * @author Anil.Saldhana@redhat.com
043: * @version $Revision: 57211 $
044: */
045: public class SFSBTxSynchronizationBean implements SessionBean,
046: SessionSynchronization {
047: private static final long serialVersionUID = 1L;
048:
049: private static Logger log = Logger
050: .getLogger(SFSBTxSynchronizationBean.class);
051: private SessionContext sessionContext;
052: private String state;
053:
054: public void ejbCreate(String state) throws CreateException {
055: this .state = state;
056: log.debug("ejbCreate(" + state + ") called");
057: Principal p = sessionContext.getCallerPrincipal();
058: log.debug("ejbCreate, callerPrincipal=" + p);
059: }
060:
061: public void ejbActivate() {
062: log.debug("ejbActivate() called");
063: }
064:
065: public void ejbPassivate() {
066: log.debug("ejbPassivate() called");
067: }
068:
069: public void ejbRemove() {
070: log.debug("ejbRemove() called");
071: }
072:
073: public void setSessionContext(SessionContext context) {
074: sessionContext = context;
075: }
076:
077: public String echo(String arg) {
078: log.debug("echo, arg=" + arg);
079: Principal p = sessionContext.getCallerPrincipal();
080: log.debug("echo, callerPrincipal=" + p);
081:
082: //Now check whether we are able to call the bean with run-as role
083: try {
084: InitialContext jndiContext = new InitialContext();
085: Object obj = jndiContext
086: .lookup("java:comp/env/ejb/RunAsSFSB");
087: obj = PortableRemoteObject.narrow(obj,
088: StatefulSessionHome.class);
089: StatefulSessionHome home = (StatefulSessionHome) obj;
090: log.debug("Found StatefulSessionHome");
091: // The create should be allowed to call getCallerPrincipal
092: StatefulSession bean = home
093: .create("testStatefulCreateCaller");
094: // Need to invoke a method to ensure an ejbCreate call
095: bean.echo("testStatefulCreateCaller");
096: } catch (Exception e) {
097: throw new RuntimeException(e);
098: }
099: return arg;
100: }
101:
102: public void afterBegin() throws EJBException, RemoteException {
103: Principal p = sessionContext.getCallerPrincipal();
104: log.error("afterBegin():callerPrincipal=" + p);
105: }
106:
107: public void afterCompletion(boolean committed) throws EJBException,
108: RemoteException {
109: Principal p = sessionContext.getCallerPrincipal();
110: log.error("afterCompletion:callerPrincipal=" + p);
111: }
112:
113: public void beforeCompletion() throws EJBException, RemoteException {
114: Principal p = sessionContext.getCallerPrincipal();
115: log.error("beforeCompletion():callerPrincipal=" + p);
116: }
117: }
|