View Javadoc

1   /*
2    * $Id: CxfWsdlMessageDispatcher.java 12200 2008-06-28 21:56:58Z dandiep $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.cxf.wsdl;
12  
13  import org.mule.api.endpoint.OutboundEndpoint;
14  import org.mule.transport.cxf.ClientWrapper;
15  import org.mule.transport.cxf.CxfMessageDispatcher;
16  import org.mule.util.StringUtils;
17  
18  import java.io.IOException;
19  
20  import javax.xml.namespace.QName;
21  
22  import org.apache.cxf.Bus;
23  import org.apache.cxf.endpoint.Client;
24  import org.apache.cxf.endpoint.dynamic.DynamicClientFactory;
25  
26  /**
27   * TODO document
28   */
29  public class CxfWsdlMessageDispatcher extends CxfMessageDispatcher
30  {
31      private final static Object CLIENT_CREATION_LOCK = new Object();
32      
33      public CxfWsdlMessageDispatcher(OutboundEndpoint endpoint)
34      {
35          super(endpoint);
36      }
37  
38      // @Override
39      protected void doConnect() throws Exception
40      {
41          try
42          {
43              wrapper = new ClientWrapper() {
44  
45                  @Override
46                  public void initialize() throws Exception, IOException
47                  {
48                      String wsdlUrl = endpoint.getEndpointURI().getAddress();
49                      String serviceName = null;
50                      String portName = null;
51  
52                      // If the property specified an alternative WSDL url, use it
53                      if (endpoint.getProperty("wsdlLocation") != null && StringUtils.isNotBlank(endpoint.getProperty("wsdlLocation").toString()))
54                      {
55                          wsdlUrl = (String) endpoint.getProperty("wsdlLocation");
56                      }
57                      
58                      // If the property specified an alternative service, use it
59                      if (endpoint.getProperty("service") != null && StringUtils.isNotBlank(endpoint.getProperty("service").toString()))
60                      {
61                          serviceName = (String) endpoint.getProperty("service");
62                      }
63                      
64                      // If the property specified an alternative port, use it
65                      if (endpoint.getProperty("port") != null && StringUtils.isNotBlank(endpoint.getProperty("port").toString()))
66                      {
67                          portName = (String) endpoint.getProperty("port");
68                      }
69                      
70                      try
71                      {
72                          this.client = createClient(bus, wsdlUrl, serviceName, portName);
73      
74                          addMuleInterceptors();
75                      }
76                      catch (Exception ex)
77                      {
78                          disconnect();
79                          throw ex;
80                      }
81                  }
82              };
83              wrapper.setBus(connector.getCxfBus());
84              wrapper.setEndpoint(endpoint);
85              wrapper.initialize();
86          }
87          catch (Exception ex)
88          {
89              disconnect();
90              throw ex;
91          }
92      }
93  
94      protected Client createClient(Bus bus, String wsdlUrl, String serviceName, String portName) throws Exception
95      {
96          synchronized (CLIENT_CREATION_LOCK)
97          {
98              DynamicClientFactory cf = DynamicClientFactory.newInstance(bus);
99              return cf.createClient(wsdlUrl, 
100                (serviceName == null ? null : QName.valueOf(serviceName)), 
101                (portName == null ? null : QName.valueOf(portName)));
102         }
103     }
104 }