View Javadoc

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