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 chainBuilder.setName("interceptor stack");
64 for (Interceptor interceptor : interceptors)
65 {
66 if (interceptor instanceof Initialisable)
67 {
68 ((Initialisable) interceptor).initialise();
69 }
70 chainBuilder.chain(interceptor);
71 }
72 if (next != null)
73 {
74 chainBuilder.chain(next);
75 }
76 try
77 {
78 chain = chainBuilder.build();
79 }
80 catch (MuleException e)
81 {
82 throw new InitialisationException(e, this);
83 }
84 }
85
86 public void dispose()
87 {
88 for (Interceptor interceptor : interceptors)
89 {
90 if (interceptor instanceof Disposable)
91 {
92 ((Disposable) interceptor).dispose();
93 }
94 }
95 }
96
97 @Override
98 public int hashCode()
99 {
100 final int prime = 31;
101 int result = 1;
102 result = prime * result + ((interceptors == null) ? 0 : interceptors.hashCode());
103 return result;
104 }
105
106 @Override
107 public boolean equals(Object obj)
108 {
109 if (this == obj) return true;
110 if (obj == null) return false;
111 if (getClass() != obj.getClass()) return false;
112 InterceptorStack other = (InterceptorStack) obj;
113 if (interceptors == null)
114 {
115 if (other.interceptors != null) return false;
116 }
117 else if (!interceptors.equals(other.interceptors)) return false;
118 return true;
119 }
120
121 }