1
2
3
4
5
6
7
8
9
10
11 package org.mule.processor.builder;
12
13 import org.mule.api.MuleContext;
14 import org.mule.api.MuleEvent;
15 import org.mule.api.MuleException;
16 import org.mule.api.construct.FlowConstruct;
17 import org.mule.api.construct.FlowConstructAware;
18 import org.mule.api.context.MuleContextAware;
19 import org.mule.api.lifecycle.Disposable;
20 import org.mule.api.lifecycle.Initialisable;
21 import org.mule.api.lifecycle.InitialisationException;
22 import org.mule.api.lifecycle.Lifecycle;
23 import org.mule.api.lifecycle.Startable;
24 import org.mule.api.lifecycle.Stoppable;
25 import org.mule.api.processor.InterceptingMessageProcessor;
26 import org.mule.api.processor.MessageProcessor;
27 import org.mule.util.StringUtils;
28
29 import java.util.Iterator;
30 import java.util.List;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35
36
37
38
39
40
41 public class InterceptingChainCompositeMessageProcessor implements MessageProcessor, Lifecycle, FlowConstructAware, MuleContextAware
42 {
43 private Log log;
44 private String name;
45 private MessageProcessor firstInChain;
46 private List<MessageProcessor> allProcessors;
47
48 public InterceptingChainCompositeMessageProcessor(InterceptingMessageProcessor firstInChain,
49 List<MessageProcessor> allProcessors,
50 String name)
51 {
52 this.name = name;
53 this.firstInChain = firstInChain;
54 this.allProcessors = allProcessors;
55
56 log = LogFactory.getLog(InterceptingChainCompositeMessageProcessor.class);
57 }
58
59 public MuleEvent process(MuleEvent event) throws MuleException
60 {
61 if (log.isDebugEnabled())
62 {
63 log.debug("Invoking " + this + " with event " + event);
64 }
65 return firstInChain.process(event);
66 }
67
68 public void initialise() throws InitialisationException
69 {
70 for (MessageProcessor processor : allProcessors)
71 {
72
73 if (processor instanceof Initialisable
74 {
75 ((Initialisable) processor).initialise();
76 }
77 }
78 }
79
80 public void start() throws MuleException
81 {
82 for (MessageProcessor processor : allProcessors)
83 {
84 if (processor instanceof Startable)
85 {
86 ((Startable) processor).start();
87 }
88 }
89 }
90
91 public void stop() throws MuleException
92 {
93 for (MessageProcessor processor : allProcessors)
94 {
95 if (processor instanceof Stoppable)
96 {
97 ((Stoppable) processor).stop();
98 }
99 }
100 }
101
102 public void dispose()
103 {
104 for (MessageProcessor processor : allProcessors)
105 {
106 if (processor instanceof Disposable)
107 {
108 ((Disposable) processor).dispose();
109 }
110 }
111 firstInChain = null;
112 allProcessors.clear();
113 }
114
115 public void setFlowConstruct(FlowConstruct flowConstruct)
116 {
117 for (MessageProcessor processor : allProcessors)
118 {
119 if (processor instanceof FlowConstructAware)
120 {
121 ((FlowConstructAware) processor).setFlowConstruct(flowConstruct);
122 }
123 }
124 }
125
126 public void setMuleContext(MuleContext context)
127 {
128 for (MessageProcessor processor : allProcessors)
129 {
130 if (processor instanceof MuleContextAware)
131 {
132 ((MuleContextAware) processor).setMuleContext(context);
133 }
134 }
135 }
136
137 @Override
138 public String toString()
139 {
140 StringBuffer string = new StringBuffer();
141 string.append("InterceptingChainCompositeMessageProcessor ");
142 if (name != null)
143 {
144 string.append(" '" + name + "' ");
145 }
146
147 Iterator<MessageProcessor> mpIterator = allProcessors.iterator();
148 if (mpIterator.hasNext())
149 {
150 string.append("\n[ ");
151 while (mpIterator.hasNext())
152 {
153 MessageProcessor mp = mpIterator.next();
154 string.append("\n " + StringUtils.replace(mp.toString(), "\n", "\n "));
155 if (mpIterator.hasNext())
156 {
157 string.append(", ");
158 }
159 }
160 string.append("\n]");
161 }
162
163 return string.toString();
164 }
165 }