KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > message > RPCParam


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 package org.jboss.axis.message;
56
57 import org.jboss.axis.description.ParameterDesc;
58 import org.jboss.axis.encoding.SerializationContext;
59 import org.jboss.axis.utils.JavaUtils;
60 import org.jboss.axis.utils.Messages;
61 import org.jboss.logging.Logger;
62
63 import javax.xml.namespace.QName JavaDoc;
64 import java.io.IOException JavaDoc;
65 import java.lang.reflect.Method JavaDoc;
66 import java.util.ArrayList JavaDoc;
67
68 /**
69  * An RPC parameter
70  *
71  * @author Glen Daniels (gdaniels@macromedia.com)
72  */

73 public class RPCParam
74 {
75    private static Logger log = Logger.getLogger(RPCParam.class.getName());
76
77    // Who's your daddy?
78
RPCElement myCall;
79
80    private transient QName JavaDoc qname;
81    // The value of this RPCParam
82
private Object JavaDoc value;
83    // counts number of calls to set
84
private int countSetCalls;
85
86    private ParameterDesc paramDesc;
87
88    /**
89     * Do we definitely want (or don't want) to send xsi:types? If null
90     * (the default), just do whatever our SerializationContext is configured
91     * to do. If TRUE or FALSE, the SerializationContext will do what we
92     * want.
93     */

94    private Boolean JavaDoc wantXSIType = null;
95
96    private static Method JavaDoc valueSetMethod;
97
98    static
99    {
100       Class JavaDoc cls = RPCParam.class;
101       try
102       {
103          valueSetMethod = cls.getMethod("set", new Class JavaDoc[]{Object JavaDoc.class});
104       }
105       catch (NoSuchMethodException JavaDoc e)
106       {
107          log.error(Messages.getMessage("noValue00", "" + e));
108          System.exit(-1);
109       }
110    }
111
112    /**
113     * Constructor for building up messages.
114     */

115    public RPCParam(String JavaDoc name, Object JavaDoc value)
116    {
117       this.qname = new QName JavaDoc("", name);
118       this.value = value;
119    }
120
121    public RPCParam(QName JavaDoc qname, Object JavaDoc value)
122    {
123       this.qname = qname;
124       this.value = value;
125    }
126
127    public RPCParam(String JavaDoc namespace, String JavaDoc name, Object JavaDoc value)
128    {
129       this.qname = new QName JavaDoc(namespace, name);
130       this.value = value;
131    }
132
133    public void setRPCCall(RPCElement call)
134    {
135       myCall = call;
136    }
137
138    public Object JavaDoc getValue()
139    {
140       return value;
141    }
142
143    public void setValue(Object JavaDoc newValue)
144    {
145       value = newValue;
146    }
147
148    /**
149     * This set method is registered during deserialization
150     * to set the deserialized value.
151     * If the method is called multiple times, the
152     * value is automatically changed into a container to
153     * hold all of the values.
154     *
155     * @param newValue is the deserialized object
156     */

157    public void set(Object JavaDoc newValue)
158    {
159       countSetCalls++;
160       // If this is the first call,
161
// simply set the value.
162
if (countSetCalls == 1)
163       {
164          this.value = newValue;
165          return;
166       }
167       // If this is the second call, create an
168
// ArrayList to hold all the values
169
else if (countSetCalls == 2)
170       {
171          ArrayList JavaDoc list = new ArrayList JavaDoc();
172          list.add(this.value);
173          this.value = list;
174       }
175       // Add the new value to the list
176
((ArrayList JavaDoc)this.value).add(newValue);
177    }
178
179    public String JavaDoc getName()
180    {
181       return this.qname.getLocalPart();
182    }
183
184    public QName JavaDoc getQName()
185    {
186       return this.qname;
187    }
188
189    public static Method JavaDoc getValueSetMethod()
190    {
191       return valueSetMethod;
192    }
193
194    public ParameterDesc getParamDesc()
195    {
196       return paramDesc;
197    }
198
199    public void setParamDesc(ParameterDesc paramDesc)
200    {
201       this.paramDesc = paramDesc;
202    }
203
204    public void setXSITypeGeneration(Boolean JavaDoc value)
205    {
206       this.wantXSIType = value;
207    }
208
209    public Boolean JavaDoc getXSITypeGeneration()
210    {
211       return this.wantXSIType;
212    }
213
214    public void serialize(SerializationContext context)
215            throws IOException JavaDoc
216    {
217
218       // Set the javaType to value's class unless
219
// parameter description information exists.
220
// Set the xmlType using the parameter description
221
// information. (an xmlType=null causes the
222
// serialize method to search for a compatible xmlType)
223

224       Object JavaDoc serValue = value;
225
226       QName JavaDoc xmlType = null;
227       if (paramDesc != null)
228       {
229
230          Class JavaDoc javaType = null;
231
232          xmlType = paramDesc.getTypeQName();
233          if (xmlType != null)
234          {
235             javaType = context.getTypeMapping().getClassForQName(xmlType);
236             if (serValue != null && javaType != null && !javaType.isAssignableFrom(serValue.getClass()))
237             {
238                if (JavaUtils.isConvertable(serValue, javaType))
239                   serValue = JavaUtils.convert(serValue, javaType);
240             }
241          }
242
243          if (javaType == null)
244          {
245             javaType = paramDesc.getJavaType();
246          }
247
248          if (javaType == null && value != null)
249          {
250             javaType = value.getClass();
251          }
252
253          if (javaType != null && !javaType.equals(paramDesc.getJavaType()))
254          {
255
256             if (!(javaType.equals(JavaUtils.getHolderValueType(paramDesc.getJavaType()))))
257             {
258                // This must (assumedly) be a polymorphic type - in ALL
259
// such cases, we must send an xsi:type attribute.
260
wantXSIType = Boolean.TRUE;
261             }
262          }
263       }
264
265       context.serialize(qname, // element qname
266
null, // no extra attrs
267
serValue, // value
268
xmlType, // java/xml type
269
true, wantXSIType);
270    }
271 }
272
Popular Tags