001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.processor;
018
019import org.apache.camel.AsyncCallback;
020import org.apache.camel.Exchange;
021import org.apache.camel.Predicate;
022import org.apache.camel.Processor;
023import org.apache.camel.Traceable;
024import org.apache.camel.util.ServiceHelper;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028/**
029 * The processor which implements the
030 * <a href="http://camel.apache.org/message-filter.html">Message Filter</a> EIP pattern.
031 *
032 * @version 
033 */
034public class FilterProcessor extends DelegateAsyncProcessor implements Traceable {
035    private static final Logger LOG = LoggerFactory.getLogger(FilterProcessor.class);
036    private final Predicate predicate;
037
038    public FilterProcessor(Predicate predicate, Processor processor) {
039        super(processor);
040        this.predicate = predicate;
041    }
042
043    @Override
044    public boolean process(Exchange exchange, AsyncCallback callback) {
045        boolean matches = false;
046        try {
047            matches = predicate.matches(exchange);
048        } catch (Throwable e) {
049            exchange.setException(e);
050        }
051
052        LOG.debug("Filter matches: {} for exchange: {}", matches, exchange);
053
054        // set property whether the filter matches or not
055        exchange.setProperty(Exchange.FILTER_MATCHED, matches);
056
057        if (matches) {
058            return processor.process(exchange, callback);
059        } else {
060            callback.done(true);
061            return true;
062        }
063    }
064
065    @Override
066    public String toString() {
067        return "Filter[if: " + predicate + " do: " + getProcessor() + "]";
068    }
069
070    public String getTraceLabel() {
071        return "filter[if: " + predicate + "]";
072    }
073
074    public Predicate getPredicate() {
075        return predicate;
076    }
077
078    @Override
079    protected void doStart() throws Exception {
080        super.doStart();
081        ServiceHelper.startService(predicate);
082    }
083
084    @Override
085    protected void doStop() throws Exception {
086        ServiceHelper.stopService(predicate);
087        super.doStop();
088    }
089}