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