KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > webservice > transport > jms > DelegatingMessageDrivenBean


1 /**
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.webservice.transport.jms;
8
9 // $Id: DelegatingMessageDrivenBean.java,v 1.1.2.3 2005/04/12 16:07:52 starksm Exp $
10

11 import org.jboss.axis.AxisFault;
12 import org.jboss.axis.ConfigurationException;
13 import org.jboss.axis.EngineConfiguration;
14 import org.jboss.axis.MessageContext;
15 import org.jboss.axis.description.OperationDesc;
16 import org.jboss.axis.description.ServiceDesc;
17 import org.jboss.axis.server.AxisServer;
18 import org.jboss.logging.Logger;
19 import org.jboss.mx.util.MBeanServerLocator;
20 import org.jboss.util.NestedRuntimeException;
21 import org.jboss.webservice.AxisServiceMBean;
22
23 import javax.ejb.EJBException JavaDoc;
24 import javax.ejb.MessageDrivenBean JavaDoc;
25 import javax.ejb.MessageDrivenContext JavaDoc;
26 import javax.jms.BytesMessage JavaDoc;
27 import javax.jms.JMSException JavaDoc;
28 import javax.jms.Message JavaDoc;
29 import javax.jms.MessageListener JavaDoc;
30 import javax.jms.Queue JavaDoc;
31 import javax.jms.QueueConnection JavaDoc;
32 import javax.jms.QueueConnectionFactory JavaDoc;
33 import javax.jms.QueueSender JavaDoc;
34 import javax.jms.QueueSession JavaDoc;
35 import javax.jms.Session JavaDoc;
36 import javax.management.MBeanServer JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38 import javax.xml.namespace.QName JavaDoc;
39 import javax.xml.soap.SOAPElement JavaDoc;
40 import javax.xml.soap.SOAPException JavaDoc;
41 import java.io.ByteArrayInputStream JavaDoc;
42 import java.io.ByteArrayOutputStream JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.InputStream JavaDoc;
45 import java.util.HashMap JavaDoc;
46 import java.util.Iterator JavaDoc;
47
48
49 /**
50  * A basic MDB that delegates to an already insalled service.
51  *
52  * @author thomas.diesler@jboss.org
53  *
54  * @ejb.bean
55  * destination-type="javax.jms.Queue"
56  * subscription-durability="NonDurable"
57  *
58  * @jboss.destination-jndi-name
59  * name="queue/JBossWSQueue"
60  */

