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