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: 792 $ 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 // process next will also take care (has not null test) if next was a stop(). 049 // stop() has no processor to execute, and thus we will end in a NPE 050 filterProcessor.processNext(exchange); 051 return; 052 } 053 } 054 if (otherwise != null) { 055 otherwise.process(exchange); 056 } 057 } 058 059 @Override 060 public String toString() { 061 StringBuilder builder = new StringBuilder("choice{"); 062 boolean first = true; 063 for (FilterProcessor processor : filters) { 064 if (first) { 065 first = false; 066 } else { 067 builder.append(", "); 068 } 069 builder.append("when "); 070 builder.append(processor.getPredicate().toString()); 071 builder.append(": "); 072 builder.append(processor.getProcessor()); 073 } 074 if (otherwise != null) { 075 builder.append(", otherwise: "); 076 builder.append(otherwise); 077 } 078 builder.append("}"); 079 return builder.toString(); 080 } 081 082 public List<FilterProcessor> getFilters() { 083 return filters; 084 } 085 086 public Processor getOtherwise() { 087 return otherwise; 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 }