KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.axis.message;
17
18 import org.apache.axis.components.logger.LogFactory;
19 import org.apache.axis.description.ParameterDesc;
20 import org.apache.axis.encoding.SerializationContext;
21 import org.apache.axis.utils.JavaUtils;
22 import org.apache.axis.utils.Messages;
23 import org.apache.axis.constants.Style;
24 import org.apache.axis.Constants;
25 import org.apache.axis.MessageContext;
26 import org.apache.commons.logging.Log;
27
28 import javax.xml.namespace.QName JavaDoc;
29 import javax.xml.soap.SOAPElement JavaDoc;
30 import javax.xml.soap.SOAPException JavaDoc;
31
32 import java.io.IOException JavaDoc;
33 import java.io.ObjectInputStream JavaDoc;
34 import java.io.ObjectOutputStream JavaDoc;
35 import java.io.Serializable JavaDoc;
36 import java.lang.reflect.Method JavaDoc;
37 import java.util.ArrayList JavaDoc;
38
39 /** An RPC parameter
40  *
41  * @author Glen Daniels (gdaniels@apache.org)
42  */

43 public class RPCParam extends MessageElement implements Serializable JavaDoc
44 {
45     protected static Log log =
46         LogFactory.getLog(RPCParam.class.getName());
47
48     private Object JavaDoc value = null;
49     private int countSetCalls = 0; // counts number of calls to set
50

51     private ParameterDesc paramDesc;
52
53     /**
54      * Do we definitely want (or don't want) to send xsi:types? If null
55      * (the default), just do whatever our SerializationContext is configured
56      * to do. If TRUE or FALSE, the SerializationContext will do what we
57      * want.
58      */

59     private Boolean JavaDoc wantXSIType = null;
60
61     private static Method JavaDoc valueSetMethod;
62     static {
63         Class JavaDoc cls = RPCParam.class;
64         try {
65             valueSetMethod = cls.getMethod("set", new Class JavaDoc[] {Object JavaDoc.class});
66         } catch (NoSuchMethodException JavaDoc e) {
67             log.error(Messages.getMessage("noValue00", "" + e));
68             throw new RuntimeException JavaDoc(e.getMessage());
69         }
70     }
71
72     /** Constructor for building up messages.
73      */

74     public RPCParam(String JavaDoc name, Object JavaDoc value)
75     {
76         this(new QName JavaDoc("", name), value);
77     }
78
79     public RPCParam(QName JavaDoc qname, Object JavaDoc value)
80     {
81         super(qname);
82         if (value instanceof java.lang.String JavaDoc) {
83             try {
84                 this.addTextNode((String JavaDoc) value);
85             } catch (SOAPException JavaDoc e) {
86                 throw new RuntimeException JavaDoc(Messages.getMessage("cannotCreateTextNode00"));
87             }
88         } else {
89             this.value = value;
90         }
91     }
92
93     public RPCParam(String JavaDoc namespace, String JavaDoc name, Object JavaDoc value)
94     {
95         this(new QName JavaDoc(namespace, name), value);
96     }
97     
98     public void setRPCCall(RPCElement call)
99     {
100         parent = call;
101     }
102     
103     public Object JavaDoc getObjectValue()
104     {
105         return value;
106     }
107     
108     public void setObjectValue(Object JavaDoc value)
109     {
110         this.value = value;
111     }
112
113     /**
114      * This set method is registered during deserialization
115      * to set the deserialized value.
116      * If the method is called multiple times, the
117      * value is automatically changed into a container to
118      * hold all of the values.
119      * @param newValue is the deserialized object
120      */

121     public void set(Object JavaDoc newValue) {
122         countSetCalls++;
123         // If this is the first call,
124
// simply set the value.
125
if (countSetCalls==1) {
126             this.value = newValue;
127             return;
128         }
129         // If this is the second call, create an
130
// ArrayList to hold all the values
131
else if (countSetCalls==2) {
132             ArrayList JavaDoc list = new ArrayList JavaDoc();
133             list.add(this.value);
134             this.value = list;
135         }
136         // Add the new value to the list
137
((ArrayList JavaDoc) this.value).add(newValue);
138     }
139
140     public static Method JavaDoc getValueSetMethod()
141     {
142         return valueSetMethod;
143     }
144
145     public ParameterDesc getParamDesc() {
146         return paramDesc;
147     }
148
149     public void setParamDesc(ParameterDesc paramDesc) {
150         this.paramDesc = paramDesc;
151     }
152
153     public void setXSITypeGeneration(Boolean JavaDoc value) {
154         this.wantXSIType = value;
155     }
156
157     public Boolean JavaDoc getXSITypeGeneration() {
158         return this.wantXSIType;
159     }
160
161     public void serialize(SerializationContext context)
162         throws IOException JavaDoc
163     {
164         // Set the javaType to value's class unless
165
// parameter description information exists.
166
// Set the xmlType using the parameter description
167
// information. (an xmlType=null causes the
168
// serialize method to search for a compatible xmlType)
169
Class JavaDoc javaType = value == null ? null: value.getClass();
170         QName JavaDoc xmlType = null;
171         if (paramDesc != null) {
172             if (javaType == null) {
173                 javaType = paramDesc.getJavaType() != null ?
174                     paramDesc.getJavaType(): javaType;
175             } else if (!(javaType.equals(paramDesc.getJavaType()))) {
176                 Class JavaDoc clazz = JavaUtils.getPrimitiveClass(javaType);
177                 if(clazz == null || !clazz.equals(paramDesc.getJavaType())) {
178                     if (!(javaType.equals(
179                             JavaUtils.getHolderValueType(paramDesc.getJavaType())))) {
180                         
181                         // This must (assumedly) be a polymorphic type - in ALL
182
// such cases, we must send an xsi:type attribute.
183
wantXSIType = Boolean.TRUE;
184                     }
185                 }
186             }
187             xmlType = paramDesc.getTypeQName();
188             QName JavaDoc itemQName = paramDesc.getItemQName();
189             if (itemQName == null) {
190                 MessageContext mc = context.getMessageContext();
191                 if (mc != null && mc.getOperation() != null && mc.getOperation().getStyle() == Style.DOCUMENT) {
192                     itemQName = Constants.QNAME_LITERAL_ITEM;
193                 }
194             }
195             context.setItemQName(itemQName);
196
197             QName JavaDoc itemType = paramDesc.getItemType();
198             context.setItemType(itemType);
199         }
200         context.serialize(getQName(), // element qname
201
null, // no extra attrs
202
value, // value
203
xmlType, // java/xml type
204
Boolean.TRUE, wantXSIType);
205     }
206
207     private void writeObject(ObjectOutputStream JavaDoc out)
208         throws IOException JavaDoc {
209         if (getQName() == null) {
210             out.writeBoolean(false);
211         } else {
212             out.writeBoolean(true);
213             out.writeObject(getQName().getNamespaceURI());
214             out.writeObject(getQName().getLocalPart());
215         }
216         out.defaultWriteObject();
217     }
218
219     private void readObject(ObjectInputStream JavaDoc in)
220         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
221         if (in.readBoolean()) {
222             setQName(new QName JavaDoc((String JavaDoc)in.readObject(),
223                               (String JavaDoc)in.readObject()));
224         }
225         in.defaultReadObject();
226     }
227
228     protected void outputImpl(SerializationContext context) throws Exception JavaDoc {
229         serialize(context);
230     }
231
232     public String JavaDoc getValue() {
233         return getValueDOM();
234     }
235
236     /**
237      * @see javax.xml.soap.SOAPElement#addTextNode(java.lang.String)
238      */

239     public SOAPElement JavaDoc addTextNode(String JavaDoc s) throws SOAPException JavaDoc {
240         value = s;
241         return super.addTextNode(s);
242     }
243     /**
244      * @see javax.xml.soap.Node#setValue(java.lang.String)
245      */

246     public void setValue(String JavaDoc value) {
247         this.value = value;
248         super.setValue(value);
249     }
250 }
251
Popular Tags