| 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; |
| 29 |
import javax.xml.soap.SOAPElement; |
| 30 |
import javax.xml.soap.SOAPException; |
| 31 |
|
| 32 |
import java.io.IOException; |
| 33 |
import java.io.ObjectInputStream; |
| 34 |
import java.io.ObjectOutputStream; |
| 35 |
import java.io.Serializable; |
| 36 |
import java.lang.reflect.Method; |
| 37 |
import java.util.ArrayList; |
| 38 |
|
| 39 |
/** An RPC parameter |
| 40 |
* |
| 41 |
* @author Glen Daniels ([email protected]) |
| 42 |
*/ |
| 43 |
public class RPCParam extends MessageElement implements Serializable |
| 44 |
{ |
| 45 |
protected static Log log = |
| 46 |
LogFactory.getLog(RPCParam.class.getName()); |
| 47 |
|
| 48 |
private Object 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 wantXSIType = null; |
| 60 |
|
| 61 |
private static Method valueSetMethod; |
| 62 |
static { |
| 63 |
Class cls = RPCParam.class; |
| 64 |
try { |
| 65 |
valueSetMethod = cls.getMethod("set", new Class[] {Object.class}); |
| 66 |
} catch (NoSuchMethodException e) { |
| 67 |
log.error(Messages.getMessage("noValue00", "" + e)); |
| 68 |
throw new RuntimeException(e.getMessage()); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** Constructor for building up messages. |
| 73 |
*/ |
| 74 |
public RPCParam(String name, Object value) |
| 75 |
{ |
| 76 |
this(new QName("", name), value); |
| 77 |
} |
| 78 |
|
| 79 |
public RPCParam(QName qname, Object value) |
| 80 |
{ |
| 81 |
super(qname); |
| 82 |
if (value instanceof java.lang.String) { |
| 83 |
try { |
| 84 |
this.addTextNode((String) value); |
| 85 |
} catch (SOAPException e) { |
| 86 |
throw new RuntimeException(Messages.getMessage("cannotCreateTextNode00")); |
| 87 |
} |
| 88 |
} else { |
| 89 |
this.value = value; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
public RPCParam(String namespace, String name, Object value) |
| 94 |
{ |
| 95 |
this(new QName(namespace, name), value); |
| 96 |
} |
| 97 |
|
| 98 |
public void setRPCCall(RPCElement call) |
| 99 |
{ |
| 100 |
parent = call; |
| 101 |
} |
| 102 |
|
| 103 |
public Object getObjectValue() |
| 104 |
{ |
| 105 |
return value; |
| 106 |
} |
| 107 |
|
| 108 |
public void setObjectValue(Object 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 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 list = new ArrayList(); |
| 133 |
list.add(this.value); |
| 134 |
this.value = list; |
| 135 |
} |
| 136 |
// Add the new value to the list |
| 137 |
((ArrayList) this.value).add(newValue); |
| 138 |
} |
| 139 |
|
| 140 |
public static Method 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 value) { |
| 154 |
this.wantXSIType = value; |
| 155 |
} |
| 156 |
|
| 157 |
public Boolean getXSITypeGeneration() { |
| 158 |
return this.wantXSIType; |
| 159 |
} |
| 160 |
|
| 161 |
public void serialize(SerializationContext context) |
| 162 |
throws IOException |
| 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 javaType = value == null ? null: value.getClass(); |
| 170 |
QName xmlType = null; |
| 171 |
// we'll send a null unless our description tells us |
| 172 |
// that we may be omitted |
| 173 |
Boolean sendNull = Boolean.TRUE; |
| 174 |
if (paramDesc != null) { |
| 175 |
if (javaType == null) { |
| 176 |
javaType = paramDesc.getJavaType() != null ? |
| 177 |
paramDesc.getJavaType(): javaType; |
| 178 |
} else if (!(javaType.equals(paramDesc.getJavaType()))) { |
| 179 |
Class clazz = JavaUtils.getPrimitiveClass(javaType); |
| 180 |
if(clazz == null || !clazz.equals(paramDesc.getJavaType())) { |
| 181 |
if (!(javaType.equals( |
| 182 |
JavaUtils.getHolderValueType(paramDesc.getJavaType())))) { |
| 183 |
|
| 184 |
// This must (assumedly) be a polymorphic type - in ALL |
| 185 |
// such cases, we must send an xsi:type attribute. |
| 186 |
wantXSIType = Boolean.TRUE; |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
xmlType = paramDesc.getTypeQName(); |
| 191 |
QName itemQName = paramDesc.getItemQName(); |
| 192 |
if (itemQName == null) { |
| 193 |
MessageContext mc = context.getMessageContext(); |
| 194 |
if (mc != null && mc.getOperation() != null && mc.getOperation().getStyle() == Style.DOCUMENT) { |
| 195 |
itemQName = Constants.QNAME_LITERAL_ITEM; |
| 196 |
} |
| 197 |
} |
| 198 |
context.setItemQName(itemQName); |
| 199 |
|
| 200 |
QName itemType = paramDesc.getItemType(); |
| 201 |
context.setItemType(itemType); |
| 202 |
|
| 203 |
// don't send anything if we're able to be omitted, |
| 204 |
// although we'll prefer to send xsi:nill if possible |
| 205 |
if (paramDesc.isOmittable() && !paramDesc.isNillable()) |
| 206 |
sendNull = Boolean.FALSE; |
| 207 |
} |
| 208 |
context.serialize(getQName(), // element qname |
| 209 |
null, // no extra attrs |
| 210 |
value, // value |
| 211 |
xmlType, // java/xml type |
| 212 |
sendNull, wantXSIType); |
| 213 |
} |
| 214 |
|
| 215 |
private void writeObject(ObjectOutputStream out) |
| 216 |
throws IOException { |
| 217 |
if (getQName() == null) { |
| 218 |
out.writeBoolean(false); |
| 219 |
} else { |
| 220 |
out.writeBoolean(true); |
| 221 |
out.writeObject(getQName().getNamespaceURI()); |
| 222 |
out.writeObject(getQName().getLocalPart()); |
| 223 |
} |
| 224 |
out.defaultWriteObject(); |
| 225 |
} |
| 226 |
|
| 227 |
private void readObject(ObjectInputStream in) |
| 228 |
throws IOException, ClassNotFoundException { |
| 229 |
if (in.readBoolean()) { |
| 230 |
setQName(new QName((String)in.readObject(), |
| 231 |
(String)in.readObject())); |
| 232 |
} |
| 233 |
in.defaultReadObject(); |
| 234 |
} |
| 235 |
|
| 236 |
protected void outputImpl(SerializationContext context) throws Exception { |
| 237 |
serialize(context); |
| 238 |
} |
| 239 |
|
| 240 |
public String getValue() { |
| 241 |
return getValueDOM(); |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* @see javax.xml.soap.SOAPElement#addTextNode(java.lang.String) |
| 246 |
*/ |
| 247 |
public SOAPElement addTextNode(String s) throws SOAPException { |
| 248 |
value = s; |
| 249 |
return super.addTextNode(s); |
| 250 |
} |
| 251 |
/** |
| 252 |
* @see javax.xml.soap.Node#setValue(java.lang.String) |
| 253 |
*/ |
| 254 |
public void setValue(String value) { |
| 255 |
this.value = value; |
| 256 |
super.setValue(value); |
| 257 |
} |
| 258 |
} |