View Javadoc

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