KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > providers > ComProvider


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.providers;
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.components.bridge.COMBridge;
24 import org.apache.axis.deployment.wsdd.providers.WSDDComProvider;
25 import org.apache.axis.description.OperationDesc;
26 import org.apache.axis.description.ParameterDesc;
27 import org.apache.axis.handlers.soap.SOAPService;
28 import org.apache.axis.message.RPCElement;
29 import org.apache.axis.message.RPCHeaderParam;
30 import org.apache.axis.message.RPCParam;
31 import org.apache.axis.message.SOAPBodyElement;
32 import org.apache.axis.message.SOAPEnvelope;
33 import org.apache.axis.soap.SOAPConstants;
34 import org.apache.axis.utils.JavaUtils;
35 import org.apache.axis.utils.Messages;
36
37 import javax.xml.namespace.QName JavaDoc;
38 import java.util.Hashtable JavaDoc;
39 import java.util.Vector JavaDoc;
40
41 public class ComProvider extends BasicProvider {
42
43
44     public void invoke(MessageContext msgContext) throws AxisFault {
45         try {
46             SOAPService service = msgContext.getService();
47             String JavaDoc progID = (String JavaDoc) service.getOption(WSDDComProvider.OPTION_PROGID);
48             String JavaDoc threadingModel = (String JavaDoc) service.getOption(WSDDComProvider.OPTION_THREADING_MODEL);
49
50             if (log.isDebugEnabled()) {
51                 log.debug("Enter: COMProvider.processMessage()");
52             }
53
54             OperationDesc operation = msgContext.getOperation();
55
56             Vector JavaDoc bodies = msgContext.getRequestMessage().getSOAPEnvelope().getBodyElements();
57             if (log.isDebugEnabled()) {
58                 log.debug(Messages.getMessage("bodyElems00", "" + bodies.size()));
59                 log.debug(Messages.getMessage("bodyIs00", "" + bodies.get(0)));
60             }
61
62             RPCElement body = null;
63             
64             // Find the first "root" body element, which is the RPC call.
65
for (int bNum = 0; body == null && bNum < bodies.size(); bNum++) {
66                 // If this is a regular old SOAPBodyElement, and it's a root,
67
// we're probably a non-wrapped doc/lit service. In this case,
68
// we deserialize the element, and create an RPCElement "wrapper"
69
// around it which points to the correct method.
70
// FIXME : There should be a cleaner way to do this...
71
if (!(bodies.get(bNum) instanceof RPCElement)) {
72                     SOAPBodyElement bodyEl = (SOAPBodyElement) bodies.get(bNum);
73                     // igors: better check if bodyEl.getID() != null
74
// to make sure this loop does not step on SOAP-ENC objects
75
// that follow the parameters! FIXME?
76
if (bodyEl.isRoot() && operation != null && bodyEl.getID() == null) {
77                         ParameterDesc param = operation.getParameter(bNum);
78                         // at least do not step on non-existent parameters!
79
if (param != null) {
80                             Object JavaDoc val = bodyEl.getValueAsType(param.getTypeQName());
81                             body = new RPCElement("",
82                                     operation.getName(),
83                                     new Object JavaDoc[]{val});
84                         }
85                     }
86                 } else {
87                     body = (RPCElement) bodies.get(bNum);
88                 }
89             }
90
91             String JavaDoc methodName = body.getMethodName();
92             Vector JavaDoc args = body.getParams();
93             int numArgs = args.size();
94
95             Vector JavaDoc argValues = new Vector JavaDoc();
96             
97             // Put the values contained in the RPCParams into an array
98
// suitable for passing to java.lang.reflect.Method.invoke()
99
// Make sure we respect parameter ordering if we know about it
100
// from metadata, and handle whatever conversions are necessary
101
// (values -> Holders, etc)
102
for (int i = 0; i < numArgs; i++) {
103                 RPCParam rpcParam = (RPCParam) args.get(i);
104                 Object JavaDoc value = rpcParam.getObjectValue();
105
106                 // first check the type on the paramter
107
ParameterDesc paramDesc = rpcParam.getParamDesc();
108
109                 // if we found some type info try to make sure the value type is
110
// correct. For instance, if we deserialized a xsd:dateTime in
111
// to a Calendar and the service takes a Date, we need to convert
112
if (paramDesc != null && paramDesc.getJavaType() != null) {
113
114                     // Get the type in the signature (java type or its holder)
115
Class JavaDoc sigType = paramDesc.getJavaType();
116
117                     // Convert the value into the expected type in the signature
118
value = JavaUtils.convert(value,
119                             sigType);
120
121                     rpcParam.setObjectValue(value);
122                 }
123                 argValues.add(value);
124             }
125
126             COMBridge bridge = new COMBridge();
127             Hashtable JavaDoc props = new Hashtable JavaDoc();
128             props.put("progid", progID);
129             if (threadingModel != null)
130                 props.put("threadmodel", threadingModel);
131
132             Object JavaDoc result = bridge.execute(methodName, argValues, props);
133
134             RPCElement resBody = new RPCElement(methodName + "Response");
135             resBody.setPrefix(body.getPrefix());
136             resBody.setNamespaceURI(body.getNamespaceURI());
137             resBody.setEncodingStyle(msgContext.getEncodingStyle());
138
139             Message resMsg = msgContext.getResponseMessage();
140             SOAPEnvelope resEnv;
141
142             // If we didn't have a response message, make sure we set one up
143
if (resMsg == null) {
144                 resEnv = new SOAPEnvelope(msgContext.getSOAPConstants());
145
146                 resMsg = new Message(resEnv);
147                 msgContext.setResponseMessage(resMsg);
148             } else {
149                 resEnv = resMsg.getSOAPEnvelope();
150             }
151
152             QName JavaDoc returnQName = operation.getReturnQName();
153             if (returnQName == null) {
154                 returnQName = new QName JavaDoc("", methodName + "Return");
155             }
156
157             // For SOAP 1.2, add a result
158
if (msgContext.getSOAPConstants() ==
159                     SOAPConstants.SOAP12_CONSTANTS) {
160                 returnQName = Constants.QNAME_RPC_RESULT;
161             }
162
163             RPCParam param = new RPCParam(returnQName, result);
164             param.setParamDesc(operation.getReturnParamDesc());
165             if (!operation.isReturnHeader()) {
166                 resBody.addParam(param);
167             } else {
168                 resEnv.addHeader(new RPCHeaderParam(param));
169             }
170
171             resEnv.addBodyElement(resBody);
172
173         } catch (Exception JavaDoc e) {
174             entLog.debug(Messages.getMessage("toAxisFault00"), e);
175             throw AxisFault.makeFault(e);
176         } catch (Throwable JavaDoc t) {
177             entLog.debug(Messages.getMessage("toAxisFault00"));
178             throw new AxisFault(Messages.getMessage("toAxisFault00"), t);
179         }
180     }
181
182     public void initServiceDesc(SOAPService service, MessageContext msgContext)
183             throws AxisFault {
184     }
185 }
186
Popular Tags