KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > providers > soap > apacheaxis > WSIFPort_ApacheAxis


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 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 "WSIF" 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 and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.wsif.providers.soap.apacheaxis;
59
60 import java.net.MalformedURLException JavaDoc;
61 import java.net.URL JavaDoc;
62 import java.util.HashMap JavaDoc;
63 import java.util.List JavaDoc;
64 import java.util.Map JavaDoc;
65
66 import javax.wsdl.Binding;
67 import javax.wsdl.BindingOperation;
68 import javax.wsdl.Definition;
69 import javax.wsdl.Port;
70 import javax.wsdl.extensions.soap.SOAPAddress;
71 import javax.wsdl.extensions.soap.SOAPBinding;
72 import javax.wsdl.extensions.soap.SOAPOperation;
73 import javax.xml.namespace.QName JavaDoc;
74 import javax.xml.rpc.JAXRPCException JavaDoc;
75
76 import org.apache.axis.client.Call;
77 import org.apache.axis.client.Transport;
78 import org.apache.axis.transport.http.HTTPTransport;
79 import org.apache.wsif.WSIFException;
80 import org.apache.wsif.WSIFOperation;
81 import org.apache.wsif.base.WSIFDefaultPort;
82 import org.apache.wsif.logging.Trc;
83 import org.apache.wsif.providers.WSIFDynamicTypeMap;
84 import org.apache.wsif.util.WSIFProperties;
85 import org.apache.wsif.util.WSIFUtils;
86 import org.apache.wsif.util.jms.WSIFJMSDestination;
87 import org.apache.wsif.util.jms.WSIFJMSFinder;
88 import org.apache.wsif.wsdl.extensions.jms.JMSAddress;
89
90 /**
91  * This class implements a WSDL SOAP binding using Apache AXIS.
92  * See section 3 of the WSDL 1.1 specification for details
93  * of the WSDL SOAP binding. WSIF extends this standard SOAP
94  * binding with the WSIF extensions for SOAP over JMS.
95  *
96  * @author Mark Whitlock <whitlock@apache.org>
97  * @author Ant Elder <ant.elder@uk.ibm.com>
98  */

