KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > transport > jms > JMSSender


1 /*
2  * Copyright 2001, 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis.transport.jms;
18
19 import org.apache.axis.AxisFault;
20 import org.apache.axis.Message;
21 import org.apache.axis.MessageContext;
22 import org.apache.axis.handlers.BasicHandler;
23 import org.apache.axis.attachments.Attachments;
24
25 import javax.jms.Destination JavaDoc;
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 /**
31  * This is meant to be used on a SOAP Client to call a SOAP server.
32  *
33  * @author Jaime Meritt (jmeritt@sonicsoftware.com)
34  * @author Richard Chung (rchung@sonicsoftware.com)
35  * @author Dave Chappell (chappell@sonicsoftware.com)
36  */

37 public class JMSSender extends BasicHandler
38 {
39     public JMSSender()
40     {
41     }
42
43     /**
44      * invoke() creates an endpoint, sends the request SOAP message, and then
45      * either reads the response SOAP message or simply returns.
46      *
47      * @todo hash on something much better than the connection factory
48      * something like domain:url:username:password would be adequate
49      * @param msgContext
50      * @throws AxisFault
51      */

52     public void invoke(MessageContext msgContext) throws AxisFault
53     {
54         JMSConnector connector = null;
55         try
56         {
57             Object JavaDoc destination = msgContext.getProperty(JMSConstants.DESTINATION);
58             if(destination == null)
59                 throw new AxisFault("noDestination");
60
61             connector = (JMSConnector)msgContext.getProperty(JMSConstants.CONNECTOR);
62
63             JMSEndpoint endpoint = null;
64             if(destination instanceof String JavaDoc)
65                 endpoint = connector.createEndpoint((String JavaDoc)destination);
66             else
67                 endpoint = connector.createEndpoint((Destination JavaDoc)destination);
68
69             ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
70             msgContext.getRequestMessage().writeTo(out);
71
72             HashMap JavaDoc props = createSendProperties(msgContext);
73
74             // If the request message contains attachments, set
75
// a contentType property to go in the outgoing message header
76
String JavaDoc ret = null;
77             Message message = msgContext.getRequestMessage();
78             Attachments mAttachments = message.getAttachmentsImpl();
79             if (mAttachments != null && 0 != mAttachments.getAttachmentCount())
80             {
81                 String JavaDoc contentType = mAttachments.getContentType();
82                 if(contentType != null && !contentType.trim().equals(""))
83                 {
84                     props.put("contentType", contentType);
85                 }
86             }
87
88             boolean waitForResponse = true;
89             if(msgContext.containsProperty(JMSConstants.WAIT_FOR_RESPONSE))
90                 waitForResponse =
91                     ((Boolean JavaDoc)msgContext.getProperty(
92                         JMSConstants.WAIT_FOR_RESPONSE)).booleanValue();
93             if(waitForResponse)
94             {
95                 long timeout = (long) msgContext.getTimeout();
96                 byte[] response = endpoint.call(out.toByteArray(), timeout, props);
97                 Message msg = new Message(response);
98                 msgContext.setResponseMessage(msg);
99             }
100             else
101             {
102                 endpoint.send(out.toByteArray(), props);
103             }
104         }
105         catch(Exception JavaDoc e)
106         {
107             throw new AxisFault("failedSend", e);
108         }
109         finally
110         {
111             if (connector != null)
112                 JMSConnectorManager.getInstance().release(connector);
113         }
114     }
115
116     private HashMap JavaDoc createSendProperties(MessageContext context)
117     {
118         //I'm not sure why this helper method is private, but
119
//we need to delegate to factory method that can build the
120
//application-specific map of properties so make a change to
121
//delegate here.
122
HashMap JavaDoc props = createApplicationProperties(context);
123
124         if(context.containsProperty(JMSConstants.PRIORITY))
125             props.put(JMSConstants.PRIORITY,
126             context.getProperty(JMSConstants.PRIORITY));
127         if(context.containsProperty(JMSConstants.DELIVERY_MODE))
128             props.put(JMSConstants.DELIVERY_MODE,
129             context.getProperty(JMSConstants.DELIVERY_MODE));
130         if(context.containsProperty(JMSConstants.TIME_TO_LIVE))
131             props.put(JMSConstants.TIME_TO_LIVE,
132             context.getProperty(JMSConstants.TIME_TO_LIVE));
133         if(context.containsProperty(JMSConstants.JMS_CORRELATION_ID))
134             props.put(JMSConstants.JMS_CORRELATION_ID,
135             context.getProperty(JMSConstants.JMS_CORRELATION_ID));
136         return props;
137     }
138
139     /** Return a map of properties that makeup the application-specific
140         for the JMS Messages.
141      */

142     protected HashMap JavaDoc createApplicationProperties(MessageContext context) {
143         HashMap JavaDoc props = null;
144         if (context.containsProperty(
145             JMSConstants.JMS_APPLICATION_MSG_PROPS)) {
146             props = new HashMap JavaDoc();
147             props.putAll((Map JavaDoc)context.getProperty(
148                 JMSConstants.JMS_APPLICATION_MSG_PROPS));
149         }
150         return props;
151     }
152 }
Popular Tags