KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > client > AxisClientProxy


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.client;
18
19 import java.lang.reflect.InvocationHandler JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Vector JavaDoc;
23
24 import javax.xml.namespace.QName JavaDoc;
25 import javax.xml.rpc.holders.Holder JavaDoc;
26
27 import org.apache.axis.description.OperationDesc;
28 import org.apache.axis.description.ParameterDesc;
29 import org.apache.axis.utils.JavaUtils;
30
31 /**
32  * Very simple dynamic proxy InvocationHandler class. This class is
33  * constructed with a Call object, and then each time a method is invoked
34  * on a dynamic proxy using this invocation handler, we simply turn it into
35  * a SOAP request.
36  *
37  * @author Glen Daniels (gdaniels@apache.org)
38  * @author C?dric Chabanois (cchabanois@ifrance.com)
39  */

40 public class AxisClientProxy implements InvocationHandler JavaDoc {
41
42     private Call call;
43     private QName JavaDoc portName;
44
45     /**
46      * Constructor - package access only (should only really get used
47      * in Service.getPort(endpoint, proxyClass).
48      * Call can be pre-filled from wsdl
49      */

50     AxisClientProxy(Call call, QName JavaDoc portName)
51     {
52         this.call = call;
53         this.portName = portName; // can be null
54
}
55
56
57     /**
58      * Map between the parameters for the method call and the parameters needed
59      * for the <code>Call</code>.
60      * <p>
61      * Parameters for invoke method are not the same as parameter for Call
62      * instance :
63      * - Holders must be converted to their mapped java types
64      * - only in and inout parameters must be present in call parameters
65      *
66      * @param proxyParams proxyParameters
67      * @return Object[] Call parameters
68      * @throws JavaUtils.HolderException
69      */

70     private Object JavaDoc[] proxyParams2CallParams(Object JavaDoc[] proxyParams)
71         throws JavaUtils.HolderException
72     {
73         OperationDesc operationDesc = call.getOperation();
74         if (operationDesc == null)
75         {
76             // we don't know which parameters are IN, OUT or INOUT
77
// let's suppose they are all in
78
return proxyParams;
79         }
80
81         Vector JavaDoc paramsCall = new Vector JavaDoc();
82         for (int i = 0; proxyParams != null && i < proxyParams.length;i++)
83         {
84             Object JavaDoc param = proxyParams[i];
85             ParameterDesc paramDesc = operationDesc.getParameter(i);
86
87             if (paramDesc.getMode() == ParameterDesc.INOUT) {
88                 paramsCall.add(JavaUtils.getHolderValue((Holder JavaDoc)param));
89             }
90             else
91             if (paramDesc.getMode() == ParameterDesc.IN) {
92                 paramsCall.add(param);
93             }
94         }
95         return paramsCall.toArray();
96     }
97
98     /**
99      * Copy in/out and out parameters (Holder parameters) back to proxyParams.
100      *
101      * @param proxyParams proxyParameters
102      */

103     private void callOutputParams2proxyParams(Object JavaDoc[] proxyParams)
104         throws JavaUtils.HolderException
105     {
106         OperationDesc operationDesc = call.getOperation();
107         if (operationDesc == null)
108         {
109             // we don't know which parameters are IN, OUT or INOUT
110
// let's suppose they are all in
111
return;
112         }
113
114         Map JavaDoc outputParams = call.getOutputParams();
115
116         for (int i = 0; i < operationDesc.getNumParams();i++)
117         {
118             Object JavaDoc param = proxyParams[i];
119             ParameterDesc paramDesc = operationDesc.getParameter(i);
120             if ((paramDesc.getMode() == ParameterDesc.INOUT) ||
121                 (paramDesc.getMode() == ParameterDesc.OUT)) {
122
123                   JavaUtils.setHolderValue((Holder JavaDoc)param,
124                       outputParams.get(paramDesc.getQName()));
125             }
126         }
127     }
128
129     // fixme: what is o used for?
130
/**
131      * Handle a method invocation.
132      *
133      * @param o the object to invoke relative to
134      * @param method the <code>Method</code> to invoke
135      * @param objects the arguments to the method
136      * @return the result of the method
137      * @throws Throwable if anything went wrong in method dispatching or the
138      * execution of the method itself
139      */

140     public Object JavaDoc invoke(Object JavaDoc o, Method JavaDoc method, Object JavaDoc[] objects)
141             throws Throwable JavaDoc {
142         // first see if we invoke Stub methods
143
if (method.getName().equals("_setProperty")) {
144             call.setProperty((String JavaDoc) objects[0], objects[1]);
145             return null;
146         } else if (method.getName().equals("_getProperty")) {
147             return call.getProperty((String JavaDoc) objects[0]);
148         } else if (method.getName().equals("_getPropertyNames")) {
149             return call.getPropertyNames();
150         } else if (Object JavaDoc.class.equals(method.getDeclaringClass())) {
151             // if we invoke basic Object methods : delegate to Call instance
152
return method.invoke(call, objects);
153         } else {
154           Object JavaDoc outValue;
155           Object JavaDoc[] paramsCall;
156
157           if ((call.getTargetEndpointAddress() != null) &&
158               (call.getPortName() != null)) {
159               // call object has been prefilled : targetEndPoint and portname
160
// are already set. We complete it with method informations
161
call.setOperation(method.getName());
162               paramsCall = proxyParams2CallParams(objects);
163               outValue = call.invoke(paramsCall);
164           }
165           else if (portName != null)
166           {
167               // we only know the portName. Try to complete this information
168
// from wsdl if available
169
call.setOperation(portName,method.getName());
170               paramsCall = proxyParams2CallParams(objects);
171               outValue = call.invoke(paramsCall);
172           }
173           else
174           {
175               // we don't even know the portName (we don't have wsdl)
176
paramsCall = objects;
177               outValue = call.invoke(method.getName(), paramsCall);
178           }
179           callOutputParams2proxyParams(objects);
180           return outValue;
181         }
182     }
183
184     /**
185      * Returns the current call.
186      *
187      * @return the current <code>Call</code>
188      */

189     public Call getCall(){
190         return call;
191     }
192 }
193
Popular Tags