99 public class WSIFPort_ApacheAxis extends WSIFDefaultPort {
100
101     private static final long serialVersionUID = 2L;
102
103     protected Definition definition;
104     protected Port port;
105     protected SOAPBinding soapBinding;
106     protected SOAPAddress soapAddress;
107     protected JMSAddress jmsAddress;
108
109     protected String JavaDoc bindingStyle;
110     protected URL JavaDoc endPointURL;
111     protected List JavaDoc jmsAddressPropVals;
112
113     protected WSIFDynamicTypeMap wsifdynamictypemap;
114     protected Map JavaDoc cachedWSIFOperations;
115
116     transient protected Transport transport;
117     transient protected Call call;
118
119     private static final String JavaDoc HTTP_TRANSPORT_URI =
120         WSIFAXISConstants.HTTP_TRANSPORT_URI;
121     private static final String JavaDoc JMS_TRANSPORT_URI =
122         WSIFAXISConstants.JMS_TRANSPORT_URI;
123
124     /**
125      * Construct a new WSIFPort
126      */

127     public WSIFPort_ApacheAxis(
128         Definition definition,
129         Port port,
130         SOAPBinding soapBinding,
131         WSIFDynamicTypeMap wsifdynamictypemap)
132         throws WSIFException {
133         Trc.entry(this, definition, port, soapBinding, wsifdynamictypemap);
134
135         this.definition = definition;
136         this.port = port;
137         this.soapBinding = soapBinding;
138         this.wsifdynamictypemap = wsifdynamictypemap;
139
140         parseSoapBinding();
141         parseServiceAddress();
142
143         if (Trc.ON)
144             Trc.exit(deep());
145     }
146
147     /**
148      * Gets the soap:binding WSDL element and validates its attributes
149      * The soap:binding WSDL element has the form:
150      * <soap:binding style="rpc|document" transport="uri">
151      */

152     private void parseSoapBinding() throws WSIFException {
153         this.bindingStyle = soapBinding.getStyle();
154         if (bindingStyle == null || bindingStyle.length() < 1) {
155             bindingStyle = WSIFAXISConstants.STYLE_DOCUMENT;
156         } else if (!WSIFAXISConstants.VALID_STYLES.contains(bindingStyle)) {
157             throw new WSIFException(
158                 "unsupported style '"
159                     + bindingStyle
160                     + "' for binding:"
161                     + soapBinding);
162         }
163
164         String JavaDoc transportURI = soapBinding.getTransportURI();
165         if (!WSIFAXISConstants.VALID_TRANSPORTS.contains(transportURI)) {
166             throw new WSIFException(
167                 "unsupported transport '"
168                     + transportURI
169                     + "' for binding: "
170                     + soapBinding);
171         }
172     }
173
174     /**
175      * Gets the soap:address or jms:address WSDL element from the service port
176      * and validates it against the binding transport.
177      * <port .... >
178      * <soap:address location="uri"/>?
179      * <jms:address destinationStyle="topic|queue"
180      * jmsVendorURI="uri"?
181      * initialContextFactory="uri"?
182      * jndiProviderURL="uri"?
183      * jndiConnectionFactoryName="nmtoken"
184      * jndiDestinationName="nmtoken"
185      * <jms:propertyValue
186      * name="nmtoken" value="nmtoken" type="qname">*
187      * </jms:address>?
188      * <jms:address jmsVendorURI="uri"
189      * jmsImplementationSpecificURI="uri"
190      * <jms:propertyValue
191      * name="nmtoken" value="nmtoken" type="qname">*
192      * </jms:address>?
193      * </port>
194      */

195     private void parseServiceAddress() throws WSIFException {
196         this.soapAddress =
197             (SOAPAddress) getExtElem(port,
198                 SOAPAddress.class,
199                 port.getExtensibilityElements());
200         this.jmsAddress =
201             (JMSAddress) getExtElem(port,
202                 JMSAddress.class,
203                 port.getExtensibilityElements());
204
205         if (soapAddress != null && jmsAddress != null)
206             throw new WSIFException(
207                 "Both soap:address and jms:address cannot be specified for port "
208                     + port);
209
210         if (soapAddress == null && jmsAddress == null)
211             throw new WSIFException(
212                 "Either soap:address or jms:address must be specified for port "
213                     + port);
214
215         if (isTransportHTTP() && soapAddress == null) {
216             throw new WSIFException(
217                 "binding transport "
218                     + HTTP_TRANSPORT_URI
219                     + " requires soap:address for port "
220                     + port);
221         }
222
223         if (isTransportJMS() && jmsAddress == null) {
224             throw new WSIFException(
225                 "binding transport "
226                     + JMS_TRANSPORT_URI
227                     + " requires jms:address for port "
228                     + port);
229         }
230
231         if (soapAddress != null) {
232             String JavaDoc s = soapAddress.getLocationURI();
233             if (s == null || s.length() < 1) {
234                 throw new WSIFException(
235                     "soap:address with location URI is required for " + port);
236             }
237             try {
238                 this.endPointURL = new URL JavaDoc(s);
239             } catch (MalformedURLException JavaDoc e) {
240                 Trc.exception(e);
241                 throw new WSIFException(
242                     "exception setting SOAP address to "
243                         + s
244                         + ": "
245                         + e.getLocalizedMessage(),
246                     e);
247             }
248         } else {
249             this.jmsAddressPropVals = jmsAddress.getJMSPropertyValues();
250         }
251     }
252
253     /**
254      * Creates a WSIFOperation for the given operation name
255      * @return WSIFOperation the WSIFOperation
256      * @see WSIFOperation#createOperation(opName)
257      * @throws WSIFException if there is an exception creating the WSIFOperation
258      */

259     public WSIFOperation createOperation(String JavaDoc operationName)
260         throws WSIFException {
261         Trc.entry(this, operationName);
262         WSIFOperation wo = createOperation(operationName, null, null);
263         Trc.exit(wo);
264         return wo;
265     }
266
267     /**
268      * Creates a WSIFOperation for the given operation name
269      * @return WSIFOperation the WSIFOperation_ApacheAxis
270      * @see WSIFOperation#createOperation(opName, inName, outName)
271      * @throws WSIFException if there is an exception creating the WSIFOperation
272      */

273     public WSIFOperation createOperation(
274         String JavaDoc operationName,
275         String JavaDoc inputName,
276         String JavaDoc outputName)
277         throws WSIFException {
278         Trc.entry(this, operationName, inputName, outputName);
279
280         WSIFOperation_ApacheAxis op =
281             getDynamicWSIFOperation(operationName, inputName, outputName);
282         if (op == null) {
283             throw new WSIFException(
284                 "Could not create operation: "
285                     + operationName
286                     + ":"
287                     + inputName
288                     + ":"
289                     + outputName);
290         }
291         Trc.exit(op);
292         return op;
293     }
294
295     /**
296      * Closes the port.
297      * All methods are invalid after calling this method.
298      * @throws WSIFException if there is an exception closing the Transport
299      */

300     public void close() throws WSIFException {
301         Trc.entry(this);
302         if (transport != null && transport instanceof WSIFJmsTransport) {
303             ((WSIFJmsTransport) transport).close();
304         }
305         Trc.exit();
306     }
307
308     /**
309      * @deprecated replaced by getTransport
310      */

311     public Transport getAxisTransport() throws WSIFException {
312         Trc.entry(this);
313         Transport t = getTransport();
314         Trc.exit(t);
315         return t;
316     }
317
318     /**
319      * Gets the QName of the WSDL Binding
320      * @return QName the name of the Binding
321      */

322     public QName JavaDoc getBindingName() {
323         Trc.entry(this);
324         Binding binding = port.getBinding();
325         QName JavaDoc bindingQN = binding.getQName();
326         Trc.exit(bindingQN);
327         return bindingQN;
328     }
329
330     /**
331      * Returns the style attribute of this Binding.
332      * @return String the style attribute
333      */

334     public String JavaDoc getBindingStyle() {
335         Trc.entry(this);
336         Trc.exit(bindingStyle);
337         return bindingStyle;
338     }
339
340     /**
341      * Gets the AXIS Call object being used by this WSIFPort
342      * @return Call the AXIS Call object
343      * @throws WSIFException if there is an exception creating the AXIS Call
344      */

345     public Call getCall() throws WSIFException {
346         Trc.entry(this);
347         if (call == null) {
348             call = makeNewAXISCall();
349         }
350         Trc.exit(call);
351         return call;
352     }
353
354     /**
355      * Creates a new AXIS Call object
356      */

357     private Call makeNewAXISCall() throws WSIFException {
358         Call c = null;
359         java.net.URL JavaDoc url = getEndPoint();
360         try {
361             if (url != null) {
362                 c = new Call(url);
363                 Transport axistransport = getTransport();
364                 if (axistransport != null) {
365                     axistransport.setUrl(url.toString());
366                 }
367             } else {
368                 c = new Call(new org.apache.axis.client.Service());
369             }
370             c.setMaintainSession(true);
371         } catch (JAXRPCException JavaDoc e) {
372             Trc.exception(e);
373             throw new WSIFException(
374                 "exception creating call object: "
375                     + e.getLocalizedMessage(),
376                 e);
377         }
378         return c;
379     }
380
381     /**
382      * Returns the Definition object for the WSDL file
383      * @return Definition the WSDL4J Definition object
384      */

385     public Definition getDefinition() {
386         Trc.entry(this);
387         Trc.exit(definition);
388         return definition;
389     }
390
391     /**
392      * @deprecated WSIF clients should use the createOperation methods
393      */

394     public WSIFOperation_ApacheAxis getDynamicWSIFOperation(
395         String JavaDoc opName,
396         String JavaDoc inputName,
397         String JavaDoc outputName)
398         throws WSIFException {
399         Trc.entry(this, opName, inputName, outputName);
400
401         WSIFOperation_ApacheAxis cachedOp = null;
402         
403         if (cachedWSIFOperations == null) {
404            cachedWSIFOperations = new HashMap JavaDoc();
405         } else {
406            cachedOp = (WSIFOperation_ApacheAxis) cachedWSIFOperations.get(
407               getKey(opName, inputName, outputName));
408         }
409
410         WSIFOperation_ApacheAxis wsifOperation;
411         if (cachedOp == null) {
412             BindingOperation bop =
413                WSIFUtils.getBindingOperation(
414                   port.getBinding(), opName, inputName, outputName );
415             if (bop == null) {
416                 throw new WSIFException(
417                    "no operation found named " +
418                    opName + ", input:" + inputName + ", output:" + outputName );
419             }
420             cachedOp =
421                 new WSIFOperation_ApacheAxis(
422                     this,
423                     bop.getOperation(),
424                     wsifdynamictypemap);
425             cachedWSIFOperations.put(
426                 getKey(opName, inputName, outputName),
427                 cachedOp);
428             wsifOperation = cachedOp;
429         } else {
430             wsifOperation = cachedOp.copy();
431         }
432
433         Trc.exit(wsifOperation);
434         return wsifOperation;
435     }
436
437     /**
438      * Returns the URL of the location attribute of the soap:address
439      * @return URL the URL of the service location
440      */

441     public URL JavaDoc getEndPoint() {
442         Trc.entry(this);
443         Trc.exit(endPointURL);
444         return endPointURL;
445     }
446
447     //TODO these getExtElem methods should be moved to ProviderUtils?
448
/**
449      * Wrapper to enable WSIFOperation to use the same WSIFDefaultPort method
450      * @see WSIFDefaultPort#getExtElem
451      */

452     public Object JavaDoc getExtElem(Object JavaDoc ctx, Class JavaDoc extType, List JavaDoc extElems)
453         throws WSIFException {
454         Trc.entry(this, ctx, extType, extElems);
455         Object JavaDoc o = super.getExtElem(ctx, extType, extElems);
456         Trc.exit(o);
457         return o;
458     }
459
460     /**
461      * Wrapper to enable WSIFOperation to use the same WSIFDefaultPort method
462      * @see WSIFDefaultPort#getExtElems
463      */

464     public List JavaDoc getExtElems(Object JavaDoc ctx, Class JavaDoc extType, List JavaDoc extElems)
465         throws WSIFException {
466         Trc.entry(this, ctx, extType, extElems);
467         List JavaDoc l = super.getExtElems(ctx, extType, extElems);
468         Trc.exit(l);
469         return l;
470     }
471
472     /**
473      * Returns any JMS propertyValue elements in the jms:address element
474      * @return List a List of the jms:propertyValue elements
475      */

476     public List JavaDoc getJmsAddressPropVals() {
477         Trc.entry(this);
478         Trc.exit(jmsAddressPropVals);
479         return jmsAddressPropVals;
480     }
481
482     /**
483      * Returns the WSDL Port object this WSIFPort represents
484      * @return Port the WSDL4J Port object
485      */

486     public Port getPort() {
487         Trc.entry(this);
488         Trc.exit(port);
489         return port;
490     }
491
492     /**
493      * Gets the soap:operation WSDL element from a BindingOperation
494      * The WSDL binding operation element has the form:
495      * <binding .... >
496      * ...
497      * <operation .... >
498      * <soap:operation soapAction="uri"? style="rpc|document"?>?
499      * ...
500      * </operation>
501      * </binding>
502      * @return SOAPOperation the soap:operation element
503      * @throws WSIFException if there is no soap:operation element in the binding operation element.
504      */

505     public SOAPOperation getSOAPOperation(BindingOperation bindingOp)
506         throws WSIFException {
507         Trc.entry(this, bindingOp);
508         SOAPOperation soapOperation =
509             (SOAPOperation) getExtElem(bindingOp,
510                 javax.wsdl.extensions.soap.SOAPOperation.class,
511                 bindingOp.getExtensibilityElements());
512         if (soapOperation == null)
513             throw new WSIFException(
514                 "no soap:operation found in binding for: " + bindingOp);
515         Trc.exit(soapOperation);
516         return soapOperation;
517     }
518
519     /**
520      * Gets the AXIS Transport object being used by this WSIFPort
521      * @return Transport the AXIS Transport object
522      * @throws WSIFException if there is an exception creating the Transport
523      */

524     public Transport getTransport() throws WSIFException {
525         Trc.entry(this);
526         if (transport == null) {
527             String JavaDoc s = soapBinding.getTransportURI();
528             if (HTTP_TRANSPORT_URI.equals(s)) {
529                 transport = new HTTPTransport();
530             } else if (JMS_TRANSPORT_URI.equals(s)) {
531                 WSIFJMSDestination jmsDestination =
532                     new WSIFJMSDestination(
533                         WSIFJMSFinder.newFinder(jmsAddress, port.getName()),
534                         jmsAddress.getJmsProvDestName(),
535                         WSIFProperties.getSyncTimeout());
536                 transport = new WSIFJmsTransport(jmsDestination);
537             }
538         }
539         Trc.exit(transport);
540         return transport;
541     }
542
543     /**
544      * Tests if an HTTP transport is being used
545      * @return boolean true if an HTTP transport is being used,
546      * otherwise false
547      */

548     public boolean isTransportHTTP() {
549         Trc.entry(this);
550         String JavaDoc transportURI = soapBinding.getTransportURI();
551         boolean httpTransport = HTTP_TRANSPORT_URI.equals(transportURI);
552         Trc.exit(httpTransport);
553         return httpTransport;
554     }
555
556     /**
557      * Tests if a JMS transport is being used
558      * @return boolean true if a JMS transport is being used,
559      * otherwise false
560      */

561     public boolean isTransportJMS() {
562         Trc.entry(this);
563         String JavaDoc transportURI = soapBinding.getTransportURI();
564         boolean jmsTransport = JMS_TRANSPORT_URI.equals(transportURI);
565         Trc.exit(jmsTransport);
566         return jmsTransport;
567     }
568
569     /**
570      * @deprecated should anyone be calling this?
571      */

572     public void setDefinition(Definition definition1) {
573         Trc.entry(this, definition1);
574         definition = definition1;
575         Trc.exit();
576     }
577
578     /**
579      * @deprecated should anyone be calling this?
580      */

581     public void setDynamicWSIFOperation(
582         String JavaDoc s,
583         String JavaDoc s1,
584         String JavaDoc s2,
585         WSIFOperation_ApacheAxis wsifoperation_apacheaxis) {
586         Trc.entry(this, s, s1, s2, wsifoperation_apacheaxis);
587         if (cachedWSIFOperations == null) {
588            cachedWSIFOperations = new HashMap JavaDoc();
589         }
590         cachedWSIFOperations.put(getKey(s, s1, s2), wsifoperation_apacheaxis);
591         Trc.exit();
592     }
593
594     /**
595      * @deprecated should anyone be calling this?
596      */

597     public void setEndPoint(URL JavaDoc url1) {
598         Trc.entry(this, url1);
599         endPointURL = url1;
600         Trc.exit();
601     }
602
603     /**
604      * @deprecated should anyone be calling this?
605      */

606     public void setPort(Port port1) {
607         Trc.entry(this, port1);
608         port = port1;
609         Trc.exit();
610     }
611
612     /**
613      * Tests if this port supports asynchronous calls to operations.
614      * @return true if the port is using a JMS transport, otherwise false
615      */

616     public boolean supportsAsync() {
617         Trc.entry(this);
618         if (isTransportJMS()) {
619             Trc.exit(true);
620             return true;
621         } else {
622             Trc.exit(false);
623             return false;
624         }
625     }
626
627     public String JavaDoc deep() {
628         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
629         try {
630             buff.append(new String JavaDoc(super.toString()));
631             buff.append(":\n");
632             buff.append(" port: ").append(port);
633             buff.append(" definition: ").append(definition);
634             buff.append(" soapbinding: ").append(soapBinding);
635             buff.append(" bindingStyle: ").append(bindingStyle);
636             buff.append(" soapAddress: ").append(soapAddress);
637             buff.append(" jmsAddress: ").append(jmsAddress);
638             buff.append(" service url: ").append(endPointURL);
639             buff.append(" jmsAddressPropVals: ").append(jmsAddressPropVals);
640             buff.append(" dynamicTypeMap: ").append(wsifdynamictypemap);
641             buff.append(" transport: ").append(transport);
642             buff.append(" call: ").append(call);
643             buff.append("operationInstances: ").append(cachedWSIFOperations);
644         } catch (Exception JavaDoc e) {
645             Trc.exceptionInTrace(e);
646         }
647         return buff.toString();
648     }
649
650 }
Popular Tags