KickJava   Java API By Example, From Geeks To Geeks.

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


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