KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > providers > soap > apacheaxis > WSIFJmsSender


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.apacheaxis;
59
60 import java.io.ByteArrayOutputStream JavaDoc;
61 import java.io.IOException JavaDoc;
62 import java.io.Serializable JavaDoc;
63
64 import javax.xml.soap.SOAPException JavaDoc;
65
66 import org.apache.axis.AxisFault;
67 import org.apache.axis.Message;
68 import org.apache.axis.MessageContext;
69 import org.apache.axis.handlers.BasicHandler;
70 import org.apache.wsif.WSIFCorrelationId;
71 import org.apache.wsif.WSIFCorrelationService;
72 import org.apache.wsif.WSIFException;
73 import org.apache.wsif.logging.Trc;
74 import org.apache.wsif.util.WSIFCorrelationServiceLocator;
75 import org.apache.wsif.util.WSIFProperties;
76 import org.apache.wsif.util.jms.WSIFJMSCorrelationId;
77 import org.apache.wsif.util.jms.WSIFJMSDestination;
78
79 /**
80  * @author Mark Whitlock <whitlock@apache.org>
81  */

82 public class WSIFJmsSender extends BasicHandler {
83     private static final long serialVersionUID = 1L;
84     private static final long SYNC_TIMEOUT = WSIFProperties.getSyncTimeout();
85     private static final long ASYNC_TIMEOUT = WSIFProperties.getAsyncTimeout();
86     private static final String JavaDoc DUMMY_RESPONSE =
87         "<?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>";
88
89     public void invoke(MessageContext messageContext) throws AxisFault {
90         Trc.entry(this, messageContext);
91         try {
92             // Determine if this is an async operation
93
boolean asyncMode =
94                 messageContext.isPropertyTrue(WSIFJmsTransport.ASYNCOPERATION);
95             WSIFJMSDestination dest =
96                 (WSIFJMSDestination) messageContext.getProperty(WSIFJmsTransport.DESTINATION);
97
98             Long JavaDoc transportSyncTimeoutValue =
99                 (Long JavaDoc)messageContext.getProperty(WSIFJmsTransport.SYNC_TIMEOUT);
100             long syncTimeout =
101                transportSyncTimeoutValue==null
102                   ? SYNC_TIMEOUT
103                   : transportSyncTimeoutValue.longValue();
104
105             Message message = messageContext.getRequestMessage();
106             
107             // The next line has the desired side effect of setting
108
// up MIME attachements correctly. MIME attachments using
109
// AxisJms still don't work, because it falls over later.
110
message.getContentType(messageContext.getSOAPConstants());
111             
112             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
113             message.writeTo(baos);
114             String JavaDoc contents = baos.toString();
115
116             if (asyncMode) {
117                 performAsyncSend(messageContext, dest, contents);
118             } else {
119                 String JavaDoc id = dest.send(contents, null);
120                 String JavaDoc response = dest.receiveString(id, syncTimeout);
121                 Message responseMessage = new Message(response);
122                 messageContext.setResponseMessage(responseMessage);
123             }
124         } catch (IOException JavaDoc ioe) {
125             Trc.exception(ioe);
126             throw new AxisFault(ioe.toString());
127         } catch (SOAPException JavaDoc se) {
128             Trc.exception(se);
129             throw new AxisFault(se.toString()); }
130         Trc.exit();
131     }
132
133     public void undo(MessageContext messageContext) {
134         Trc.entry(this, messageContext);
135         Trc.exit();
136     }
137
138     /**
139      * Send the request asynchronously.
140      * If there is a WISFResponseHandler associated with the operation then
141      * the the WSIFOperation is stored in the correlation service with the JMS
142      * messgage ID of the request. A listener is then set to listen for the
143      * response to the request and when the response arrives the listener
144      * looks up the responses ID in the correlation service and forwards
145      * the response to the associated WSIFOperation.
146      */

147     private void performAsyncSend(
148         MessageContext messageContext,
149         WSIFJMSDestination dest,
150         String JavaDoc data)
151         throws WSIFException {
152         String JavaDoc msgID;
153
154         WSIFOperation_ApacheAxis wsifOp =
155             (WSIFOperation_ApacheAxis) messageContext.getProperty(
156                 WSIFJmsTransport.WSIFOPERATION);
157
158         WSIFCorrelationId cid;
159
160         // only save op in the correlation service if there's a response handler
161
if ( wsifOp.getResponseHandler() == null ) {
162            msgID = dest.send( data );
163            cid = new WSIFJMSCorrelationId( msgID );
164         } else {
165             Long JavaDoc transportAsyncTimeoutValue =
166                (Long JavaDoc)messageContext.getProperty(WSIFJmsTransport.ASYNC_TIMEOUT);
167             long asyncTimeout =
168                transportAsyncTimeoutValue==null
169                ? ASYNC_TIMEOUT
170                : transportAsyncTimeoutValue.longValue();
171            WSIFCorrelationService correlator =
172               WSIFCorrelationServiceLocator.getCorrelationService();
173            synchronized( correlator ) {
174               msgID = dest.send( data );
175               cid = new WSIFJMSCorrelationId( msgID );
176               if ( correlator != null ) {
177                  correlator.put( cid, (Serializable JavaDoc)wsifOp, asyncTimeout );
178               }
179            }
180         }
181           
182
183         // Save msg ID in the WSIFop for this calling client
184
wsifOp.setAsyncRequestID(new WSIFJMSCorrelationId(msgID));
185
186         // Axis doesn't like a null response so give it something
187
Message responseMessage = new Message(DUMMY_RESPONSE);
188         messageContext.setResponseMessage(responseMessage);
189     }
190 }
Popular Tags