KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-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.local;
18
19 import org.apache.axis.AxisFault;
20 import org.apache.axis.Constants;
21 import org.apache.axis.Message;
22 import org.apache.axis.MessageContext;
23 import org.apache.axis.attachments.Attachments;
24 import org.apache.axis.components.logger.LogFactory;
25 import org.apache.axis.handlers.BasicHandler;
26 import org.apache.axis.message.SOAPEnvelope;
27 import org.apache.axis.message.SOAPFault;
28 import org.apache.axis.server.AxisServer;
29 import org.apache.axis.utils.Messages;
30 import org.apache.commons.logging.Log;
31
32 import java.net.URL JavaDoc;
33
34 /**
35  * This is meant to be used on a SOAP Client to call a SOAP server.
36  *
37  * @author Sam Ruby <rubys@us.ibm.com>
38  */

39 public class LocalSender extends BasicHandler {
40     protected static Log log =
41         LogFactory.getLog(LocalSender.class.getName());
42
43     private volatile AxisServer server;
44
45     /**
46      * Allocate an embedded Axis server to process requests and initialize it.
47      */

48     public synchronized void init() {
49         this.server= new AxisServer();
50     }
51
52     public void invoke(MessageContext clientContext) throws AxisFault {
53         if (log.isDebugEnabled()) {
54             log.debug("Enter: LocalSender::invoke");
55         }
56
57         AxisServer targetServer =
58             (AxisServer)clientContext.getProperty(LocalTransport.LOCAL_SERVER);
59
60         if (log.isDebugEnabled()) {
61             log.debug(Messages.getMessage("usingServer00",
62                 "LocalSender", "" + targetServer));
63         }
64
65         if (targetServer == null) {
66             // This should have already been done, but it doesn't appear to be
67
// something that can be relied on. Oh, well...
68
if (server == null) init();
69             targetServer = server;
70         }
71
72         // Define a new messageContext per request
73
MessageContext serverContext = new MessageContext(targetServer);
74
75         // copy the request, and force its format to String in order to
76
// exercise the serializers.
77

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

80         Message clientRequest = clientContext.getRequestMessage();
81         
82         String JavaDoc msgStr = clientRequest.getSOAPPartAsString();
83
84         if (log.isDebugEnabled()) {
85             log.debug(Messages.getMessage("sendingXML00", "LocalSender"));
86             log.debug(msgStr);
87         }
88         
89         Message serverRequest = new Message(msgStr);
90
91         Attachments serverAttachments = serverRequest.getAttachmentsImpl();
92         Attachments clientAttachments = clientRequest.getAttachmentsImpl();
93
94         if (null != clientAttachments && null != serverAttachments) {
95             serverAttachments.setAttachmentParts(clientAttachments.getAttachments());
96         }
97
98         serverContext.setRequestMessage(serverRequest);
99
100 // END FIX: http://nagoya.apache.org/bugzilla/show_bug.cgi?id=17161
101

102         serverContext.setTransportName("local");
103
104         // Also copy authentication info if present
105
String JavaDoc user = clientContext.getUsername();
106         if (user != null) {
107             serverContext.setUsername(user);
108             String JavaDoc pass = clientContext.getPassword();
109             if (pass != null)
110                 serverContext.setPassword(pass);
111         }
112
113         // set the realpath if possible
114
String JavaDoc transURL = clientContext.getStrProp(MessageContext.TRANS_URL);
115         if (transURL != null) {
116             try {
117                 URL JavaDoc url = new URL JavaDoc(transURL);
118                 String JavaDoc file = url.getFile();
119                 if (file.length()>0 && file.charAt(0)=='/') {
120                     file = file.substring(1);
121                 }
122                 serverContext.setProperty(Constants.MC_REALPATH, file);
123                 serverContext.setProperty(MessageContext.TRANS_URL,
124                                           "local:///" + file);
125                 // This enables "local:///AdminService" and the like to work.
126
serverContext.setTargetService(file);
127             } catch (Exception JavaDoc e) {
128                 throw AxisFault.makeFault(e);
129             }
130         }
131
132         // If we've been given an explicit "remote" service to invoke,
133
// use it. (Note that right now this overrides the setting above;
134
// is this the correct precedence?)
135
String JavaDoc remoteService = clientContext.getStrProp(LocalTransport.REMOTE_SERVICE);
136         if (remoteService != null)
137             serverContext.setTargetService(remoteService);
138
139         // invoke the request
140
try {
141             targetServer.invoke(serverContext);
142         } catch (AxisFault fault) {
143             Message respMsg = serverContext.getResponseMessage();
144             if (respMsg == null) {
145                 respMsg = new Message(fault);
146                 serverContext.setResponseMessage(respMsg);
147             } else {
148                 SOAPFault faultEl = new SOAPFault(fault);
149                 SOAPEnvelope env = respMsg.getSOAPEnvelope();
150                 env.clearBody();
151                 env.addBodyElement(faultEl);
152             }
153         }
154
155         // copy back the response, and force its format to String in order to
156
// exercise the deserializers.
157
clientContext.setResponseMessage(serverContext.getResponseMessage());
158         clientContext.getResponseMessage().getSOAPPartAsString();
159
160         if (log.isDebugEnabled()) {
161             log.debug("Exit: LocalSender::invoke");
162         }
163     }
164 }
165
Popular Tags