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 javax.ejb.MessageDrivenBean;
025: import javax.ejb.MessageDrivenContext;
026: import javax.ejb.EJBException;
027: import javax.jms.MessageListener;
028: import javax.jms.Message;
029: import javax.jms.Queue;
030: import javax.jms.QueueConnection;
031: import javax.jms.QueueConnectionFactory;
032: import javax.jms.QueueSender;
033: import javax.jms.QueueSession;
034: import javax.jms.Session;
035: import javax.naming.InitialContext;
036: import javax.naming.NamingException;
037:
038: import org.jboss.logging.Logger;
039: import org.jboss.test.security.interfaces.CalledSessionLocalHome;
040: import org.jboss.test.security.interfaces.CalledSessionLocal;
041:
042: /** An MDB that takes the string from the msg passed to onMessage
043: and invokes the echo(String) method on an internal Entity using
044: the InternalRole assigned in the MDB descriptor run-as element.
045:
046: @author Scott.Stark@jboss.org
047: @version $Revision: 57211 $
048: */
049: public class RunAsPropagationMDB implements MessageDrivenBean,
050: MessageListener {
051: static Logger log = Logger.getLogger(RunAsPropagationMDB.class);
052:
053: private MessageDrivenContext ctx = null;
054: private InitialContext iniCtx;
055:
056: public RunAsPropagationMDB() {
057: }
058:
059: public void setMessageDrivenContext(MessageDrivenContext ctx)
060: throws EJBException {
061: this .ctx = ctx;
062: try {
063: iniCtx = new InitialContext();
064: } catch (NamingException e) {
065: throw new EJBException(e);
066: }
067: }
068:
069: public void ejbCreate() {
070: }
071:
072: public void ejbRemove() {
073: ctx = null;
074: }
075:
076: public void onMessage(Message message) {
077: Queue replyTo = null;
078: try {
079: replyTo = (Queue) message.getJMSReplyTo();
080: String arg = message.getStringProperty("arg");
081: CalledSessionLocalHome home = (CalledSessionLocalHome) iniCtx
082: .lookup("java:comp/env/ejb/CalledSessionLocalHome");
083: CalledSessionLocal bean = home.create();
084: String echo = bean.callLocalEcho(arg);
085: log.info("echo(" + arg + ") -> " + echo);
086: sendReply(replyTo, arg);
087: } catch (Throwable e) {
088: log.debug("failed", e);
089: if (replyTo != null)
090: sendReply(replyTo, "Failed, ex=" + e.getMessage());
091: }
092: }
093:
094: private void sendReply(Queue replyTo, String info) {
095: try {
096: InitialContext ctx = new InitialContext();
097: QueueConnectionFactory queueFactory = (QueueConnectionFactory) ctx
098: .lookup("java:comp/env/jms/QueFactory");
099: QueueConnection queueConn = queueFactory
100: .createQueueConnection();
101: QueueSession session = queueConn.createQueueSession(false,
102: Session.AUTO_ACKNOWLEDGE);
103: Message msg = session.createMessage();
104: msg.setStringProperty("reply", info);
105: QueueSender sender = session.createSender(replyTo);
106: sender.send(msg);
107: sender.close();
108: session.close();
109: queueConn.close();
110: log.info("Sent reply");
111: } catch (Exception e) {
112: log.error("Failed to send reply", e);
113: }
114: }
115: }
|