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