1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.routing; |
8 | |
|
9 | |
import org.mule.api.MuleContext; |
10 | |
import org.mule.api.MuleEvent; |
11 | |
import org.mule.api.MuleException; |
12 | |
import org.mule.api.construct.FlowConstruct; |
13 | |
import org.mule.api.construct.FlowConstructAware; |
14 | |
import org.mule.api.context.MuleContextAware; |
15 | |
import org.mule.api.lifecycle.Disposable; |
16 | |
import org.mule.api.lifecycle.Initialisable; |
17 | |
import org.mule.api.lifecycle.InitialisationException; |
18 | |
import org.mule.api.lifecycle.Lifecycle; |
19 | |
import org.mule.api.lifecycle.Startable; |
20 | |
import org.mule.api.lifecycle.Stoppable; |
21 | |
import org.mule.api.processor.InterceptingMessageProcessor; |
22 | |
import org.mule.api.processor.MessageProcessor; |
23 | |
import org.mule.api.routing.filter.Filter; |
24 | |
import org.mule.api.routing.filter.FilterUnacceptedException; |
25 | |
import org.mule.config.i18n.CoreMessages; |
26 | |
import org.mule.processor.AbstractFilteringMessageProcessor; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class MessageFilter extends AbstractFilteringMessageProcessor implements FlowConstructAware, Lifecycle |
40 | |
{ |
41 | |
protected Filter filter; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
@Deprecated |
49 | |
public MessageFilter() |
50 | |
{ |
51 | 0 | super(); |
52 | 0 | } |
53 | |
|
54 | |
public MessageFilter(Filter filter) |
55 | |
{ |
56 | 0 | super(); |
57 | 0 | this.filter = filter; |
58 | 0 | } |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public MessageFilter(Filter filter, boolean throwExceptionOnUnaccepted, MessageProcessor messageProcessor) |
67 | 0 | { |
68 | 0 | this.filter = filter; |
69 | 0 | this.throwOnUnaccepted = throwExceptionOnUnaccepted; |
70 | 0 | this.unacceptedMessageProcessor = messageProcessor; |
71 | 0 | setUnacceptedMessageProcessor(unacceptedMessageProcessor); |
72 | 0 | } |
73 | |
|
74 | |
@Override |
75 | |
protected boolean accept(MuleEvent event) |
76 | |
{ |
77 | 0 | if (filter == null) |
78 | |
{ |
79 | 0 | return true; |
80 | |
} |
81 | |
|
82 | 0 | if (event != null) |
83 | |
{ |
84 | 0 | return filter.accept(event.getMessage()); |
85 | |
} |
86 | |
else |
87 | |
{ |
88 | 0 | return false; |
89 | |
} |
90 | |
} |
91 | |
|
92 | |
@Override |
93 | |
protected MuleException filterUnacceptedException(MuleEvent event) |
94 | |
{ |
95 | 0 | return new FilterUnacceptedException(CoreMessages.messageRejectedByFilter(), event, filter); |
96 | |
} |
97 | |
|
98 | |
public Filter getFilter() |
99 | |
{ |
100 | 0 | return filter; |
101 | |
} |
102 | |
|
103 | |
public void setFilter(Filter filter) |
104 | |
{ |
105 | 0 | this.filter = filter; |
106 | 0 | } |
107 | |
|
108 | |
@Override |
109 | |
public String toString() |
110 | |
{ |
111 | 0 | return (filter == null ? "null filter" : filter.getClass().getName()) + " (wrapped by " |
112 | |
+ this.getClass().getSimpleName() + ")"; |
113 | |
} |
114 | |
|
115 | |
@Override |
116 | |
public void setMuleContext(MuleContext context) |
117 | |
{ |
118 | 0 | super.setMuleContext(context); |
119 | 0 | if (!onUnacceptedFlowConstruct && unacceptedMessageProcessor instanceof MuleContextAware) |
120 | |
{ |
121 | 0 | ((MuleContextAware) unacceptedMessageProcessor).setMuleContext(context); |
122 | |
} |
123 | 0 | } |
124 | |
|
125 | |
public void setFlowConstruct(FlowConstruct flowConstruct) |
126 | |
{ |
127 | 0 | if (!onUnacceptedFlowConstruct && unacceptedMessageProcessor instanceof FlowConstructAware) |
128 | |
{ |
129 | 0 | ((FlowConstructAware) unacceptedMessageProcessor).setFlowConstruct(flowConstruct); |
130 | |
} |
131 | 0 | } |
132 | |
|
133 | |
public void initialise() throws InitialisationException |
134 | |
{ |
135 | 0 | if (!onUnacceptedFlowConstruct && unacceptedMessageProcessor instanceof Initialisable) |
136 | |
{ |
137 | 0 | ((Initialisable) unacceptedMessageProcessor).initialise(); |
138 | |
} |
139 | 0 | } |
140 | |
|
141 | |
public void start() throws MuleException |
142 | |
{ |
143 | 0 | if (!onUnacceptedFlowConstruct && unacceptedMessageProcessor instanceof Startable) |
144 | |
{ |
145 | 0 | ((Startable) unacceptedMessageProcessor).start(); |
146 | |
} |
147 | 0 | } |
148 | |
|
149 | |
public void stop() throws MuleException |
150 | |
{ |
151 | 0 | if (!onUnacceptedFlowConstruct && unacceptedMessageProcessor instanceof Stoppable) |
152 | |
{ |
153 | 0 | ((Stoppable) unacceptedMessageProcessor).stop(); |
154 | |
} |
155 | 0 | } |
156 | |
|
157 | |
public void dispose() |
158 | |
{ |
159 | 0 | if (!onUnacceptedFlowConstruct && unacceptedMessageProcessor instanceof Disposable) |
160 | |
{ |
161 | 0 | ((Disposable) unacceptedMessageProcessor).dispose(); |
162 | |
} |
163 | 0 | } |
164 | |
} |