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.io.StringWriter;
025: import java.io.PrintWriter;
026: import javax.ejb.EJBException;
027: import javax.ejb.MessageDrivenBean;
028: import javax.ejb.MessageDrivenContext;
029: import javax.jms.Message;
030: import javax.jms.MessageListener;
031: import javax.jms.Queue;
032: import javax.jms.QueueConnection;
033: import javax.jms.QueueConnectionFactory;
034: import javax.jms.QueueSender;
035: import javax.jms.QueueSession;
036: import javax.jms.Session;
037: import javax.naming.InitialContext;
038: import javax.naming.Name;
039: import javax.naming.NameParser;
040: import javax.naming.NamingException;
041:
042: import org.jboss.test.security.interfaces.ProjRepository;
043: import org.jboss.test.security.interfaces.ProjRepositoryHome;
044: import org.jboss.logging.Logger;
045:
046: /** An MDB that invokes several methods on the ProjRepository session bean,
047: each of which require a seperate role to test the assignement of multiple
048: roles to the run-as identity.
049:
050: @author Scott.Stark@jboss.org
051: @version $Revision: 57211 $
052: */
053: public class RunAsWithRolesMDB implements MessageDrivenBean,
054: MessageListener {
055: static Logger log = Logger.getLogger(RunAsWithRolesMDB.class);
056:
057: private MessageDrivenContext ctx = null;
058: private InitialContext iniCtx;
059:
060: public RunAsWithRolesMDB() {
061: }
062:
063: public void setMessageDrivenContext(MessageDrivenContext ctx)
064: throws EJBException {
065: this .ctx = ctx;
066: try {
067: iniCtx = new InitialContext();
068: } catch (NamingException e) {
069: throw new EJBException(e);
070: }
071: }
072:
073: public void ejbCreate() {
074: }
075:
076: public void ejbRemove() {
077: ctx = null;
078: }
079:
080: public void onMessage(Message message) {
081: Queue replyTo = null;
082: try {
083: replyTo = (Queue) message.getJMSReplyTo();
084: String name = message.getStringProperty("name");
085: ProjRepositoryHome home = (ProjRepositoryHome) iniCtx
086: .lookup("java:comp/env/ejb/ProjRepository");
087: NameParser parser = iniCtx.getNameParser("");
088: Name projName = parser.parse(name);
089: // This requires the ProjectAdmin role
090: ProjRepository bean = home.create(projName);
091: // This requires CreateFolder role
092: Name programs = parser.parse("/Programs Files");
093: bean.createFolder(programs);
094: // This requires DeleteFolder role
095: bean.deleteItem(programs);
096: sendReply(replyTo, "Role tests ok");
097: bean.remove();
098: } catch (Throwable e) {
099: log.debug("failed", e);
100: if (replyTo != null) {
101: StringWriter sw = new StringWriter();
102: PrintWriter pw = new PrintWriter(sw);
103: e.printStackTrace(pw);
104: sendReply(replyTo, "Failed, ex=\n" + sw.toString());
105: }
106: }
107: }
108:
109: private void sendReply(Queue replyTo, String info) {
110: try {
111: InitialContext ctx = new InitialContext();
112: QueueConnectionFactory queueFactory = (QueueConnectionFactory) ctx
113: .lookup("java:comp/env/jms/QueFactory");
114: QueueConnection queueConn = queueFactory
115: .createQueueConnection();
116: QueueSession session = queueConn.createQueueSession(false,
117: Session.AUTO_ACKNOWLEDGE);
118: Message msg = session.createMessage();
119: msg.setStringProperty("reply", info);
120: QueueSender sender = session.createSender(replyTo);
121: sender.send(msg);
122: sender.close();
123: session.close();
124: queueConn.close();
125: log.info("Sent reply");
126: } catch (Exception e) {
127: log.error("Failed to send reply", e);
128: }
129: }
130: }
|