View Javadoc

1   /*
2    * $Id: XFireMessageReceiver.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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.providers.soap.xfire;
12  
13  import org.mule.providers.AbstractMessageReceiver;
14  import org.mule.providers.soap.SoapConstants;
15  import org.mule.umo.UMOComponent;
16  import org.mule.umo.UMOException;
17  import org.mule.umo.endpoint.UMOEndpoint;
18  import org.mule.umo.lifecycle.InitialisationException;
19  import org.mule.umo.provider.UMOConnector;
20  import org.mule.util.ClassUtils;
21  import org.mule.util.MapUtils;
22  import org.mule.util.StringUtils;
23  
24  import java.net.MalformedURLException;
25  import java.net.URL;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Map;
29  
30  import javax.xml.namespace.QName;
31  
32  import org.codehaus.xfire.annotations.WebAnnotations;
33  import org.codehaus.xfire.annotations.WebServiceAnnotation;
34  import org.codehaus.xfire.handler.Handler;
35  import org.codehaus.xfire.service.Service;
36  
37  /**
38   * Used to register an Xfire endpoint registered with Mule and associated with a
39   * component This receiver is responsible or registering the transport endpoint i.e.
40   * http:// as well as managing the association of this transport endpoint with the
41   * Xfire service.
42   */
43  public class XFireMessageReceiver extends AbstractMessageReceiver
44  {
45  
46      protected XFireConnector connector;
47      protected Service service;
48  
49      protected List serviceInterfaces;
50  
51      public XFireMessageReceiver(UMOConnector umoConnector,
52                                  UMOComponent component,
53                                  UMOEndpoint umoEndpoint) throws InitialisationException
54      {
55          super(umoConnector, component, umoEndpoint);
56          connector = (XFireConnector)umoConnector;
57          init();
58      }
59  
60      protected void init() throws InitialisationException
61      {
62          try
63          {
64              Map props = new HashMap(component.getDescriptor().getProperties());
65              props.putAll(endpoint.getProperties());
66  
67              // check if there is the namespace property on the component
68              String namespace = (String)component.getDescriptor().getProperties().get(
69                  SoapConstants.SOAP_NAMESPACE_PROPERTY);
70  
71              // check for namespace set as annotation
72              if (connector.isEnableJSR181Annotations())
73              {
74                  WebAnnotations wa = (WebAnnotations)ClassUtils.instanciateClass(
75                      XFireConnector.CLASSNAME_ANNOTATIONS, null, this.getClass());
76                  WebServiceAnnotation webServiceAnnotation = wa.getWebServiceAnnotation(component
77                      .getDescriptor().getImplementationClass());
78                  namespace = webServiceAnnotation.getTargetNamespace();
79              }
80  
81              if ((namespace == null) || (namespace.equalsIgnoreCase("")))
82              {
83                  namespace = MapUtils.getString(props, "namespace",
84                      XFireConnector.DEFAULT_MULE_NAMESPACE_URI);
85              }
86              
87              //Convert createDefaultBindings string to boolean before rewriting as xfire property
88              if(props.get("createDefaultBindings")!=null){
89           	   props.put("createDefaultBindings", Boolean.valueOf((String)props.get("createDefaultBindings")));  
90              }
91  
92              if (props.size() == 0)
93              {
94                  // Xfire checks that properties are null rather than empty
95                  props = null;
96              }
97              else
98              {
99                  rewriteProperty(props, "portType");
100                 rewriteProperty(props, "style");
101                 rewriteProperty(props, "use");
102                 rewriteProperty(props, "createDefaultBindings");
103                 rewriteProperty(props, "soap12Transports");
104                 rewriteProperty(props, "soap11Transports");
105                 rewriteProperty(props, "scope");
106                 rewriteProperty(props, "schemas");
107             }
108 
109             serviceInterfaces = (List)component.getDescriptor().getProperties().get(
110                 "serviceInterfaces");
111             Class exposedInterface;
112 
113             if (serviceInterfaces == null)
114                 exposedInterface = component.getDescriptor().getImplementationClass();
115 
116             else
117             {
118                 String className = (String)serviceInterfaces.get(0);
119                 exposedInterface = ClassUtils.loadClass(className, this.getClass());
120                 logger.info(className + " class was used to expose your service");
121 
122                 if (serviceInterfaces.size() > 1)
123                 {
124                     logger.info("Only the first class was used to expose your method");
125                 }
126             }
127 
128             String wsdlUrl = (String)component.getDescriptor().getProperties().get(
129                 SoapConstants.WSDL_URL_PROPERTY);
130 
131             if (StringUtils.isBlank(wsdlUrl))
132             {
133                 service = connector.getServiceFactory().create(exposedInterface,
134                     component.getDescriptor().getName(), namespace, props);
135             }
136             else
137             {
138                 service = connector.getServiceFactory().create(exposedInterface,
139                     new QName(namespace, component.getDescriptor().getName()), new URL(wsdlUrl),
140                     props);
141             }
142 
143             List inList = connector.getServerInHandlers();
144             if (inList != null)
145             {
146                 for (int i = 0; i < inList.size(); i++)
147                 {
148                     Class clazz = ClassUtils.loadClass(inList.get(i).toString(), this.getClass());
149                     Handler handler = (Handler)clazz.getConstructor(null).newInstance(null);
150                     service.addInHandler(handler);
151                 }
152             }
153 
154             boolean sync = endpoint.isSynchronous();
155             // default to synchronous if using http
156             if (endpoint.getEndpointURI().getScheme().startsWith("http")
157                 || endpoint.getEndpointURI().getScheme().startsWith("servlet"))
158             {
159                 sync = true;
160             }
161             service.setInvoker(new MuleInvoker(this, sync));
162 
163         }
164         catch (UMOException e)
165         {
166             throw new InitialisationException(e, this);
167         }
168         catch (ClassNotFoundException e)
169         {
170             // will be thrown in the case that the ClassUtils.loadClass() does
171             // not find the class to load
172             throw new InitialisationException(e, this);
173         }
174         catch (MalformedURLException e)
175         {
176             throw new InitialisationException(e, this);
177         }
178         catch (Exception e)
179         {
180             throw new InitialisationException(e, this);
181         }
182     }
183 
184     protected void doDispose()
185     {
186         // template method
187     }
188 
189     public void doConnect() throws Exception
190     {
191         // Tell the Xfire registry about our new service.
192         connector.getXfire().getServiceRegistry().register(service);
193         connector.registerReceiverWithMuleService(this, endpoint.getEndpointURI());
194     }
195 
196     public void doDisconnect() throws Exception
197     {
198         connector.getXfire().getServiceRegistry().unregister(service);
199     }
200 
201     public void doStart() throws UMOException
202     {
203         // nothing to do
204     }
205 
206     public void doStop() throws UMOException
207     {
208         // nothing to do
209     }
210 
211     protected void rewriteProperty(Map props, String name)
212     {
213         if (props.containsKey(name))
214         {
215             Object temp = props.remove(name);
216             props.put("objectServiceFactory." + name, temp);
217         }
218     }
219 
220 }