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.model;
018
019 import java.util.ArrayList;
020 import java.util.Collections;
021 import java.util.List;
022
023 import javax.xml.bind.annotation.XmlAccessType;
024 import javax.xml.bind.annotation.XmlAccessorType;
025 import javax.xml.bind.annotation.XmlElement;
026 import javax.xml.bind.annotation.XmlElementRef;
027 import javax.xml.bind.annotation.XmlRootElement;
028
029 import org.apache.camel.Predicate;
030 import org.apache.camel.Processor;
031 import org.apache.camel.builder.ExpressionClause;
032 import org.apache.camel.impl.RouteContext;
033 import org.apache.camel.processor.ChoiceProcessor;
034 import org.apache.camel.processor.FilterProcessor;
035 import org.apache.camel.util.CollectionStringBuffer;
036
037 /**
038 * @version $Revision: 36321 $
039 */
040 @XmlRootElement(name = "choice")
041 @XmlAccessorType(XmlAccessType.FIELD)
042 public class ChoiceType extends ProcessorType<ChoiceType> {
043 @XmlElementRef
044 private List<WhenType> whenClauses = new ArrayList<WhenType>();
045 @XmlElement(required = false)
046 private OtherwiseType otherwise;
047
048 @Override
049 public String toString() {
050 return "Choice[ " + getWhenClauses() + " " + getOtherwise() + "]";
051 }
052
053 @Override
054 public Processor createProcessor(RouteContext routeContext) throws Exception {
055 List<FilterProcessor> filters = new ArrayList<FilterProcessor>();
056 for (WhenType whenClaus : whenClauses) {
057 filters.add(whenClaus.createProcessor(routeContext));
058 }
059 Processor otherwiseProcessor = null;
060 if (otherwise != null) {
061 otherwiseProcessor = otherwise.createProcessor(routeContext);
062 }
063 return new ChoiceProcessor(filters, otherwiseProcessor);
064 }
065
066 // Fluent API
067 // -------------------------------------------------------------------------
068 public ChoiceType when(Predicate predicate) {
069 getWhenClauses().add(new WhenType(predicate));
070 return this;
071 }
072
073
074 public ExpressionClause<ChoiceType> when() {
075 WhenType when = new WhenType();
076 getWhenClauses().add(when);
077 ExpressionClause<ChoiceType> clause = new ExpressionClause<ChoiceType>(this);
078 when.setExpression(clause);
079 return clause;
080 }
081
082
083 public ChoiceType otherwise() {
084 OtherwiseType answer = new OtherwiseType();
085 setOtherwise(answer);
086 return this;
087 }
088
089 // Properties
090 // -------------------------------------------------------------------------
091
092 @Override
093 public String getLabel() {
094 CollectionStringBuffer buffer = new CollectionStringBuffer();
095 List<WhenType> list = getWhenClauses();
096 for (WhenType whenType : list) {
097 buffer.append(whenType.getLabel());
098 }
099 return buffer.toString();
100 }
101
102 public List<WhenType> getWhenClauses() {
103 return whenClauses;
104 }
105
106 public void setWhenClauses(List<WhenType> whenClauses) {
107 this.whenClauses = whenClauses;
108 }
109
110 public List<ProcessorType<?>> getOutputs() {
111 if (otherwise != null) {
112 return otherwise.getOutputs();
113 } else if (whenClauses.isEmpty()) {
114 return Collections.EMPTY_LIST;
115 } else {
116 WhenType when = whenClauses.get(whenClauses.size() - 1);
117 return when.getOutputs();
118 }
119 }
120
121 public OtherwiseType getOtherwise() {
122 return otherwise;
123 }
124
125 public void setOtherwise(OtherwiseType otherwise) {
126 this.otherwise = otherwise;
127 }
128 }