1
2
3
4
5
6
7
8
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
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 }