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.EJBException;
026: import javax.ejb.EntityBean;
027: import javax.ejb.EntityContext;
028: import javax.ejb.CreateException;
029: import javax.naming.InitialContext;
030: import javax.naming.NamingException;
031: import javax.security.auth.Subject;
032:
033: import org.jboss.logging.Logger;
034:
035: /**
036: * A CMP2 entity bean used to test Principal propagation using the echo method.
037: * @author Scott.Stark@jboss.org
038: * @version $Revision: 57211 $
039: */
040: public abstract class Cmp2Bean implements EntityBean {
041: static Logger log = Logger.getLogger(Cmp2Bean.class);
042: private EntityContext context;
043:
044: public void ejbActivate() {
045: }
046:
047: public void ejbPassivate() {
048: }
049:
050: public void ejbRemove() {
051: }
052:
053: public void ejbLoad() {
054: }
055:
056: public void ejbStore() {
057: }
058:
059: public void setEntityContext(EntityContext context) {
060: this .context = context;
061: }
062:
063: public void unsetEntityContext() {
064: this .context = null;
065: }
066:
067: public String ejbCreate(String key) throws CreateException {
068: setKey(key);
069: return null;
070: }
071:
072: public void ejbPostCreate(String key) {
073: }
074:
075: public abstract String getKey();
076:
077: public abstract void setKey(String key);
078:
079: public String echo(String arg) {
080: Principal p = context.getCallerPrincipal();
081: log.debug("EntityBean.echo, callerPrincipal=" + p);
082: // Check the java:comp/env/security/security-domain
083: try {
084: InitialContext ctx = new InitialContext();
085: Object securityMgr = ctx
086: .lookup("java:comp/env/security/security-domain");
087: log
088: .debug("Checking java:comp/env/security/security-domain");
089: if (securityMgr == null)
090: throw new EJBException(
091: "Failed to find security mgr under: java:comp/env/security/security-domain");
092: log.debug("Found SecurityManager: " + securityMgr);
093: Subject activeSubject = (Subject) ctx
094: .lookup("java:comp/env/security/subject");
095: log.debug("ActiveSubject: " + activeSubject);
096: } catch (NamingException e) {
097: log.debug("failed", e);
098: throw new EJBException("Naming exception: "
099: + e.toString(true));
100: }
101: return p.getName();
102: }
103:
104: }
|