61 public class DelegatingMessageDrivenBean implements MessageDrivenBean JavaDoc, MessageListener JavaDoc
62 {
63    /** @since 4.0.2 */
64    static final long serialVersionUID = -2841152046009542652L;
65
66    // logging support
67
protected Logger log = Logger.getLogger(DelegatingMessageDrivenBean.class);
68
69    private MessageDrivenContext JavaDoc mdbCtx;
70    private QueueConnectionFactory JavaDoc queueFactory;
71
72    /**
73     * Get the JBossWS installed Axis sever.
74     */

75    protected AxisServer getAxisServer()
76    {
77       AxisServer axisServer;
78       try
79       {
80          MBeanServer JavaDoc mbeanServer = MBeanServerLocator.locateJBoss();
81          axisServer = (AxisServer)mbeanServer.getAttribute(AxisServiceMBean.OBJECT_NAME, "AxisServer");
82          log.debug("got AxisServer: " + axisServer);
83       }
84       catch (Exception JavaDoc e)
85       {
86          throw new RuntimeException JavaDoc("Cannot obtain axis server", e);
87       }
88       return axisServer;
89    }
90
91    /**
92     * All messages come in here, if it is a BytesMessage we pass it on for further processing.
93     */

94    public void onMessage(Message JavaDoc message)
95    {
96       try
97       {
98          if (message instanceof BytesMessage JavaDoc)
99          {
100             processSOAPMessage((BytesMessage JavaDoc)message);
101          }
102          else
103          {
104             log.warn("Ingnore message, because it is not a javax.jms.BytesMessage: " + message);
105          }
106       }
107       catch (Exception JavaDoc e)
108       {
109          throw new EJBException JavaDoc(e);
110       }
111    }
112
113    /**
114     * Read the SOAP message and pass it on to Axis. The axisServer should have an
115     * installes service, which uses this MDB as pivot point.
116     */

117    protected void processSOAPMessage(BytesMessage JavaDoc message) throws Exception JavaDoc
118    {
119       InputStream JavaDoc in = null;
120       byte[] buffer = new byte[8 * 1024];
121       ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc(buffer.length);
122       try
123       {
124          // get the incoming axisRequest content into a byte array
125
int read = message.readBytes(buffer);
126          while (read != -1)
127          {
128             out.write(buffer, 0, read);
129             read = message.readBytes(buffer);
130          }
131          in = new ByteArrayInputStream JavaDoc(out.toByteArray());
132       }
133       catch (Exception JavaDoc e)
134       {
135          log.error("Cannot get bytes from message", e);
136          return;
137       }
138
139       log.debug("onMessage: " + new String JavaDoc(out.toByteArray()));
140
141       AxisServer axisServer = getAxisServer();
142
143       // create the axisRequest and context and invoke the axisServer
144
org.jboss.axis.Message axisRequest = new org.jboss.axis.Message(in);
145       MessageContext msgContext = new MessageContext(axisServer);
146       msgContext.setRequestMessage(axisRequest);
147
148       // maps available service operation QNames to target service names
149
// we might want to cache this information
150
HashMap JavaDoc serviceOperations = getServiceOperationsMap(axisServer);
151
152       // get the targetService from the SOAPElement
153
String JavaDoc targetService = null;
154       Iterator JavaDoc it = axisRequest.getSOAPEnvelope().getBody().getChildElements();
155       while (it.hasNext())
156       {
157          SOAPElement JavaDoc soapElement = (SOAPElement JavaDoc)it.next();
158          String JavaDoc namespace = soapElement.getElementName().getURI();
159          String JavaDoc localName = soapElement.getElementName().getLocalName();
160          QName JavaDoc qname = new QName JavaDoc(namespace, localName);
161          log.debug("maybe operation: " + qname);
162          targetService = (String JavaDoc)serviceOperations.get(qname);
163       }
164
165       if (targetService != null)
166       {
167          log.debug("setTargetService: " + targetService);
168          msgContext.setTargetService(targetService);
169       }
170
171       org.jboss.axis.Message axisResponse = null;
172       try
173       {
174          axisServer.invoke(msgContext);
175          axisResponse = msgContext.getResponseMessage();
176       }
177       catch (AxisFault af)
178       {
179          axisResponse = new org.jboss.axis.Message(af);
180          axisResponse.setMessageContext(msgContext);
181       }
182       catch (Exception JavaDoc e)
183       {
184          axisResponse = new org.jboss.axis.Message(new AxisFault(e.toString()));
185          axisResponse.setMessageContext(msgContext);
186       }
187
188       Queue JavaDoc replyQueue = getReplyQueue(message);
189       if (replyQueue != null)
190          sendResponse(replyQueue, axisResponse);
191    }
192
193    /**
194     * Map the available operations for each service to their respective service name
195     */

196    private HashMap JavaDoc getServiceOperationsMap(AxisServer server)
197    {
198       HashMap JavaDoc serviceOperations = new HashMap JavaDoc();
199
200       try
201       {
202          EngineConfiguration config = server.getConfig();
203          Iterator JavaDoc it = config.getDeployedServices();
204          while (it.hasNext())
205          {
206             ServiceDesc service = (ServiceDesc)it.next();
207             log.debug("service: [name=" + service.getName() + ",ns=" + service.getDefaultNamespace() + "]");
208             Iterator JavaDoc opit = service.getOperations().iterator();
209             while (opit.hasNext())
210             {
211                OperationDesc operation = (OperationDesc)opit.next();
212                QName JavaDoc qname = operation.getElementQName();
213                log.debug(" operation: [qname=" + qname + "]");
214                serviceOperations.put(qname, service.getName());
215             }
216          }
217       }
218       catch (ConfigurationException e)
219       {
220          log.error("Cannot map service operations", e);
221       }
222
223       return serviceOperations;
224    }
225
226    /**
227     * Get the reply queue.
228     */

229    protected Queue JavaDoc getReplyQueue(BytesMessage JavaDoc message)
230            throws JMSException JavaDoc
231    {
232       Queue JavaDoc replyQueue = (Queue JavaDoc)message.getJMSReplyTo();
233       return replyQueue;
234    }
235
236    /**
237     * Respond to the call by sending a message to the reply queue
238     */

239    protected void sendResponse(Queue JavaDoc replyQueue, org.jboss.axis.Message axisResponse)
240            throws SOAPException JavaDoc, IOException JavaDoc, JMSException JavaDoc
241    {
242       ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc(8 * 1024);
243       axisResponse.writeTo(out);
244
245       QueueConnection JavaDoc qc = queueFactory.createQueueConnection();
246       QueueSession JavaDoc session = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
247       QueueSender JavaDoc sender = null;
248       try
249       {
250          sender = session.createSender(replyQueue);
251          BytesMessage JavaDoc responseMessage = session.createBytesMessage();
252          responseMessage.writeBytes(out.toByteArray());
253          sender.send(responseMessage);
254          log.info("Sent response");
255       }
256       finally
257       {
258          try
259          {
260             sender.close();
261          }
262          catch (JMSException JavaDoc ignored)
263          {
264          }
265          try
266          {
267             session.close();
268          }
269          catch (JMSException JavaDoc ignored)
270          {
271          }
272          try
273          {
274             qc.close();
275          }
276          catch (JMSException JavaDoc ignored)
277          {
278          }
279       }
280    }
281
282    // MDB lifecycle methods ********************************************************************************************
283

284    public void ejbCreate()
285    {
286       try
287       {
288          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
289          queueFactory = (QueueConnectionFactory JavaDoc)ctx.lookup("java:/ConnectionFactory");
290       }
291       catch (Exception JavaDoc e)
292       {
293          throw new NestedRuntimeException(e);
294       }
295    }
296
297    /**
298     * A container invokes this method before it ends the life of the message-driven object.
299     */

300    public void ejbRemove() throws EJBException JavaDoc
301    {
302    }
303
304    /**
305     * Set the associated message-driven context.
306     */

307    public void setMessageDrivenContext(MessageDrivenContext JavaDoc ctx) throws EJBException JavaDoc
308    {
309       this.mdbCtx = ctx;
310    }
311 }
312
Popular Tags