View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.construct.builder;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.MuleException;
11  import org.mule.api.exception.MessagingExceptionHandler;
12  import org.mule.api.source.MessageSource;
13  import org.mule.construct.AbstractFlowConstruct;
14  
15  @SuppressWarnings("unchecked")
16  public abstract class AbstractFlowConstructBuilder<T extends AbstractFlowConstructBuilder, F extends AbstractFlowConstruct>
17  {
18      protected String name;
19      protected String initialState;
20      protected MessageSource messageSource;
21      protected MessagingExceptionHandler exceptionListener;
22  
23      public T name(String name)
24      {
25          this.name = name;
26          return (T) this;
27      }
28  
29      public T messageSource(MessageSource messageSource)
30      {
31          this.messageSource = messageSource;
32          return (T) this;
33      }
34  
35      public T exceptionStrategy(MessagingExceptionHandler exceptionListener)
36      {
37          this.exceptionListener = exceptionListener;
38          return (T) this;
39      }
40  
41      public T initialState(String initialState)
42      {
43          this.initialState = initialState;
44          return (T) this;
45      }
46  
47      public F build(MuleContext muleContext) throws MuleException
48      {
49          final F flowConstruct = buildFlowConstruct(muleContext);
50          addExceptionListener(flowConstruct);
51          if(initialState!=null)
52          {
53              flowConstruct.setInitialState(initialState);
54          }
55          return flowConstruct;
56      }
57  
58      public F buildAndRegister(MuleContext muleContext) throws MuleException
59      {
60          final F flowConstruct = build(muleContext);
61          muleContext.getRegistry().registerObject(flowConstruct.getName(), flowConstruct);
62          return flowConstruct;
63      }
64  
65      protected abstract F buildFlowConstruct(MuleContext muleContext) throws MuleException;
66  
67      protected void addExceptionListener(AbstractFlowConstruct flowConstruct)
68      {
69          if (exceptionListener != null)
70          {
71              flowConstruct.setExceptionListener(exceptionListener);
72          }
73      }
74  }