View Javadoc

1   /*
2    * $Id: AbstractFlowConstructBuilder.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.builder;
12  
13  import java.util.Collections;
14  import java.util.List;
15  
16  import org.mule.api.MuleContext;
17  import org.mule.api.MuleException;
18  import org.mule.api.exception.MessagingExceptionHandler;
19  import org.mule.api.processor.MessageProcessor;
20  import org.mule.api.source.MessageSource;
21  import org.mule.construct.AbstractFlowConstruct;
22  
23  @SuppressWarnings("unchecked")
24  public abstract class AbstractFlowConstructBuilder<T extends AbstractFlowConstructBuilder<?, ?>, F extends AbstractFlowConstruct>
25  {
26      protected String name;
27      protected MessageSource messageSource;
28      protected MessagingExceptionHandler exceptionListener;
29  
30      // setters should be exposed only for builders where it makes sense
31      protected List<MessageProcessor> transformers = Collections.emptyList();
32      protected List<MessageProcessor> responseTransformers = Collections.emptyList();
33  
34      public T name(String name)
35      {
36          this.name = name;
37          return (T) this;
38      }
39  
40      public T messageSource(MessageSource messageSource)
41      {
42          this.messageSource = messageSource;
43          return (T) this;
44      }
45  
46      public T exceptionStrategy(MessagingExceptionHandler exceptionListener)
47      {
48          this.exceptionListener = exceptionListener;
49          return (T) this;
50      }
51  
52      public F build(MuleContext muleContext) throws MuleException
53      {
54          final F flowConstruct = buildFlowConstruct(muleContext);
55          addExceptionListener(flowConstruct);
56          return flowConstruct;
57      }
58  
59      public F buildAndRegister(MuleContext muleContext) throws MuleException
60      {
61          final F flowConstruct = build(muleContext);
62          muleContext.getRegistry().registerObject(flowConstruct.getName(), flowConstruct);
63          return flowConstruct;
64      }
65  
66      protected abstract F buildFlowConstruct(MuleContext muleContext) throws MuleException;
67  
68      protected void addExceptionListener(AbstractFlowConstruct flowConstruct)
69      {
70          if (exceptionListener != null)
71          {
72              flowConstruct.setExceptionListener(exceptionListener);
73          }
74      }
75  }