KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > routing > inbound > SelectiveConsumer


1 /*
2  * $Id: SelectiveConsumer.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.routing.inbound;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.mule.config.i18n.Message;
16 import org.mule.config.i18n.Messages;
17 import org.mule.impl.MuleMessage;
18 import org.mule.management.stats.RouterStatistics;
19 import org.mule.umo.MessagingException;
20 import org.mule.umo.UMOEvent;
21 import org.mule.umo.UMOFilter;
22 import org.mule.umo.UMOMessage;
23 import org.mule.umo.routing.RoutingException;
24 import org.mule.umo.routing.UMOInboundRouter;
25 import org.mule.umo.transformer.TransformerException;
26
27 /**
28  * <code>SelectiveConsumer</code> is an inbound router used to filter out unwanted
29  * events. The filtering is performed by a <code>UMOFilter</code> that can be set
30  * on the router. If the event does not match the filter a
31  * <code>UMOROutnerCatchAllStrategy</code> can be set on this router to route
32  * unwanted events. If a catch strategy is not set the router just returns null.
33  *
34  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
35  * @version $Revision: 3798 $
36  */

37
38 public class SelectiveConsumer implements UMOInboundRouter
39 {
40     /**
41      * logger used by this class
42      */

43     protected transient Log logger = LogFactory.getLog(getClass());
44
45     private UMOFilter filter;
46     private boolean transformFirst = true;
47
48     private RouterStatistics routerStatistics;
49
50     public boolean isMatch(UMOEvent event) throws MessagingException
51     {
52         logger.debug("Attempting to route event");
53         if (filter == null)
54         {
55             return true;
56         }
57         UMOMessage message;
58         if (transformFirst)
59         {
60             try
61             {
62                 Object JavaDoc payload = event.getTransformedMessage();
63                 message = new MuleMessage(payload, event.getMessage());
64             }
65             catch (TransformerException e)
66             {
67                 throw new RoutingException(new Message(Messages.TRANSFORM_FAILED_BEFORE_FILTER),
68                     event.getMessage(), event.getEndpoint(), e);
69             }
70         }
71         else
72         {
73             message = event.getMessage();
74         }
75         boolean result = filter.accept(message);
76         if (logger.isDebugEnabled())
77         {
78             logger.debug((result ? "Event passed filter " : "Event did not pass filter ")
79                          + filter.getClass().getName());
80         }
81         return result;
82     }
83
84     public UMOEvent[] process(UMOEvent event) throws MessagingException
85     {
86         if (isMatch(event))
87         {
88             return new UMOEvent[]{event};
89         }
90         else
91         {
92             return null;
93         }
94     }
95
96     public UMOFilter getFilter()
97     {
98         return filter;
99     }
100
101     public void setFilter(UMOFilter filter)
102     {
103         this.filter = filter;
104     }
105
106     public boolean isTransformFirst()
107     {
108         return transformFirst;
109     }
110
111     public void setTransformFirst(boolean transformFirst)
112     {
113         this.transformFirst = transformFirst;
114     }
115
116     public void setRouterStatistics(RouterStatistics stats)
117     {
118         this.routerStatistics = stats;
119     }
120
121     public RouterStatistics getRouterStatistics()
122     {
123         return routerStatistics;
124     }
125 }
126
Popular Tags