KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > providers > soap > apachesoap > SOAPJMSConnection


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.wsif.providers.soap.apachesoap;
59
60 import java.io.BufferedReader JavaDoc;
61 import java.io.IOException JavaDoc;
62 import java.io.Serializable JavaDoc;
63 import java.io.StringReader JavaDoc;
64 import java.io.StringWriter JavaDoc;
65 import java.net.URL JavaDoc;
66 import java.util.Hashtable JavaDoc;
67 import java.util.HashMap JavaDoc;
68
69 import javax.mail.MessagingException JavaDoc;
70 import org.apache.soap.Envelope;
71 import org.apache.soap.SOAPException;
72 import org.apache.soap.encoding.SOAPMappingRegistry;
73 import org.apache.soap.rpc.SOAPContext;
74 import org.apache.soap.transport.SOAPTransport;
75 import org.apache.wsif.WSIFCorrelationId;
76 import org.apache.wsif.WSIFCorrelationService;
77 import org.apache.wsif.WSIFException;
78 import org.apache.wsif.logging.Trc;
79 import org.apache.wsif.util.WSIFCorrelationServiceLocator;
80 import org.apache.wsif.util.WSIFProperties;
81 import org.apache.wsif.util.jms.WSIFJMSCorrelationId;
82 import org.apache.wsif.util.jms.WSIFJMSDestination;
83 import org.apache.wsif.util.jms.WSIFJMSFinder;
84 import org.apache.wsif.wsdl.extensions.jms.JMSAddress;
85
86 /**
87  * This class is a SOAPTransport that supports JMS.
88  *
89  * @author Mark Whitlock <whitlock@apache.org>
90  */

