KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > transport > local > LocalSender


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 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 "Axis" 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. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55
56 package org.jboss.axis.transport.local;
57
58 import org.jboss.axis.AxisFault;
59 import org.jboss.axis.Constants;
60 import org.jboss.axis.Message;
61 import org.jboss.axis.MessageContext;
62 import org.jboss.axis.attachments.Attachments;
63 import org.jboss.axis.handlers.BasicHandler;
64 import org.jboss.axis.message.SOAPEnvelopeAxisImpl;
65 import org.jboss.axis.message.SOAPFaultImpl;
66 import org.jboss.axis.server.AxisServer;
67 import org.jboss.axis.utils.Messages;
68 import org.jboss.logging.Logger;
69
70 import java.net.URL JavaDoc;
71
72 /**
73  * This is meant to be used on a SOAP Client to call a SOAP server.
74  *
75  * @author Sam Ruby <rubys@us.ibm.com>
76  */

77 public class LocalSender extends BasicHandler
78 {
79    private static Logger log = Logger.getLogger(LocalSender.class.getName());
80
81    private volatile AxisServer server;
82
83    /**
84     * Allocate an embedded Axis server to process requests and initialize it.
85     */

86    public synchronized void init()
87    {
88       this.server = new AxisServer();
89    }
90
91    public void invoke(MessageContext clientContext) throws AxisFault
92    {
93       if (log.isDebugEnabled())
94       {
95          log.debug("Enter: LocalSender::invoke");
96       }
97
98       AxisServer targetServer =
99               (AxisServer)clientContext.getProperty(LocalTransport.LOCAL_SERVER);
100
101       if (log.isDebugEnabled())
102       {
103          log.debug(Messages.getMessage("usingServer00",
104                  "LocalSender", "" + targetServer));
105       }
106
107       if (targetServer == null)
108       {
109          // This should have already been done, but it doesn't appear to be
110
// something that can be relied on. Oh, well...
111
if (server == null) init();
112          targetServer = server;
113       }
114
115       // Define a new messageContext per request
116
MessageContext serverContext = new MessageContext(targetServer);
117
118       // copy the request, and force its format to String in order to
119
// exercise the serializers.
120

121 // START FIX: http://nagoya.apache.org/bugzilla/show_bug.cgi?id=17161
122

123       Message clientRequest = clientContext.getRequestMessage();
124
125       String JavaDoc msgStr = clientRequest.getSOAPPartAsString();
126
127       if (log.isDebugEnabled())
128       {
129          log.debug(Messages.getMessage("sendingXML00", "LocalSender"));
130          log.debug(msgStr);
131       }
132
133       Message serverRequest = new Message(msgStr);
134
135       Attachments serverAttachments = serverRequest.getAttachmentsImpl();
136       Attachments clientAttachments = clientRequest.getAttachmentsImpl();
137
138       if (null != clientAttachments && null != serverAttachments)
139       {
140          serverAttachments.setAttachmentParts(clientAttachments.getAttachments());
141       }
142
143       serverContext.setRequestMessage(serverRequest);
144
145 // END FIX: http://nagoya.apache.org/bugzilla/show_bug.cgi?id=17161
146

147       serverContext.setTransportName("local");
148
149       // Also copy authentication info if present
150
String JavaDoc user = clientContext.getUsername();
151       if (user != null)
152       {
153          serverContext.setUsername(user);
154          String JavaDoc pass = clientContext.getPassword();
155          if (pass != null)
156             serverContext.setPassword(pass);
157       }
158
159       // set the realpath if possible
160
String JavaDoc transURL = clientContext.getStrProp(MessageContext.TRANS_URL);
161       if (transURL != null)
162       {
163          try
164          {
165             URL JavaDoc url = new URL JavaDoc(transURL);
166             String JavaDoc file = url.getFile();
167             if (file.length() > 0 && file.charAt(0) == '/') file = file.substring(1);
168             serverContext.setProperty(Constants.MC_REALPATH, file);
169
170             // This enables "local:///AdminService" and the like to work.
171
serverContext.setTargetService(file);
172          }
173          catch (Exception JavaDoc e)
174          {
175             throw AxisFault.makeFault(e);
176          }
177       }
178
179       // If we've been given an explicit "remote" service to invoke,
180
// use it. (Note that right now this overrides the setting above;
181
// is this the correct precedence?)
182
String JavaDoc remoteService = clientContext.getStrProp(LocalTransport.REMOTE_SERVICE);
183       if (remoteService != null)
184          serverContext.setTargetService(remoteService);
185
186       // invoke the request
187
try
188       {
189          targetServer.invoke(serverContext);
190       }
191       catch (AxisFault fault)
192       {
193          Message respMsg = serverContext.getResponseMessage();
194          if (respMsg == null)
195          {
196             respMsg = new Message(fault);
197             serverContext.setResponseMessage(respMsg);
198          }
199          else
200          {
201             SOAPFaultImpl faultEl = new SOAPFaultImpl(fault);
202             SOAPEnvelopeAxisImpl env = respMsg.getSOAPEnvelope();
203             env.clearBody();
204             env.addBodyElement(faultEl);
205          }
206       }
207
208       // copy back the response, and force its format to String in order to
209
// exercise the deserializers.
210
clientContext.setResponseMessage(serverContext.getResponseMessage());
211       //clientContext.getResponseMessage().getAsString();
212

213       if (log.isDebugEnabled())
214       {
215          log.debug("Exit: LocalSender::invoke");
216       }
217    }
218 }
219
Popular Tags