1
2
3
4
5
6
7
8
9
10
11 package org.mule.interceptor;
12
13 import org.mule.api.MuleEvent;
14 import org.mule.api.MuleException;
15 import org.mule.api.interceptor.Interceptor;
16 import org.mule.api.lifecycle.Disposable;
17 import org.mule.api.lifecycle.Initialisable;
18 import org.mule.api.lifecycle.InitialisationException;
19 import org.mule.api.processor.MessageProcessor;
20 import org.mule.processor.AbstractInterceptingMessageProcessor;
21 import org.mule.processor.chain.DefaultMessageProcessorChainBuilder;
22
23 import java.util.List;
24
25
26
27
28 public class InterceptorStack extends AbstractInterceptingMessageProcessor
29 implements Interceptor, Initialisable, Disposable
30 {
31
32 private List<Interceptor> interceptors;
33 private MessageProcessor chain;
34
35 public InterceptorStack()
36 {
37
38 }
39
40 public InterceptorStack(List<Interceptor> interceptors)
41 {
42 this.interceptors = interceptors;
43 }
44
45 public MuleEvent process(MuleEvent event) throws MuleException
46 {
47 return chain.process(event);
48 }
49
50 public List<Interceptor> getInterceptors()
51 {
52 return interceptors;
53 }
54
55 public void setInterceptors(List<Interceptor> interceptors)
56 {
57 this.interceptors = interceptors;
58 }
59
60 public void initialise() throws InitialisationException
61 {
62 DefaultMessageProcessorChainBuilder chainBuilder = new DefaultMessageProcessorChainBuilder();
63 for (Interceptor interceptor : interceptors)
64 {
65 if (interceptor instanceof Initialisable)
66 {
67 ((Initialisable) interceptor).initialise();
68 }
69 chainBuilder.chain(interceptor);
70 }
71 if (next != null)
72 {
73 chainBuilder.chain(next);
74 }
75 try
76 {
77 chain = chainBuilder.build();
78 }
79 catch (MuleException e)
80 {
81 throw new InitialisationException(e, this);
82 }
83 }
84
85 public void dispose()
86 {
87 for (Interceptor interceptor : interceptors)
88 {
89 if (interceptor instanceof Disposable)
90 {
91 ((Disposable) interceptor).dispose();
92 }
93 }
94 }
95
96 @Override
97 public int hashCode()
98 {
99 final int prime = 31;
100 int result = 1;
101 result = prime * result + ((interceptors == null) ? 0 : interceptors.hashCode());
102 return result;
103 }
104
105 @Override
106 public boolean equals(Object obj)
107 {
108 if (this == obj) return true;
109 if (obj == null) return false;
110 if (getClass() != obj.getClass()) return false;
111 InterceptorStack other = (InterceptorStack) obj;
112 if (interceptors == null)
113 {
114 if (other.interceptors != null) return false;
115 }
116 else if (!interceptors.equals(other.interceptors)) return false;
117 return true;
118 }
119
120 }