KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > webservice > client > CallImpl


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 // $Id: CallImpl.java,v 1.9.2.5 2005/03/02 14:32:13 tdiesler Exp $
9
package org.jboss.webservice.client;
10
11 // $Id: CallImpl.java,v 1.9.2.5 2005/03/02 14:32:13 tdiesler Exp $
12

13 import org.jboss.axis.Message;
14 import org.jboss.logging.Logger;
15 import org.jboss.webservice.deployment.OperationDescription;
16 import org.jboss.webservice.deployment.ServiceDescription;
17
18 import javax.activation.DataHandler JavaDoc;
19 import javax.activation.DataSource JavaDoc;
20 import javax.activation.URLDataSource JavaDoc;
21 import javax.mail.internet.MimeMultipart JavaDoc;
22 import javax.xml.soap.AttachmentPart JavaDoc;
23 import javax.xml.transform.Source JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.rmi.RemoteException JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 /**
34  * A Call object that is ws4ee aware.
35  * <p/>
36  * It takes the jaxrpc-mapping into account when setting up the call.
37  *
38  * @author Thomas.Diesler@jboss.org
39  * @since 29-May-2004
40  */

41 public class CallImpl extends org.jboss.axis.client.Call
42 {
43    // provide logging
44
private static final Logger log = Logger.getLogger(CallImpl.class);
45
46    private ServiceImpl jaxrpcService;
47    private OperationDescription opDescription;
48
49    // Attachment objects keyed by contentID
50
private Map JavaDoc attachments = new HashMap JavaDoc();
51
52    /**
53     * Constructs a new Call object for a given jaxrpcService
54     */

55    public CallImpl(ServiceImpl service)
56    {
57       super(service);
58       this.jaxrpcService = service;
59    }
60
61    /**
62     * Build a call from a URL string
63     *
64     * @param url the target endpoint URL
65     * @throws java.net.MalformedURLException
66     */

67    public CallImpl(Object JavaDoc url) throws MalformedURLException JavaDoc
68    {
69       this(new ServiceImpl());
70       setTargetEndpointAddress(new URL JavaDoc(url.toString()));
71    }
72
73    /**
74     * The super implementation fills in as much as it can from the wsdl.
75     * This is the main entry point for axis to setup the call, so far we have nothing to add.
76     * The only thing we do, is helping axis to find the wsdl operation.
77     * This has been stubbed out, see below.
78     */

79    public void setOperation(String JavaDoc javaOpName)
80    {
81       super.setOperation(javaOpName);
82
83       org.jboss.axis.description.OperationDesc axisOp = getOperation();
84       String JavaDoc wsdlOpName = getOperationName().getLocalPart();
85
86       // Find the operation in our description
87
String JavaDoc portName = (getPortName() != null ? getPortName().getLocalPart() : null);
88       ServiceDescription serviceDesc = jaxrpcService.getServiceDescription(portName);
89       Properties JavaDoc callProperties = serviceDesc.getCallProperties();
90       if (callProperties != null)
91       {
92          // Set the default call properties
93
Iterator JavaDoc keys = callProperties.keySet().iterator();
94          while (keys.hasNext())
95          {
96             String JavaDoc key = (String JavaDoc)keys.next();
97             String JavaDoc value = callProperties.getProperty(key);
98             this.setProperty(key, value);
99          }
100       }
101
102       // Reset the operation description
103
this.opDescription = null;
104
105       Iterator JavaDoc itOp = serviceDesc.getOperations();
106       while (opDescription == null && itOp.hasNext())
107       {
108          OperationDescription operation = (OperationDescription)itOp.next();
109          if (operation.getWsdlName().equals(wsdlOpName))
110             opDescription = operation;
111       }
112
113       if (opDescription != null)
114       {
115          if (serviceDesc.getStyle().equals(axisOp.getStyle()) == false)
116          {
117             log.debug("Fixing style: [was=" + axisOp.getStyle() + ",is=" + serviceDesc.getStyle() + "]");
118             axisOp.setStyle(serviceDesc.getStyle());
119          }
120
121          if (serviceDesc.getUse().equals(axisOp.getUse()) == false)
122          {
123             log.debug("Fixing use: [was=" + axisOp.getUse() + ",is=" + serviceDesc.getUse() + "]");
124             axisOp.setUse(serviceDesc.getUse());
125          }
126       }
127       else
128       {
129          log.warn("Cannot find operation description for: " + wsdlOpName);
130       }
131    }
132
133    /**
134     * The default implementation simply returns the java method name.
135     * A ws4ee implementation would take the jaxrpc-mapping file into consideration
136     * and return the corresponding wsdl operation
137     */

138    protected String JavaDoc getWsdlOpName(String JavaDoc javaOpName)
139    {
140       String JavaDoc wsdlOpName = javaOpName;
141
142       ServiceDescription serviceDesc = getServiceDescription();
143       if (serviceDesc != null)
144       {
145          Iterator JavaDoc it = serviceDesc.getOperations();
146          while (it.hasNext())
147          {
148             OperationDescription operation = (OperationDescription)it.next();
149             if (javaOpName.equals(operation.getJavaName()))
150             {
151                if (wsdlOpName.equals(operation.getWsdlName()) == false)
152                {
153                   wsdlOpName = operation.getWsdlName();
154                   log.debug("Replacing operation name '" + javaOpName + "' with '" + wsdlOpName + "'");
155                }
156             }
157          }
158       }
159
160       return wsdlOpName;
161    }
162
163    /** Get the ServiceDescription for this Call */
164    private ServiceDescription getServiceDescription()
165    {
166       ServiceDescription serviceDesc = null;
167       if (jaxrpcService != null)
168       {
169          String JavaDoc portName = (getPortName() != null ? getPortName().getLocalPart() : null);
170          serviceDesc = jaxrpcService.getServiceDescription(portName);
171       }
172       return serviceDesc;
173    }
174
175    /** Add an attachment with a given contentID
176     *
177     * See <code>addAttachmentParts</code> for a list of supported types
178     *
179     * @param contentID the attachments contentID
180     * @param mimepart the attachment part
181     */

182    public void addAttachment(String JavaDoc contentID, Object JavaDoc mimepart)
183    {
184       attachments.put(contentID, mimepart);
185    }
186
187    /** Get an iterator over the available contentIDs */
188    public Iterator JavaDoc getAttachmentIdentifiers()
189    {
190       return Collections.unmodifiableSet(attachments.keySet()).iterator();
191    }
192
193    /** Get the attachment for the given contentID. */
194    public Object JavaDoc getAttachment(String JavaDoc contentID)
195    {
196       return attachments.get(contentID);
197    }
198
199    /** Remove the attachment for the given contentID. */
200    public void removeAttachment(String JavaDoc contentID)
201    {
202       attachments.remove(contentID);
203    }
204
205    /** Add attachment parts to the SOAP message
206     */

207    protected void addAttachmentParts(Message msg)
208    {
209       Iterator JavaDoc it = getAttachmentIdentifiers();
210       while (it.hasNext())
211       {
212          String JavaDoc contentID = (String JavaDoc)it.next();
213          Object JavaDoc part = getAttachment(contentID);
214
215          AttachmentPart JavaDoc ap = null;
216          if (part instanceof String JavaDoc)
217          {
218             ap = msg.createAttachmentPart(part, "text/plain");
219          }
220          else if (part instanceof Source JavaDoc)
221          {
222             ap = msg.createAttachmentPart(part, "application/xml");
223          }
224          else if (part instanceof URL JavaDoc)
225          {
226             DataSource JavaDoc ds = new URLDataSource JavaDoc((URL JavaDoc)part);
227             ap = msg.createAttachmentPart(new DataHandler JavaDoc(ds));
228          }
229          else if (part instanceof DataHandler JavaDoc)
230          {
231             ap = msg.createAttachmentPart((DataHandler JavaDoc)part);
232          }
233          else if (part instanceof MimeMultipart JavaDoc)
234          {
235             ap = msg.createAttachmentPart((MimeMultipart JavaDoc)part, "multipart/mixed");
236          }
237
238          if (ap == null)
239             throw new IllegalArgumentException JavaDoc("Unsupported attachment part: " + part);
240
241          ap.setContentId(contentID);
242
243          // Add the part to the axis call
244
attachmentParts.add(ap);
245       }
246
247       super.addAttachmentParts(msg);
248       attachments.clear();
249    }
250
251    /** Calls the super implementation with either rpc or one-way call semantics.
252     */

253    public Object JavaDoc invoke(Object JavaDoc[] params) throws RemoteException JavaDoc
254    {
255       try
256       {
257          if (opDescription != null && opDescription.isOneWay())
258          {
259             log.debug("Using one-way call semantics for: " + getOperationName());
260             super.invokeOneWay(params);
261             return null;
262          }
263          else
264          {
265             return super.invoke(params);
266          }
267       }
268       finally
269       {
270          // Release the message context. This cannot be done in the super method since
271
// the Axis generated stubs extractAttachments from the call after invoke returns.
272
msgContext = null;
273
274          // Clear the headers. Maybe this should be done in Axis, but again this might break generated stubs.
275
clearHeaders();
276       }
277    }
278 }
279
Popular Tags