1 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 37 38 public class SelectiveConsumer implements UMOInboundRouter 39 { 40 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 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 |