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