KickJava   Java API By Example, From Geeks To Geeks.

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


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: JMSTransportSupport.java,v 1.1.2.3 2005/04/12 16:07:54 starksm Exp $
10

11 import org.jboss.axis.AxisFault;
12 import org.jboss.axis.MessageContext;
13 import org.jboss.axis.configuration.SimpleProvider;
14 import org.jboss.axis.handlers.soap.SOAPService;
15 import org.jboss.axis.server.AxisServer;
16 import org.jboss.logging.Logger;
17 import org.jboss.util.NestedRuntimeException;
18 import org.jboss.webservice.server.InvokerProviderSimple;
19
20 import javax.ejb.EJBException JavaDoc;
21 import javax.ejb.MessageDrivenBean JavaDoc;
22 import javax.ejb.MessageDrivenContext JavaDoc;
23 import javax.jms.BytesMessage JavaDoc;
24 import javax.jms.JMSException JavaDoc;
25 import javax.jms.Message JavaDoc;
26 import javax.jms.MessageListener JavaDoc;
27 import javax.jms.Queue JavaDoc;
28 import javax.jms.QueueConnection JavaDoc;
29 import javax.jms.QueueConnectionFactory JavaDoc;
30 import javax.jms.QueueSender JavaDoc;
31 import javax.jms.QueueSession JavaDoc;
32 import javax.jms.Session JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.xml.soap.SOAPException JavaDoc;
35 import java.io.ByteArrayInputStream JavaDoc;
36 import java.io.ByteArrayOutputStream JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.io.InputStream JavaDoc;
39
40 /**
41  * The abstract base class for MDBs that want to act as web service endpoints.
42  * A subclass should only need to implement the service endpoint interface.
43  *
44  * @author thomas.diesler@jboss.org
45  */

46 public abstract class JMSTransportSupport implements MessageDrivenBean JavaDoc, MessageListener JavaDoc
47 {
48    /** @since 4.0.2 */
49    static final long serialVersionUID = -6224491411234603413L;
50    // logging support
51
protected Logger log = Logger.getLogger(JMSTransportSupport.class);
52
53    //private MessageDrivenContext mdbCtx;
54
private QueueConnectionFactory JavaDoc queueFactory;
55
56    /**
57     * Get the JBossWS installed Axis sever.
58     */

59    protected AxisServer getAxisServer()
60    {
61       AxisServer axisServer;
62       try
63       {
64          SimpleProvider config = new SimpleProvider();
65          SOAPService service = new SOAPService(new InvokerProviderSimple(this));
66          config.deployService(getServiceName(), service);
67          axisServer = new AxisServer(config);
68          log.debug("got AxisServer: " + axisServer);
69       }
70       catch (Exception JavaDoc e)
71       {
72          throw new RuntimeException JavaDoc("Cannot obtain axis server", e);
73       }
74       return axisServer;
75    }
76
77    /**
78     * Return the service name for this MDB
79     */

80    protected String JavaDoc getServiceName()
81    {
82       return "jms-service";
83    }
84
85    /**
86     * All messages come in here, if it is a BytesMessage we pass it on for further processing.
87     */

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

111    protected void processSOAPMessage(BytesMessage JavaDoc message) throws Exception JavaDoc
112    {
113       InputStream JavaDoc in = null;
114       byte[] buffer = new byte[8 * 1024];
115       ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc(buffer.length);
116       try
117       {
118          // get the incoming axisRequest content into a byte array
119
int read = message.readBytes(buffer);
120          while (read != -1)
121          {
122             out.write(buffer, 0, read);
123             read = message.readBytes(buffer);
124          }
125          in = new ByteArrayInputStream JavaDoc(out.toByteArray());
126       }
127       catch (Exception JavaDoc e)
128       {
129          log.error("Cannot get bytes from message", e);
130          return;
131       }
132
133       log.debug("onMessage: " + new String JavaDoc(out.toByteArray()));
134
135       // create the axisRequest and context and invoke the server
136
org.jboss.axis.Message axisRequest = new org.jboss.axis.Message(in);
137       MessageContext msgContext = new MessageContext(getAxisServer());
138       msgContext.setRequestMessage(axisRequest);
139       msgContext.setTargetService(getServiceName());
140
141       org.jboss.axis.Message axisResponse = null;
142       try
143       {
144          getAxisServer().invoke(msgContext);
145          axisResponse = msgContext.getResponseMessage();
146       }
147       catch (AxisFault af)
148       {
149          axisResponse = new org.jboss.axis.Message(af);
150          axisResponse.setMessageContext(msgContext);
151       }
152       catch (Exception JavaDoc e)
153       {
154          axisResponse = new org.jboss.axis.Message(new AxisFault(e.toString()));
155          axisResponse.setMessageContext(msgContext);
156       }
157
158       Queue JavaDoc replyQueue = getReplyQueue(message);
159       if (replyQueue != null)
160          sendResponse(replyQueue, axisResponse);
161    }
162
163    /**
164     * Get the reply queue.
165     */

166    protected Queue JavaDoc getReplyQueue(BytesMessage JavaDoc message)
167            throws JMSException JavaDoc
168    {
169       Queue JavaDoc replyQueue = (Queue JavaDoc)message.getJMSReplyTo();
170       return replyQueue;
171    }
172
173    /**
174     * Respond to the call by sending a message to the reply queue
175     */

176    protected void sendResponse(Queue JavaDoc replyQueue, org.jboss.axis.Message axisResponse)
177            throws SOAPException JavaDoc, IOException JavaDoc, JMSException JavaDoc
178    {
179       ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc(8 * 1024);
180       axisResponse.writeTo(out);
181
182       QueueConnection JavaDoc qc = queueFactory.createQueueConnection();
183       QueueSession JavaDoc session = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
184       QueueSender JavaDoc sender = null;
185       try
186       {
187          sender = session.createSender(replyQueue);
188          BytesMessage JavaDoc responseMessage = session.createBytesMessage();
189          responseMessage.writeBytes(out.toByteArray());
190          sender.send(responseMessage);
191          log.info("Sent response");
192       }
193       finally
194       {
195          try
196          {
197             sender.close();
198          }
199          catch (JMSException JavaDoc ignored)
200          {
201          }
202          try
203          {
204             session.close();
205          }
206          catch (JMSException JavaDoc ignored)
207          {
208          }
209          try
210          {
211             qc.close();
212          }
213          catch (JMSException JavaDoc ignored)
214          {
215          }
216       }
217    }
218
219    // MDB lifecycle methods ********************************************************************************************
220

221    public void ejbCreate()
222    {
223       try
224       {
225          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
226          queueFactory = (QueueConnectionFactory JavaDoc)ctx.lookup("java:/ConnectionFactory");
227       }
228       catch (Exception JavaDoc e)
229       {
230          throw new NestedRuntimeException(e);
231       }
232    }
233
234    /**
235     * A container invokes this method before it ends the life of the message-driven object.
236     */

237    public void ejbRemove() throws EJBException JavaDoc
238    {
239    }
240
241    /**
242     * Set the associated message-driven context.
243     */

244    public void setMessageDrivenContext(MessageDrivenContext JavaDoc ctx) throws EJBException JavaDoc
245    {
246       //this.mdbCtx = ctx;
247
}
248 }
249
Popular Tags