KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > client > Stub


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
56 package org.jboss.axis.client;
57
58 import org.jboss.axis.AxisFault;
59 import org.jboss.axis.Message;
60 import org.jboss.axis.message.SOAPHeaderElementAxisImpl;
61 import org.jboss.axis.utils.Messages;
62
63 import javax.xml.namespace.QName JavaDoc;
64 import javax.xml.rpc.JAXRPCException JavaDoc;
65 import javax.xml.rpc.Service JavaDoc;
66 import java.net.MalformedURLException JavaDoc;
67 import java.net.URL JavaDoc;
68 import java.util.Iterator JavaDoc;
69 import java.util.Properties JavaDoc;
70 import java.util.Vector JavaDoc;
71
72 /**
73  * This class is the base for all generated stubs.
74  */

75
76 public abstract class Stub implements javax.xml.rpc.Stub JavaDoc
77 {
78
79    protected Service service = null;
80
81    // If maintainSessionSet is true, then setMaintainSession
82
// was called and it set the value of maintainSession.
83
// Use that value when getting the new Call object.
84
// If maintainSession HAS NOT been set, then the
85
// Call object uses the default maintainSession
86
// from the Service.
87
protected boolean maintainSessionSet = false;
88    protected boolean maintainSession = false;
89
90    protected Properties JavaDoc cachedProperties = new Properties JavaDoc();
91    protected String JavaDoc cachedUsername = null;
92    protected String JavaDoc cachedPassword = null;
93    protected URL JavaDoc cachedEndpoint = null;
94    protected Integer JavaDoc cachedTimeout = null;
95    protected QName JavaDoc cachedPortName = null;
96
97    // Support for Header
98
private Vector JavaDoc headers = new Vector JavaDoc();
99
100    // Support for Attachments
101
private Vector JavaDoc attachments = new Vector JavaDoc();
102
103    // Flag to determine whether this is the first call to register type mappings.
104
// This need not be synchronized because firstCall is ONLY called from within
105
// a synchronized block in the generated stub code.
106
private boolean firstCall = true;
107
108    /**
109     * Is this the first time the type mappings are being registered?
110     */

111    protected boolean firstCall()
112    {
113       boolean ret = firstCall;
114       firstCall = false;
115       return ret;
116    } // firstCall
117

118    /**
119     * Sets the value for a named property. JAX-RPC 1.0 specification
120     * specifies a standard set of properties that may be passed
121     * to the Stub._setProperty method. These properties include:
122     * <UL>
123     * <LI>javax.xml.rpc.security.auth.username: Username for the HTTP Basic Authentication
124     * <LI>javax.xml.rpc.security.auth.password: Password for the HTTP Basic Authentication
125     * <LI>javax.xml.rpc.service.endpoint.address: Target service endpoint address.
126     * <LI>[TBD: Additional properties]
127     * </UL>
128     *
129     * @param name - Name of the property
130     * @param value - Value of the property
131     */

132    public void _setProperty(String JavaDoc name, Object JavaDoc value)
133    {
134       if (name == null || value == null)
135       {
136          throw new JAXRPCException JavaDoc(Messages.getMessage(name == null ?
137                  "badProp03" : "badProp04"));
138       }
139       else if (name.equals(Call.USERNAME_PROPERTY))
140       {
141          if (!(value instanceof String JavaDoc))
142          {
143             throw new JAXRPCException JavaDoc(Messages.getMessage("badProp00", new String JavaDoc[]{
144                name, "java.lang.String", value.getClass().getName()}));
145          }
146          cachedUsername = (String JavaDoc)value;
147       }
148       else if (name.equals(Call.PASSWORD_PROPERTY))
149       {
150          if (!(value instanceof String JavaDoc))
151          {
152             throw new JAXRPCException JavaDoc(Messages.getMessage("badProp00", new String JavaDoc[]{
153                name, "java.lang.String", value.getClass().getName()}));
154          }
155          cachedPassword = (String JavaDoc)value;
156       }
157       else if (name.equals(Stub.ENDPOINT_ADDRESS_PROPERTY))
158       {
159          if (!(value instanceof String JavaDoc))
160          {
161             throw new JAXRPCException JavaDoc(Messages.getMessage("badProp00", new String JavaDoc[]{
162                name, "java.lang.String", value.getClass().getName()}));
163          }
164          try
165          {
166             cachedEndpoint = new URL JavaDoc((String JavaDoc)value);
167          }
168          catch (MalformedURLException JavaDoc mue)
169          {
170             throw new JAXRPCException JavaDoc(mue.getMessage());
171          }
172       }
173       else if (name.equals(Call.SESSION_MAINTAIN_PROPERTY))
174       {
175          if (!(value instanceof Boolean JavaDoc))
176          {
177             throw new JAXRPCException JavaDoc(Messages.getMessage("badProp00", new String JavaDoc[]
178             {name,
179              "java.lang.Boolean",
180              value.getClass().getName()}));
181          }
182          maintainSessionSet = true;
183          maintainSession = ((Boolean JavaDoc)value).booleanValue();
184       }
185       else if (name.startsWith("java.") || name.startsWith("javax."))
186       {
187          throw new JAXRPCException JavaDoc(Messages.getMessage("badProp05", name));
188       }
189       else
190       {
191          cachedProperties.put(name, value);
192       }
193    } // _setProperty
194

195    /**
196     * Gets the value of a named property.
197     *
198     * @param name
199     * @return the value of a named property.
200     */

201    public Object JavaDoc _getProperty(String JavaDoc name)
202    {
203       if (name != null)
204       {
205          if (name.equals(Call.USERNAME_PROPERTY))
206          {
207             return cachedUsername;
208          }
209          else if (name.equals(Call.PASSWORD_PROPERTY))
210          {
211             return cachedPassword;
212          }
213          else if (name.equals(Stub.ENDPOINT_ADDRESS_PROPERTY))
214          {
215             return cachedEndpoint.toString();
216          }
217          else if (name.equals(Call.SESSION_MAINTAIN_PROPERTY))
218          {
219             return maintainSessionSet ? new Boolean JavaDoc(maintainSession) : null;
220          }
221          else if (name.startsWith("java.") || name.startsWith("javax."))
222          {
223             throw new JAXRPCException JavaDoc(Messages.getMessage("badProp05", name));
224          }
225          else
226          {
227             return cachedProperties.get(name);
228          }
229       }
230       else
231       {
232          return null;
233       }
234    } // _getProperty
235

236    /**
237     * Remove a property from this instance of the Stub
238     * NOTE: This is NOT part of JAX-RPC and is an Axis extension.
239     *
240     * @param name the name of the property to remove
241     * @return the value to which the key had been mapped, or null if the key did not have a mapping.
242     */

243    public Object JavaDoc removeProperty(String JavaDoc name)
244    {
245       return cachedProperties.remove(name);
246    }
247
248    /**
249     * Return the names of configurable properties for this stub class.
250     */

251    public Iterator JavaDoc _getPropertyNames()
252    {
253       return cachedProperties.keySet().iterator();
254    } // _getPropertyNames
255

256    /**
257     * Set the username.
258     */

259    public void setUsername(String JavaDoc username)
260    {
261       cachedUsername = username;
262    } // setUsername
263

264    /**
265     * Get the user name
266     */

267    public String JavaDoc getUsername()
268    {
269       return cachedUsername;
270    } // getUsername
271

272    /**
273     * Set the password.
274     */

275    public void setPassword(String JavaDoc password)
276    {
277       cachedPassword = password;
278    } // setPassword
279

280    /**
281     * Get the password
282     */

283    public String JavaDoc getPassword()
284    {
285       return cachedPassword;
286    } // getPassword
287

288    /**
289     * Get the timeout value in milliseconds. 0 means no timeout.
290     */

291    public int getTimeout()
292    {
293       return cachedTimeout == null ? 0 : cachedTimeout.intValue();
294    } // getTimeout
295

296    /**
297     * Set the timeout in milliseconds.
298     */

299    public void setTimeout(int timeout)
300    {
301       cachedTimeout = new Integer JavaDoc(timeout);
302    } // setTimeout
303

304    /**
305     * Get the port name.
306     */

307    public QName JavaDoc getPortName()
308    {
309       return cachedPortName;
310    } // getPortName
311

312    /**
313     * Set the port QName.
314     */

315    public void setPortName(QName JavaDoc portName)
316    {
317       cachedPortName = portName;
318    } // setPortName
319

320    /**
321     * Set the port name.
322     */

323    public void setPortName(String JavaDoc portName)
324    {
325       setPortName(new QName JavaDoc(portName));
326    } // setPortName
327

328    /**
329     * If set to true, session is maintained; if false, it is not.
330     */

331    public void setMaintainSession(boolean session)
332    {
333       maintainSessionSet = true;
334       maintainSession = session;
335       cachedProperties.put(Call.SESSION_MAINTAIN_PROPERTY, new Boolean JavaDoc(session));
336    } // setmaintainSession
337

338
339    /**
340     * Set the header
341     *
342     * @param namespace
343     * @param partName that uniquely identify a header object.
344     * @param headerValue Object that is sent in the request as a SOAPHeader
345     */

346    public void setHeader(String JavaDoc namespace, String JavaDoc partName, String JavaDoc headerValue)
347    {
348       headers.add(new SOAPHeaderElementAxisImpl(namespace, partName, headerValue));
349    }
350
351    /**
352     * Set the header
353     */

354    public void setHeader(SOAPHeaderElementAxisImpl header)
355    {
356       headers.add(header);
357    }
358
359    /**
360     * Extract attachments
361     *
362     * @param call
363     */

364    public void extractAttachments(Call call)
365    {
366       attachments.clear();
367       Message resMessage = call.getResponseMessage();
368       if (resMessage != null && resMessage.countAttachments() > 0)
369       {
370          Iterator JavaDoc iterator = resMessage.getAttachments();
371          while (iterator.hasNext())
372          {
373             attachments.add(iterator.next());
374          }
375       }
376    }
377
378    /**
379     * Add an attachment
380     *
381     * @param handler
382     */

383    public void addAttachment(Object JavaDoc handler)
384    {
385       attachments.add(handler);
386    }
387
388    /**
389     * Get the header element
390     */

391    public SOAPHeaderElementAxisImpl getHeader(String JavaDoc namespace, String JavaDoc partName)
392    {
393       for (int i = 0; i < headers.size(); i++)
394       {
395          SOAPHeaderElementAxisImpl header = (SOAPHeaderElementAxisImpl)headers.get(i);
396          if (header.getNamespaceURI().equals(namespace) &&
397                  header.getName().equals(partName))
398             return header;
399       }
400       return null;
401    }
402
403    /**
404     * Get the array of header elements
405     */

406    public SOAPHeaderElementAxisImpl[] getHeaders()
407    {
408       SOAPHeaderElementAxisImpl[] array = new SOAPHeaderElementAxisImpl[headers.size()];
409       headers.copyInto(array);
410       return array;
411    }
412
413    /**
414     * Get the array of attachments
415     */

416    public Object JavaDoc[] getAttachments()
417    {
418       Object JavaDoc[] array = new Object JavaDoc[attachments.size()];
419       attachments.copyInto(array);
420       attachments.clear();
421       return array;
422    }
423
424    /**
425     * This method clears both requestHeaders and responseHeaders hashtables.
426     */

427    public void clearHeaders()
428    {
429       headers.clear();
430    }
431
432    /**
433     * This method clears the request attachments.
434     */

435    public void clearAttachments()
436    {
437       attachments.clear();
438    }
439
440    protected void setRequestHeaders(org.jboss.axis.client.Call call) throws AxisFault
441    {
442       SOAPHeaderElementAxisImpl[] headers = getHeaders();
443       for (int i = 0; i < headers.length; i++)
444       {
445          call.addHeader(headers[i]);
446       }
447    }
448
449    protected void setAttachments(org.jboss.axis.client.Call call) throws AxisFault
450    {
451       Object JavaDoc[] attachments = getAttachments();
452       for (int i = 0; i < attachments.length; i++)
453       {
454          call.addAttachmentPart(attachments[i]);
455       }
456    }
457
458    /**
459     * Helper method for updating headers from the response.
460     * <p/>
461     * Deprecated, since response headers should not be
462     * automatically reflected back into the stub list.
463     *
464     * @deprecated This method has been changed to a no-op but remains
465     * in the code to keep compatibility with pre-1.1
466     * generated stubs.
467     */

468    protected void getResponseHeaders(org.jboss.axis.client.Call call) throws AxisFault
469    {
470    }
471
472 }
473
Popular Tags