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.builder;
12  
13  import java.util.List;
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.processor.MessageProcessor;
21  import org.mule.construct.AbstractFlowConstruct;
22  
23  @SuppressWarnings("unchecked")
24  public abstract class AbstractFlowConstructWithSingleInboundEndpointBuilder<T extends AbstractFlowConstructBuilder, F extends AbstractFlowConstruct>
25      extends AbstractFlowConstructBuilder<T, F>
26  {
27      private InboundEndpoint inboundEndpoint;
28      private EndpointBuilder inboundEndpointBuilder;
29      private String inboundAddress;
30  
31      // setters should be exposed only for builders where it makes sense
32      protected List<MessageProcessor> inboundTransformers;
33      protected List<MessageProcessor> inboundResponseTransformers;
34  
35      public T inboundEndpoint(InboundEndpoint inboundEndpoint)
36      {
37          this.inboundEndpoint = inboundEndpoint;
38          return (T) this;
39      }
40  
41      public T inboundEndpoint(EndpointBuilder inboundEndpointBuilder)
42      {
43          this.inboundEndpointBuilder = inboundEndpointBuilder;
44          return (T) this;
45      }
46  
47      public T inboundAddress(String inboundAddress)
48      {
49          this.inboundAddress = inboundAddress;
50          return (T) this;
51      }
52  
53      protected InboundEndpoint getOrBuildInboundEndpoint(MuleContext muleContext) throws MuleException
54      {
55          if (inboundEndpoint != null)
56          {
57              return inboundEndpoint;
58          }
59  
60          if (inboundEndpointBuilder == null)
61          {
62              inboundEndpointBuilder = muleContext.getRegistry().lookupEndpointFactory().getEndpointBuilder(
63                  inboundAddress);
64          }
65  
66          inboundEndpointBuilder.setExchangePattern(getInboundMessageExchangePattern());
67  
68          if (inboundTransformers != null)
69          {
70              inboundEndpointBuilder.setMessageProcessors(inboundTransformers);
71          }
72  
73          if (inboundResponseTransformers != null)
74          {
75              inboundEndpointBuilder.setResponseMessageProcessors(inboundResponseTransformers);
76          }
77  
78          doConfigureInboundEndpointBuilder(muleContext, inboundEndpointBuilder);
79  
80          return inboundEndpointBuilder.buildInboundEndpoint();
81      }
82  
83      protected abstract MessageExchangePattern getInboundMessageExchangePattern();
84  
85      protected void doConfigureInboundEndpointBuilder(MuleContext muleContext, EndpointBuilder endpointBuilder)
86      {
87          // template method
88      }
89  }