View Javadoc

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