001 /** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 package org.apache.camel.processor; 019 020 import org.apache.camel.Predicate; 021 import org.apache.camel.Processor; 022 import org.apache.camel.Exchange; 023 import org.apache.camel.util.ServiceHelper; 024 import org.apache.camel.impl.ServiceSupport; 025 026 import java.util.ArrayList; 027 import java.util.List; 028 029 /** 030 * Implements a Choice structure where one or more predicates are used which if they are true their processors 031 * are used, with a default otherwise clause used if none match. 032 * 033 * @version $Revision: 534145 $ 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 } 065 else { 066 builder.append(", "); 067 } 068 builder.append("when "); 069 builder.append(processor.getPredicate().toString()); 070 builder.append(": "); 071 builder.append(processor.getProcessor()); 072 } 073 if (otherwise != null) { 074 builder.append(", otherwise: "); 075 builder.append(otherwise); 076 } 077 builder.append("}"); 078 return builder.toString(); 079 } 080 081 public List<FilterProcessor> getFilters() { 082 return filters; 083 } 084 085 public Processor getOtherwise() { 086 return otherwise; 087 } 088 089 090 protected void doStart() throws Exception { 091 ServiceHelper.startServices(filters); 092 ServiceHelper.startServices(otherwise); 093 } 094 095 protected void doStop() throws Exception { 096 ServiceHelper.stopServices(otherwise); 097 ServiceHelper.stopServices(filters); 098 } 099 }