91 public class SOAPJMSConnection implements SOAPTransport {
92     private BufferedReader JavaDoc responseReader = null;
93     private SOAPContext responseSOAPContext = null;
94     private WSIFJMSDestination destination = null;
95
96     // folowing are for async operation
97
private boolean asyncOperation = false;
98     private WSIFOperation_ApacheSOAP wsifOperation = null;
99     private long syncTimeout;
100     private long asyncTimeout;
101
102     private static final String JavaDoc DUMMY_RESPONSE =
103         "<?xml version='1.0' encoding='UTF-8'?>\n<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\">\n<SOAP-ENV:Body>\n<ns1:addEntryResponse xmlns:ns1=\"http://wsifservice.addressbook/\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n</ns1:addEntryResponse>\n\n</SOAP-ENV:Body>\n</SOAP-ENV:Envelope>";
104
105     public SOAPJMSConnection(JMSAddress ja, String JavaDoc portName) throws WSIFException {
106         Trc.entry(this, ja, portName);
107         destination =
108             new WSIFJMSDestination(
109                 WSIFJMSFinder.newFinder(ja,portName),
110                 ja.getJmsProvDestName(),
111                 WSIFProperties.getSyncTimeout());
112         Trc.exit(ja);
113     }
114
115     public Hashtable JavaDoc getHeaders() {
116         Trc.entry(this);
117         Trc.exit(null);
118         return null;
119     }
120
121     public SOAPContext getResponseSOAPContext() {
122         Trc.entry(this);
123         Trc.exit(responseSOAPContext);
124         return responseSOAPContext;
125     }
126
127     public long getSyncTimeout() {
128         Trc.entry(this);
129         Trc.exit(new Long JavaDoc(syncTimeout));
130         return syncTimeout;
131     }
132
133     public long getAsyncTimeout() {
134         Trc.entry(this);
135         Trc.exit(new Long JavaDoc(asyncTimeout));
136         return asyncTimeout;
137     }
138
139     public BufferedReader JavaDoc receive() {
140         Trc.entry(this);
141         Trc.exit();
142         return responseReader;
143     }
144
145     /**
146      * This send method is really doing a send followed by a receive. The
147      * receive method just returns the BufferedReader that is set up by
148      * this method.
149      */

150     public void send(
151         URL JavaDoc sendTo,
152         String JavaDoc action,
153         Hashtable JavaDoc headers,
154         Envelope env,
155         SOAPMappingRegistry smr,
156         SOAPContext ctx)
157         throws SOAPException {
158         Trc.entry(this, sendTo, action, headers, env, smr, ctx);
159
160         try {
161             if (isAsyncOperation()) {
162                 performAsyncSend(sendTo, action, headers, env, smr, ctx);
163             } else {
164                 StringWriter JavaDoc payloadSW = new StringWriter JavaDoc();
165                 env.marshall(payloadSW, smr, ctx);
166                 String JavaDoc id = destination.send(payloadSW.toString(), null);
167                 String JavaDoc response = destination.receiveString(id, syncTimeout);
168                 responseSOAPContext = new SOAPContext();
169                 responseSOAPContext.setRootPart(response, "text/xml");
170                 responseReader = new BufferedReader JavaDoc(new StringReader JavaDoc(response));
171             }
172         } catch (IOException JavaDoc ioe) {
173             Trc.exception(ioe);
174             // Not sure what the faultCode should be - this may be wrong
175
throw new SOAPException("WSIF SOAPJMSConnection ", ioe.toString());
176         } catch (MessagingException JavaDoc me) {
177             Trc.exception(me);
178             // Not sure what the faultCode should be - this may be wrong
179
throw new SOAPException("WSIF SOAPJMSConnection ", me.toString());
180         }
181         Trc.exit();
182     }
183
184     /**
185      * Send the request asynchronously.
186      * After sending the request this associates the JMS message correlation
187      * ID with the WSIFOperation in the correlation service, and stores the
188      * the correlation ID in the message context so the WSIFOperation can
189      * pass it back to the executeRequestResponseAsync caller.
190      */

191     private void performAsyncSend(
192         URL JavaDoc sendTo,
193         String JavaDoc action,
194         Hashtable JavaDoc headers,
195         Envelope env,
196         SOAPMappingRegistry smr,
197         SOAPContext ctx)
198         throws IOException JavaDoc, SOAPException, MessagingException JavaDoc {
199         String JavaDoc msgID;
200         StringWriter JavaDoc payloadSW = new StringWriter JavaDoc();
201         env.marshall(payloadSW, smr, ctx);
202         WSIFOperation_ApacheSOAP wsifOp = (WSIFOperation_ApacheSOAP) getWsifOperation();
203
204         WSIFCorrelationId cid;
205
206         // only save op in the correlation service if there's a response handler
207
if ( wsifOp.getResponseHandler() == null ) {
208             msgID = destination.send(payloadSW.toString(), null);
209             cid = new WSIFJMSCorrelationId( msgID );
210         } else {
211            WSIFCorrelationService correlator =
212               WSIFCorrelationServiceLocator.getCorrelationService();
213            synchronized( correlator ) {
214               msgID = destination.send(payloadSW.toString(), null);
215               cid = new WSIFJMSCorrelationId( msgID );
216               if ( correlator != null ) {
217                  correlator.put(
218                     cid,
219                     (Serializable JavaDoc)getWsifOperation(),
220                     asyncTimeout );
221               }
222            }
223         }
224            
225
226         wsifOp.setAsyncRequestID(new WSIFJMSCorrelationId(msgID));
227
228         // SOAP doesn't like a null response so give it a dummy null
229
responseSOAPContext = new SOAPContext();
230         responseSOAPContext.setRootPart(DUMMY_RESPONSE, "text/xml");
231         responseReader = new BufferedReader JavaDoc(new StringReader JavaDoc(DUMMY_RESPONSE));
232
233     }
234
235     public void setAsyncOperation(boolean b) {
236         Trc.entry(this, b);
237         asyncOperation = b;
238         Trc.exit();
239     }
240
241     public boolean isAsyncOperation() {
242         Trc.entry(this);
243         Trc.exit(asyncOperation);
244         return asyncOperation;
245     }
246
247     public WSIFOperation_ApacheSOAP getWsifOperation() {
248         Trc.entry(this);
249         Trc.exit(wsifOperation);
250         return wsifOperation;
251     }
252
253     public void setWsifOperation(WSIFOperation_ApacheSOAP op) {
254         Trc.entry(this, op);
255         wsifOperation = op;
256         Trc.exit();
257     }
258
259     public void setJmsProperty(String JavaDoc name, Object JavaDoc value) throws WSIFException {
260         Trc.entry(this, name, value);
261         destination.setProperty(name, value);
262         Trc.exit();
263     }
264
265     public void setJmsProperties(HashMap JavaDoc hm) {
266         Trc.entry(this,hm);
267         destination.setProperties(hm);
268         Trc.exit();
269     }
270
271     public HashMap JavaDoc getJmsProperties() throws WSIFException {
272        Trc.entry(this);
273        HashMap JavaDoc hm = destination.getProperties();
274        Trc.exit(hm);
275        return hm;
276     }
277
278     void close() throws WSIFException {
279         Trc.entry(this);
280         destination.close();
281         Trc.exit();
282     }
283
284     public void setSyncTimeout(long timeout) {
285         Trc.entry(this, new Long JavaDoc(timeout));
286         syncTimeout = timeout;
287         Trc.exit();
288     }
289
290     public void setAsyncTimeout(long timeout) {
291         Trc.entry(this, new Long JavaDoc(timeout));
292         asyncTimeout = timeout;
293         Trc.exit();
294     }
295 }
Popular Tags