001: /*
002: * $Id: SelectiveConsumer.java 10489 2008-01-23 17:53:38Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.routing.inbound;
012:
013: import org.mule.DefaultMuleMessage;
014: import org.mule.api.MessagingException;
015: import org.mule.api.MuleEvent;
016: import org.mule.api.MuleMessage;
017: import org.mule.api.routing.InboundRouter;
018: import org.mule.api.routing.RoutingException;
019: import org.mule.api.routing.filter.Filter;
020: import org.mule.api.transformer.TransformerException;
021: import org.mule.config.i18n.CoreMessages;
022: import org.mule.routing.AbstractRouter;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: /**
028: * <code>SelectiveConsumer</code> is an inbound router used to filter out unwanted
029: * events. The filtering is performed by a <code>Filter</code> that can be set
030: * on the router.
031: *
032: * @see InboundRouter
033: * @see org.mule.api.routing.InboundRouterCollection
034: * @see org.mule.api.routing.RouterCollection
035: */
036:
037: public class SelectiveConsumer extends AbstractRouter implements
038: InboundRouter {
039: protected final Log logger = LogFactory.getLog(getClass());
040:
041: private volatile Filter filter;
042: private volatile boolean transformFirst = true;
043:
044: public boolean isMatch(MuleEvent event) throws MessagingException {
045: if (logger.isDebugEnabled()) {
046: logger.debug("Attempting to route event: " + event.getId());
047: }
048:
049: if (filter == null) {
050: return true;
051: }
052:
053: MuleMessage message = event.getMessage();
054:
055: if (transformFirst) {
056: try {
057: Object payload = event.transformMessage();
058: message = new DefaultMuleMessage(payload, message);
059: } catch (TransformerException e) {
060: throw new RoutingException(CoreMessages
061: .transformFailedBeforeFilter(), event
062: .getMessage(), event.getEndpoint(), e);
063: }
064: }
065:
066: boolean result = filter.accept(message);
067:
068: if (logger.isDebugEnabled()) {
069: logger.debug("MuleEvent "
070: + event.getId()
071: + (result ? " passed filter "
072: : " did not pass filter ")
073: + filter.getClass().getName());
074: }
075:
076: return result;
077: }
078:
079: public MuleEvent[] process(MuleEvent event)
080: throws MessagingException {
081: if (this .isMatch(event)) {
082: return new MuleEvent[] { event };
083: } else {
084: return null;
085: }
086: }
087:
088: public Filter getFilter() {
089: return filter;
090: }
091:
092: public void setFilter(Filter filter) {
093: this .filter = filter;
094: }
095:
096: public boolean isTransformFirst() {
097: return transformFirst;
098: }
099:
100: public void setTransformFirst(boolean transformFirst) {
101: this.transformFirst = transformFirst;
102: }
103: }
|