KickJava   Java API By Example, From Geeks To Geeks.

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


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.jboss.axis.message;
18
19 import org.jboss.axis.AxisFault;
20 import org.jboss.axis.NotImplementedException;
21 import org.jboss.axis.encoding.DeserializationContext;
22 import org.jboss.logging.Logger;
23 import org.w3c.dom.DOMException JavaDoc;
24 import org.xml.sax.Attributes JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26
27 import javax.xml.namespace.QName JavaDoc;
28 import javax.xml.soap.Name JavaDoc;
29 import javax.xml.soap.SOAPElement JavaDoc;
30 import javax.xml.soap.SOAPException JavaDoc;
31 import java.math.BigDecimal JavaDoc;
32 import java.math.BigInteger JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Arrays JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Vector JavaDoc;
38
39 /**
40  * Abstracts an RPC parameter as SOAPElement
41  * <p/>
42  * This is a hack that bridges between the SOAPElement that represents a RPC parameter
43  * and the actual RPCParam object which lives in a list inside the RPCElement. As
44  * long as the RPCParam is not a true javax.xml.soap.Node, clients cannot use the saaj
45  * api to modify its value.
46  * <p/>
47  * This class should eventually become the RPCParam.
48  *
49  * @author Thomas Diesler (thomas.diesler@jboss.org)
50  */

51 public class RPCParamElementImpl extends SOAPElementAxisImpl
52 {
53
54    private static Logger log = Logger.getLogger(RPCParamElementImpl.class.getName());
55
56    // The parent of this rpc param
57
private RPCElement rpcElement;
58    private DeserializationContext context;
59
60    public RPCParamElementImpl(String JavaDoc namespace, String JavaDoc localPart, String JavaDoc prefix, Attributes JavaDoc attributes, DeserializationContext context) throws AxisFault
61    {
62       super(namespace, localPart, prefix, attributes, context);
63
64       SOAPElement JavaDoc curEl = context.getCurElement();
65       if (curEl instanceof RPCParamElementImpl)
66          rpcElement = ((RPCParamElementImpl)curEl).rpcElement;
67
68       if (curEl instanceof RPCElement)
69          rpcElement = (RPCElement)curEl;
70
71       if (rpcElement == null)
72       {
73          IllegalArgumentException JavaDoc ex = new IllegalArgumentException JavaDoc("Unexpected element type: " + curEl.getClass().getName());
74          log.error(ex.getMessage(), ex);
75          throw ex;
76       }
77
78       this.context = context;
79    }
80
81    public RPCParamElementImpl(RPCParam rpcParam)
82    {
83       super(rpcParam.getQName(), rpcParam.getValue());
84       rpcElement = rpcParam.myCall;
85
86       if (rpcElement == null)
87       {
88          IllegalArgumentException JavaDoc ex = new IllegalArgumentException JavaDoc("RPCParam has no parent element");
89          log.error(ex.getMessage(), ex);
90          throw ex;
91       }
92
93       // Added to build the soap tree such that it contains SOAPElements for the parameters
94
// Revisit to do this properly for the different encoding styles, arrays, value types, etc.
95
// TDI 06-June-2006
96

97       List JavaDoc supported = Arrays.asList(new Class JavaDoc[]{
98          String JavaDoc.class, Integer JavaDoc.class, Float JavaDoc.class, Double JavaDoc.class, Long JavaDoc.class, Boolean JavaDoc.class,
99          Short JavaDoc.class, Byte JavaDoc.class, BigInteger JavaDoc.class, BigDecimal JavaDoc.class
100       });
101
102       Object JavaDoc value = rpcParam.getValue();
103       if (value != null)
104       {
105          try
106          {
107             if (supported.contains(value.getClass()))
108             {
109                super.addTextNode(value.toString());
110             }
111             else
112             {
113                log.debug("Cannot add text node for rpc parameter type: " + value.getClass().getName());
114             }
115          }
116          catch (SOAPException JavaDoc e)
117          {
118             log.error("Cannot addTextNode: " + value, e);
119          }
120       }
121    }
122
123    public Iterator JavaDoc getChildElements()
124    {
125       if (!hasChildNodes() && objectValue != null)
126       {
127          throw new NotImplementedException("SOAPElement view of RPCParam not implemented");
128       }
129       else
130       {
131          return super.getChildElements();
132       }
133    }
134
135    public Iterator JavaDoc getChildElements(Name JavaDoc name)
136    {
137       if (!hasChildNodes() && objectValue != null)
138       {
139          throw new NotImplementedException("SOAPElement view of RPCParam not implemented");
140       }
141       else
142       {
143          return super.getChildElements(name);
144       }
145    }
146
147    /**
148     * Try to keep the RPCParam object in sync
149     */

150    public SOAPElement JavaDoc addTextNode(String JavaDoc value) throws SOAPException JavaDoc
151    {
152       SOAPElement JavaDoc txtNode = super.addTextNode(value);
153       setRPCParamValue(value);
154       return txtNode;
155    }
156
157    /**
158     * Try to keep the RPCParam object in sync
159     */

160    public void setValue(String JavaDoc value)
161    {
162       super.setValue(value);
163       setRPCParamValue(value);
164    }
165
166    /**
167     * Try to keep the RPCParam object in sync
168     */

169    public void setNodeValue(String JavaDoc value) throws DOMException JavaDoc
170    {
171       super.setNodeValue(value);
172       setRPCParamValue(value);
173    }
174
175    /**
176     * Set the value in the RPCParam object, if found
177     */

178    private void setRPCParamValue(String JavaDoc newValue)
179    {
180       // The context is null when the client side build this object
181
// before it sends the message on the wire
182
if (context == null || context.isDoneParsing())
183       {
184          RPCParam rpcParam = getRPCParam();
185          Object JavaDoc paramValue = rpcParam.getValue();
186
187          if (paramValue != null && !paramValue.getClass().isAssignableFrom(newValue.getClass()))
188             log.warn("Trying to change the value type from " + paramValue.getClass().getName() + " to " + newValue.getClass().getName());
189
190          rpcParam.setValue(newValue);
191       }
192    }
193
194    /**
195     * Search through the list of parameters in the rpcElement and return the one and only one
196     * that has the same QName as this SOAPElement
197     */

198    private RPCParam getRPCParam()
199    {
200
201       RPCParam rpcParam = null;
202
203       QName JavaDoc rpcParamElementName = getQName();
204       ArrayList JavaDoc weHave = new ArrayList JavaDoc();
205       try
206       {
207          Vector JavaDoc params = rpcElement.getParams();
208          for (int i = 0; i < params.size(); i++)
209          {
210
211             RPCParam aux = (RPCParam)params.elementAt(i);
212             QName JavaDoc rpcParamName = aux.getQName();
213             weHave.add(rpcParamName);
214
215             if (rpcParamName.equals(rpcParamElementName))
216             {
217                if (rpcParam != null)
218                   log.error("Duplicate parameter: " + rpcParamName);
219                else
220                   rpcParam = aux;
221             }
222          }
223       }
224       catch (SAXException JavaDoc e)
225       {
226          log.error("Cannot get RPCParam " + rpcParamElementName, e);
227       }
228
229       if (rpcParam == null)
230          log.warn("Cannot find parameter: " + getQName() + " we have " + weHave);
231
232       return rpcParam;
233    }
234 }
235
Popular Tags