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