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