KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > ejb > RunAsPropagationMDB


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.security.ejb;
23
24 import javax.ejb.MessageDrivenBean JavaDoc;
25 import javax.ejb.MessageDrivenContext JavaDoc;
26 import javax.ejb.EJBException JavaDoc;
27 import javax.jms.MessageListener JavaDoc;
28 import javax.jms.Message JavaDoc;
29 import javax.jms.Queue JavaDoc;
30 import javax.jms.QueueConnection JavaDoc;
31 import javax.jms.QueueConnectionFactory JavaDoc;
32 import javax.jms.QueueSender JavaDoc;
33 import javax.jms.QueueSession JavaDoc;
34 import javax.jms.Session JavaDoc;
35 import javax.naming.InitialContext JavaDoc;
36 import javax.naming.NamingException JavaDoc;
37
38 import org.jboss.logging.Logger;
39 import org.jboss.test.security.interfaces.CalledSessionLocalHome;
40 import org.jboss.test.security.interfaces.CalledSessionLocal;
41
42 /** An MDB that takes the string from the msg passed to onMessage
43  and invokes the echo(String) method on an internal Entity using
44  the InternalRole assigned in the MDB descriptor run-as element.
45  
46  @author Scott.Stark@jboss.org
47  @version $Revision: 37406 $
48  */

49 public class RunAsPropagationMDB implements MessageDrivenBean JavaDoc, MessageListener JavaDoc
50 {
51    static Logger log = Logger.getLogger(RunAsPropagationMDB.class);
52    
53    private MessageDrivenContext JavaDoc ctx = null;
54    private InitialContext JavaDoc iniCtx;
55    
56    public RunAsPropagationMDB()
57    {
58    }
59
60    public void setMessageDrivenContext(MessageDrivenContext JavaDoc ctx)
61       throws EJBException JavaDoc
62    {
63       this.ctx = ctx;
64       try
65       {
66          iniCtx = new InitialContext JavaDoc();
67       }
68       catch(NamingException JavaDoc e)
69       {
70          throw new EJBException JavaDoc(e);
71       }
72    }
73    
74    public void ejbCreate()
75    {
76    }
77    
78    public void ejbRemove()
79    {
80       ctx = null;
81    }
82
83    public void onMessage(Message JavaDoc message)
84    {
85       Queue JavaDoc replyTo = null;
86       try
87       {
88          replyTo = (Queue JavaDoc) message.getJMSReplyTo();
89          String JavaDoc arg = message.getStringProperty("arg");
90          CalledSessionLocalHome home = (CalledSessionLocalHome) iniCtx.lookup("java:comp/env/ejb/CalledSessionLocalHome");
91          CalledSessionLocal bean = home.create();
92          String JavaDoc echo = bean.callLocalEcho(arg);
93          log.info("echo("+arg+") -> "+echo);
94          sendReply(replyTo, arg);
95       }
96       catch(Throwable JavaDoc e)
97       {
98          log.debug("failed", e);
99          if( replyTo != null )
100             sendReply(replyTo, "Failed, ex="+e.getMessage());
101       }
102    }
103    private void sendReply(Queue JavaDoc replyTo, String JavaDoc info)
104    {
105       try
106       {
107          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
108          QueueConnectionFactory JavaDoc queueFactory = (QueueConnectionFactory JavaDoc) ctx.lookup("java:comp/env/jms/QueFactory");
109          QueueConnection JavaDoc queueConn = queueFactory.createQueueConnection();
110          QueueSession JavaDoc session = queueConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
111          Message JavaDoc msg = session.createMessage();
112          msg.setStringProperty("reply", info);
113          QueueSender JavaDoc sender = session.createSender(replyTo);
114          sender.send(msg);
115          sender.close();
116          session.close();
117          queueConn.close();
118          log.info("Sent reply");
119       }
120       catch(Exception JavaDoc e)
121       {
122          log.error("Failed to send reply", e);
123       }
124    }
125 }
126
Popular Tags