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     */
017    package org.apache.camel.processor;
018    
019    import java.util.ArrayList;
020    import java.util.List;
021    
022    import org.apache.camel.Exchange;
023    import org.apache.camel.Predicate;
024    import org.apache.camel.Processor;
025    import org.apache.camel.impl.ServiceSupport;
026    import org.apache.camel.util.ServiceHelper;
027    
028    /**
029     * Implements a Choice structure where one or more predicates are used which if
030     * they are true their processors are used, with a default otherwise clause used
031     * if none match.
032     * 
033     * @version $Revision: 35332 $
034     */
035    public class ChoiceProcessor extends ServiceSupport implements Processor {
036        private List<FilterProcessor> filters = new ArrayList<FilterProcessor>();
037        private Processor otherwise;
038    
039        public ChoiceProcessor(List<FilterProcessor> filters, Processor otherwise) {
040            this.filters = filters;
041            this.otherwise = otherwise;
042        }
043    
044        public void process(Exchange exchange) throws Exception {
045            for (FilterProcessor filterProcessor : filters) {
046                Predicate<Exchange> predicate = filterProcessor.getPredicate();
047                if (predicate != null && predicate.matches(exchange)) {
048                    filterProcessor.getProcessor().process(exchange);
049                    return;
050                }
051            }
052            if (otherwise != null) {
053                otherwise.process(exchange);
054            }
055        }
056    
057        @Override
058        public String toString() {
059            StringBuilder builder = new StringBuilder("choice{");
060            boolean first = true;
061            for (FilterProcessor processor : filters) {
062                if (first) {
063                    first = false;
064                } else {
065                    builder.append(", ");
066                }
067                builder.append("when ");
068                builder.append(processor.getPredicate().toString());
069                builder.append(": ");
070                builder.append(processor.getProcessor());
071            }
072            if (otherwise != null) {
073                builder.append(", otherwise: ");
074                builder.append(otherwise);
075            }
076            builder.append("}");
077            return builder.toString();
078        }
079    
080        public List<FilterProcessor> getFilters() {
081            return filters;
082        }
083    
084        public Processor getOtherwise() {
085            return otherwise;
086        }
087    
088        protected void doStart() throws Exception {
089            ServiceHelper.startServices(filters);
090            ServiceHelper.startServices(otherwise);
091        }
092    
093        protected void doStop() throws Exception {
094            ServiceHelper.stopServices(otherwise);
095            ServiceHelper.stopServices(filters);
096        }
097    }