Coverage Report - org.mule.construct.builder.BridgeBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
BridgeBuilder
0%
0/48
0%
0/18
0
 
 1  
 /*
 2  
  * $Id: BridgeBuilder.java 19925 2010-10-15 00:45:14Z ddossot $
 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.construct.builder;
 12  
 
 13  
 import java.util.Arrays;
 14  
 
 15  
 import org.mule.MessageExchangePattern;
 16  
 import org.mule.api.MuleContext;
 17  
 import org.mule.api.MuleException;
 18  
 import org.mule.api.endpoint.EndpointBuilder;
 19  
 import org.mule.api.endpoint.InboundEndpoint;
 20  
 import org.mule.api.endpoint.OutboundEndpoint;
 21  
 import org.mule.api.processor.MessageProcessor;
 22  
 import org.mule.api.transaction.TransactionConfig;
 23  
 import org.mule.api.transaction.TransactionException;
 24  
 import org.mule.api.transaction.TransactionFactory;
 25  
 import org.mule.api.transformer.Transformer;
 26  
 import org.mule.config.i18n.MessageFactory;
 27  
 import org.mule.construct.Bridge;
 28  
 import org.mule.transaction.MuleTransactionConfig;
 29  
 import org.mule.transaction.XaTransactionFactory;
 30  
 import org.mule.util.ClassUtils;
 31  
 import org.mule.util.StringUtils;
 32  
 
 33  0
 public class BridgeBuilder extends
 34  
     AbstractFlowConstructWithSingleInboundAndOutboundEndpointBuilder<BridgeBuilder, Bridge>
 35  
 {
 36  0
     protected MessageExchangePattern exchangePattern = MessageExchangePattern.REQUEST_RESPONSE;
 37  0
     protected boolean transacted = false;
 38  
 
 39  
     @Override
 40  
     protected MessageExchangePattern getInboundMessageExchangePattern()
 41  
     {
 42  0
         return exchangePattern;
 43  
     }
 44  
 
 45  
     @Override
 46  
     protected MessageExchangePattern getOutboundMessageExchangePattern()
 47  
     {
 48  0
         return exchangePattern;
 49  
     }
 50  
 
 51  
     public BridgeBuilder exchangePattern(MessageExchangePattern exchangePattern)
 52  
     {
 53  0
         this.exchangePattern = exchangePattern;
 54  0
         return this;
 55  
     }
 56  
 
 57  
     public BridgeBuilder transacted(boolean transacted)
 58  
     {
 59  0
         this.transacted = transacted;
 60  0
         return this;
 61  
     }
 62  
 
 63  
     public BridgeBuilder inboundTransformers(Transformer... transformers)
 64  
     {
 65  0
         this.inboundTransformers = Arrays.asList((MessageProcessor[]) transformers);
 66  0
         return this;
 67  
     }
 68  
 
 69  
     public BridgeBuilder inboundResponseTransformers(Transformer... responseTransformers)
 70  
     {
 71  0
         this.inboundResponseTransformers = Arrays.asList((MessageProcessor[]) responseTransformers);
 72  0
         return this;
 73  
     }
 74  
 
 75  
     @Override
 76  
     protected void doConfigureInboundEndpointBuilder(MuleContext muleContext, EndpointBuilder endpointBuilder)
 77  
     {
 78  0
         if (transacted)
 79  
         {
 80  0
             MuleTransactionConfig transactionConfig = new MuleTransactionConfig();
 81  0
             transactionConfig.setMuleContext(muleContext);
 82  0
             transactionConfig.setAction(TransactionConfig.ACTION_BEGIN_OR_JOIN);
 83  0
             endpointBuilder.setTransactionConfig(transactionConfig);
 84  
         }
 85  0
     }
 86  
 
 87  
     @Override
 88  
     protected void doConfigureOutboundEndpointBuilder(MuleContext muleContext, EndpointBuilder endpointBuilder)
 89  
     {
 90  0
         if (transacted)
 91  
         {
 92  0
             MuleTransactionConfig transactionConfig = new MuleTransactionConfig();
 93  0
             transactionConfig.setMuleContext(muleContext);
 94  0
             transactionConfig.setAction(TransactionConfig.ACTION_ALWAYS_JOIN);
 95  0
             endpointBuilder.setTransactionConfig(transactionConfig);
 96  
         }
 97  0
     }
 98  
 
 99  
     @Override
 100  
     protected Bridge buildFlowConstruct(MuleContext muleContext) throws MuleException
 101  
     {
 102  0
         InboundEndpoint inboundEndpoint = getOrBuildInboundEndpoint(muleContext);
 103  0
         OutboundEndpoint outboundEndpoint = getOrBuildOutboundEndpoint(muleContext);
 104  
 
 105  0
         if (transacted)
 106  
         {
 107  0
             setTransactionFactoriesIfNeeded(inboundEndpoint, outboundEndpoint);
 108  
         }
 109  
 
 110  0
         return new Bridge(name, muleContext, inboundEndpoint, outboundEndpoint, exchangePattern, transacted);
 111  
     }
 112  
 
 113  
     private void setTransactionFactoriesIfNeeded(InboundEndpoint inboundEndpoint,
 114  
                                                  OutboundEndpoint outboundEndpoint) throws MuleException
 115  
     {
 116  0
         String inboundProtocol = inboundEndpoint.getConnector().getProtocol();
 117  0
         String outboundProtocol = outboundEndpoint.getConnector().getProtocol();
 118  
 
 119  0
         boolean needXA = !inboundProtocol.equals(outboundProtocol);
 120  
 
 121  0
         TransactionConfig inboundTransactionConfig = inboundEndpoint.getTransactionConfig();
 122  
 
 123  0
         if (inboundTransactionConfig.getFactory() == null)
 124  
         {
 125  0
             TransactionFactory transactionFactory = needXA
 126  
                                                           ? new XaTransactionFactory()
 127  
                                                           : getTransactionFactory(inboundProtocol);
 128  
 
 129  0
             inboundTransactionConfig.setFactory(transactionFactory);
 130  
         }
 131  
 
 132  0
         TransactionConfig outboundTransactionConfig = outboundEndpoint.getTransactionConfig();
 133  
 
 134  0
         if (outboundTransactionConfig.getFactory() == null)
 135  
         {
 136  0
             TransactionFactory transactionFactory = needXA
 137  
                                                           ? new XaTransactionFactory()
 138  
                                                           : getTransactionFactory(outboundProtocol);
 139  0
             outboundTransactionConfig.setFactory(transactionFactory);
 140  
         }
 141  0
     }
 142  
 
 143  
     /**
 144  
      * Very simplistic attempt to locate a protocol specific transaction factory.
 145  
      */
 146  
     private TransactionFactory getTransactionFactory(String protocol) throws MuleException
 147  
     {
 148  0
         String protocolTransactionFactoryClassName = "org.mule.transport." + StringUtils.lowerCase(protocol)
 149  
                                                      + "." + StringUtils.capitalize(protocol)
 150  
                                                      + "TransactionFactory";
 151  
 
 152  0
         if (!ClassUtils.isClassOnPath(protocolTransactionFactoryClassName, getClass()))
 153  
         {
 154  0
             throw new TransactionException(
 155  
                 MessageFactory.createStaticMessage("Failed to locate a transaction factory for protocol: "
 156  
                                                    + protocol));
 157  
         }
 158  
 
 159  
         try
 160  
         {
 161  0
             return (TransactionFactory) ClassUtils.instanciateClass(protocolTransactionFactoryClassName);
 162  
         }
 163  0
         catch (Exception e)
 164  
         {
 165  0
             throw new TransactionException(
 166  
                 MessageFactory.createStaticMessage("Failed to instantiate a transaction factory for protocol: "
 167  
                                                    + protocol), e);
 168  
         }
 169  
     }
 170  
 }