View Javadoc

1   /*
2    * $Id$
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;
12  
13  import org.apache.commons.lang.Validate;
14  import org.mule.MessageExchangePattern;
15  import org.mule.api.MuleContext;
16  import org.mule.api.construct.FlowConstructInvalidException;
17  import org.mule.api.endpoint.InboundEndpoint;
18  import org.mule.api.endpoint.OutboundEndpoint;
19  import org.mule.api.source.MessageSource;
20  import org.mule.config.i18n.MessageFactory;
21  import org.mule.construct.processor.FlowConstructStatisticsMessageObserver;
22  import org.mule.interceptor.LoggingInterceptor;
23  import org.mule.processor.builder.InterceptingChainMessageProcessorBuilder;
24  
25  /**
26   * A simple bridge between a single inbound endpoint and a single outbound endpoint.
27   * It enforces a consistent exchange pattern across its endpoints. If declared
28   * transactional, it ensures that the correct endpoint configuration is in place.
29   */
30  public class Bridge extends AbstractFlowConstruct
31  {
32      private final OutboundEndpoint outboundEndpoint;
33      private final MessageExchangePattern exchangePattern;
34      private final boolean transacted;
35  
36      public Bridge(String name,
37                    MuleContext muleContext,
38                    MessageSource messageSource,
39                    OutboundEndpoint outboundEndpoint,
40                    MessageExchangePattern exchangePattern,
41                    boolean transacted)
42      {
43          super(name, muleContext);
44  
45          Validate.notNull(messageSource, "messageSource can't be null");
46          Validate.notNull(outboundEndpoint, "outboundEndpoint can't be null");
47          Validate.notNull(exchangePattern, "exchangePattern can't be null");
48  
49          this.messageSource = messageSource;
50          this.outboundEndpoint = outboundEndpoint;
51          this.exchangePattern = exchangePattern;
52          this.transacted = transacted;
53      }
54  
55      @Override
56      protected void configureMessageProcessors(InterceptingChainMessageProcessorBuilder builder)
57      {
58          builder.chain(new LoggingInterceptor());
59          builder.chain(new FlowConstructStatisticsMessageObserver());
60          builder.chain(outboundEndpoint);
61      }
62  
63      @Override
64      protected void validateConstruct() throws FlowConstructInvalidException
65      {
66          super.validateConstruct();
67  
68          if (messageSource instanceof InboundEndpoint)
69          {
70              validateInboundEndpoint((InboundEndpoint) messageSource);
71          }
72  
73          validateOutboundEndpoint();
74      }
75  
76      private void validateOutboundEndpoint() throws FlowConstructInvalidException
77      {
78          if (outboundEndpoint.getExchangePattern() != exchangePattern)
79          {
80              throw new FlowConstructInvalidException(
81                  MessageFactory.createStaticMessage("Inconsistent bridge outbound endpoint exchange pattern, expected "
82                                                     + exchangePattern
83                                                     + " but was "
84                                                     + outboundEndpoint.getExchangePattern()), this);
85          }
86  
87          if (transacted
88              && ((outboundEndpoint.getTransactionConfig() == null) || (!outboundEndpoint.getTransactionConfig()
89                  .isConfigured())))
90          {
91              throw new FlowConstructInvalidException(
92                  MessageFactory.createStaticMessage("A transacted bridge requires a transacted outbound endpoint"),
93                  this);
94          }
95  
96          if ((!transacted) && (outboundEndpoint.getTransactionConfig() != null)
97              && (outboundEndpoint.getTransactionConfig().isConfigured()))
98          {
99              throw new FlowConstructInvalidException(
100                 MessageFactory.createStaticMessage("A non-transacted bridge requires a non-transacted outbound endpoint"),
101                 this);
102         }
103     }
104 
105     private void validateInboundEndpoint(InboundEndpoint inboundEndpoint)
106         throws FlowConstructInvalidException
107     {
108         if (inboundEndpoint.getExchangePattern() != exchangePattern)
109         {
110             throw new FlowConstructInvalidException(
111                 MessageFactory.createStaticMessage("Inconsistent bridge inbound endpoint exchange pattern, expected "
112                                                    + exchangePattern
113                                                    + " but was "
114                                                    + inboundEndpoint.getExchangePattern()), this);
115         }
116 
117         if (transacted
118             && ((inboundEndpoint.getTransactionConfig() == null) || (!inboundEndpoint.getTransactionConfig()
119                 .isConfigured())))
120         {
121             throw new FlowConstructInvalidException(
122                 MessageFactory.createStaticMessage("A transacted bridge requires a transacted inbound endpoint"),
123                 this);
124         }
125 
126         if ((!transacted) && (inboundEndpoint.getTransactionConfig() != null)
127             && (inboundEndpoint.getTransactionConfig().isConfigured()))
128         {
129             throw new FlowConstructInvalidException(
130                 MessageFactory.createStaticMessage("A non-transacted bridge requires a non-transacted inbound endpoint"),
131                 this);
132         }
133     }
134 }