View Javadoc

1   /*
2    * $Id: AbstractInboundMessageProcessorBuilder.java 19998 2010-10-24 14:39:16Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.module.cxf.builder;
12  
13  import org.mule.api.DefaultMuleException;
14  import org.mule.api.MuleContext;
15  import org.mule.api.MuleException;
16  import org.mule.api.context.MuleContextAware;
17  import org.mule.api.lifecycle.Callable;
18  import org.mule.api.lifecycle.Disposable;
19  import org.mule.api.lifecycle.Initialisable;
20  import org.mule.api.processor.MessageProcessorBuilder;
21  import org.mule.api.service.ServiceAware;
22  import org.mule.module.cxf.CxfConfiguration;
23  import org.mule.module.cxf.CxfInboundMessageProcessor;
24  import org.mule.module.cxf.MuleInvoker;
25  import org.mule.module.cxf.support.CxfUtils;
26  import org.mule.module.cxf.support.MuleHeadersInInterceptor;
27  import org.mule.module.cxf.support.MuleHeadersOutInterceptor;
28  import org.mule.module.cxf.support.MuleProtocolHeadersOutInterceptor;
29  import org.mule.module.cxf.support.MuleServiceConfiguration;
30  import org.mule.util.ClassUtils;
31  
32  import java.util.HashMap;
33  import java.util.List;
34  import java.util.Map;
35  import java.util.concurrent.CopyOnWriteArrayList;
36  
37  import org.apache.cxf.Bus;
38  import org.apache.cxf.configuration.Configurer;
39  import org.apache.cxf.endpoint.Server;
40  import org.apache.cxf.feature.AbstractFeature;
41  import org.apache.cxf.frontend.ServerFactoryBean;
42  import org.apache.cxf.interceptor.AttachmentOutInterceptor;
43  import org.apache.cxf.interceptor.Interceptor;
44  import org.apache.cxf.interceptor.OneWayProcessorInterceptor;
45  import org.apache.cxf.service.factory.AbstractServiceConfiguration;
46  import org.apache.cxf.service.factory.ReflectionServiceFactoryBean;
47  import org.apache.cxf.service.invoker.Invoker;
48  
49  /**
50   * An abstract builder for CXF services. It handles all common operations such
51   * as interceptor configuration, mule header enabling, etc. Subclasses can extend
52   * this and control how the Server is created and how the {@link CxfInboundMessageProcessor}
53   * is configured.
54   */
55  public abstract class AbstractInboundMessageProcessorBuilder implements MuleContextAware, MessageProcessorBuilder
56  {
57      private CxfConfiguration configuration;
58      private Server server;
59      private boolean enableMuleSoapHeaders = true;
60      private String wsdlLocation;
61      private String bindingId;
62      private String mtomEnabled;
63      private String service;
64      private String namespace;
65      private List<AbstractFeature> features;
66      private List<Interceptor> inInterceptors = new CopyOnWriteArrayList<Interceptor>();
67      private List<Interceptor> inFaultInterceptors = new CopyOnWriteArrayList<Interceptor>();
68      private List<Interceptor> outInterceptors = new CopyOnWriteArrayList<Interceptor>();
69      private List<Interceptor> outFaultInterceptors = new CopyOnWriteArrayList<Interceptor>();
70      protected MuleContext muleContext;
71      private String port;
72      private Map<String,Object> properties;
73      
74      public CxfInboundMessageProcessor build() throws MuleException
75      {
76          if (muleContext == null)
77          {
78              throw new IllegalStateException("MuleContext must be supplied.");
79          }
80          
81          if (configuration == null)
82          {
83              configuration = CxfConfiguration.getConfiguration(muleContext);
84          }
85          
86          if (configuration == null)
87          {
88              throw new IllegalStateException("A CxfConfiguration object must be supplied.");
89          }
90  
91          ServerFactoryBean sfb;
92          try
93          {
94              sfb = createServerFactory();
95          }
96          catch (Exception e)
97          {
98              throw new DefaultMuleException(e);
99          }
100 
101         // The binding - i.e. SOAP, XML, HTTP Binding, etc
102         if (bindingId != null)
103         {
104             sfb.setBindingId(bindingId);
105         }
106         
107         if (features != null)
108         {
109             sfb.getFeatures().addAll(features);
110         }
111         
112         if (mtomEnabled != null)
113         {
114             Map<String, Object> properties = sfb.getProperties();
115             if (properties == null)
116             {
117                 properties = new HashMap<String, Object>();
118                 sfb.setProperties(properties);
119             }
120             properties.put("mtom-enabled", mtomEnabled);
121             properties.put(AttachmentOutInterceptor.WRITE_ATTACHMENTS, true);
122         }
123         
124         if (inInterceptors != null)
125         {
126             sfb.getInInterceptors().addAll(inInterceptors);
127         }
128         
129         if (inFaultInterceptors != null)
130         {
131             sfb.getInFaultInterceptors().addAll(inFaultInterceptors);
132         }
133         
134         if (outInterceptors != null)
135         {
136             sfb.getOutInterceptors().addAll(outInterceptors);
137         }
138         
139         if (outFaultInterceptors != null)
140         {
141             sfb.getOutFaultInterceptors().addAll(outFaultInterceptors);
142         }
143         
144         if (enableMuleSoapHeaders)
145         {
146             sfb.getInInterceptors().add(new MuleHeadersInInterceptor());
147             sfb.getInFaultInterceptors().add(new MuleHeadersInInterceptor());
148             sfb.getOutInterceptors().add(new MuleHeadersOutInterceptor());
149             sfb.getOutFaultInterceptors().add(new MuleHeadersOutInterceptor());
150         }
151         sfb.getOutInterceptors().add(new MuleProtocolHeadersOutInterceptor());
152         sfb.getOutFaultInterceptors().add(new MuleProtocolHeadersOutInterceptor());
153         
154         sfb.setAddress(getAddress()); // dummy URL for CXF
155 
156         if (wsdlLocation != null)
157         {
158             sfb.setWsdlURL(wsdlLocation);
159         }
160 
161         ReflectionServiceFactoryBean svcFac = sfb.getServiceFactory();
162         initServiceFactory(svcFac);
163 
164         CxfInboundMessageProcessor processor = new CxfInboundMessageProcessor();
165         configureMessageProcessor(sfb, processor);
166         sfb.setStart(false);
167 
168         Bus bus = configuration.getCxfBus();
169         sfb.setBus(bus);
170         svcFac.setBus(bus);
171         
172         Configurer configurer = bus.getExtension(Configurer.class);
173         if (null != configurer)
174         {
175             configurer.configureBean(svcFac.getEndpointName().toString(), sfb);
176         }
177         
178         sfb.setProperties(properties);
179         sfb.setInvoker(createInvoker(processor));
180         
181         server = sfb.create();
182         
183         CxfUtils.removeInterceptor(server.getEndpoint().getService().getInInterceptors(), OneWayProcessorInterceptor.class.getName());
184         configureServer(server);
185 
186         processor.setBus(sfb.getBus());
187         processor.setServer(server);
188         processor.setProxy(isProxy());
189         return processor;
190     }
191     
192     protected Invoker createInvoker(CxfInboundMessageProcessor processor)
193     {
194         return new MuleInvoker(processor, getServiceClass());
195     }
196 
197     protected void configureServer(Server server2)
198     {
199     }
200 
201     protected abstract Class<?> getServiceClass();
202 
203     protected void configureMessageProcessor(ServerFactoryBean sfb, CxfInboundMessageProcessor processor)
204     {
205     }
206 
207     protected abstract ServerFactoryBean createServerFactory() throws Exception;
208 
209     protected String getAddress()
210     {
211         return "http://internalMuleCxfRegistry/" + hashCode();
212     }
213 
214     /**
215      * This method configures the {@link ReflectionServiceFactoryBean}.
216      */
217     private void initServiceFactory(ReflectionServiceFactoryBean svcFac)
218     {
219         addIgnoredMethods(svcFac, Callable.class.getName());
220         addIgnoredMethods(svcFac, Initialisable.class.getName());
221         addIgnoredMethods(svcFac, Disposable.class.getName());
222         addIgnoredMethods(svcFac, ServiceAware.class.getName());
223 
224         svcFac.getServiceConfigurations().add(0, new MuleServiceConfiguration(this));
225 
226         svcFac.setServiceClass(getServiceClass());
227         for (AbstractServiceConfiguration c : svcFac.getServiceConfigurations())
228         {
229             c.setServiceFactory(svcFac);
230         }
231     }
232 
233     public void addIgnoredMethods(ReflectionServiceFactoryBean svcFac, String className)
234     {
235         try
236         {
237             Class<?> c = ClassUtils.loadClass(className, getClass());
238             for (int i = 0; i < c.getMethods().length; i++)
239             {
240                 svcFac.getIgnoredMethods().add(c.getMethods()[i]);
241             }
242         }
243         catch (ClassNotFoundException e)
244         {
245             // can be ignored.
246         }
247     }
248 
249 
250     public Server getServer()
251     {
252         return server;
253     }
254 
255     public abstract boolean isProxy();
256 
257     public CxfConfiguration getConfiguration()
258     {
259         return configuration;
260     }
261 
262     public void setConfiguration(CxfConfiguration configuration)
263     {
264         this.configuration = configuration;
265     }
266 
267     public boolean isEnableMuleSoapHeaders()
268     {
269         return enableMuleSoapHeaders;
270     }
271 
272     public void setEnableMuleSoapHeaders(boolean enableMuleSoapHeaders)
273     {
274         this.enableMuleSoapHeaders = enableMuleSoapHeaders;
275     }
276 
277     public String getWsdlLocation()
278     {
279         return wsdlLocation;
280     }
281 
282     public void setWsdlLocation(String wsdlUrl)
283     {
284         this.wsdlLocation = wsdlUrl;
285     }
286 
287     public String getBindingId()
288     {
289         return bindingId;
290     }
291 
292     public void setBindingId(String bindingId)
293     {
294         this.bindingId = bindingId;
295     }
296     public String getMtomEnabled()
297     {
298         return mtomEnabled;
299     }
300 
301     public void setMtomEnabled(String mtomEnabled)
302     {
303         this.mtomEnabled = mtomEnabled;
304     }
305 
306     public String getService()
307     {
308         return service;
309     }
310 
311     public void setService(String name)
312     {
313         this.service = name;
314     }
315 
316     public String getNamespace()
317     {
318         return namespace;
319     }
320 
321     public void setNamespace(String namespace)
322     {
323         this.namespace = namespace;
324     }
325 
326     public List<AbstractFeature> getFeatures()
327     {
328         return features;
329     }
330 
331     public void setFeatures(List<AbstractFeature> features)
332     {
333         this.features = features;
334     }
335 
336     public List<Interceptor> getInInterceptors()
337     {
338         return inInterceptors;
339     }
340 
341     public void setInInterceptors(List<Interceptor> inInterceptors)
342     {
343         this.inInterceptors = inInterceptors;
344     }
345 
346     public List<Interceptor> getInFaultInterceptors()
347     {
348         return inFaultInterceptors;
349     }
350 
351     public void setInFaultInterceptors(List<Interceptor> inFaultInterceptors)
352     {
353         this.inFaultInterceptors = inFaultInterceptors;
354     }
355 
356     public List<Interceptor> getOutInterceptors()
357     {
358         return outInterceptors;
359     }
360 
361     public void setOutInterceptors(List<Interceptor> outInterceptors)
362     {
363         this.outInterceptors = outInterceptors;
364     }
365 
366     public List<Interceptor> getOutFaultInterceptors()
367     {
368         return outFaultInterceptors;
369     }
370 
371     public void setOutFaultInterceptors(List<Interceptor> outFaultInterceptors)
372     {
373         this.outFaultInterceptors = outFaultInterceptors;
374     }
375     
376     public void setMuleContext(MuleContext muleContext)
377     {
378         this.muleContext = muleContext;
379     }
380 
381     public String getPort()
382     {
383         return port;
384     }
385 
386     public void setPort(String endpoint)
387     {
388         this.port = endpoint;
389     }
390 
391     public Map<String, Object> getProperties()
392     {
393         return properties;
394     }
395 
396     public void setProperties(Map<String, Object> properties)
397     {
398         this.properties = properties;
399     }
400 }