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.converter.DefaultTypeConverter;
033 import org.apache.camel.processor.ChoiceProcessor;
034 import org.apache.camel.processor.FilterProcessor;
035 import org.apache.camel.spi.RouteContext;
036 import org.apache.camel.util.CollectionStringBuffer;
037 import org.apache.commons.logging.Log;
038 import org.apache.commons.logging.LogFactory;
039
040 /**
041 * Represents an XML <choice/> element
042 *
043 * @version $Revision: 671918 $
044 */
045 @XmlRootElement(name = "choice")
046 @XmlAccessorType(XmlAccessType.FIELD)
047 public class ChoiceType extends ProcessorType<ChoiceType> {
048
049 private static final transient Log LOG = LogFactory.getLog(ChoiceType.class);
050
051 @XmlElementRef
052 private List<WhenType> whenClauses = new ArrayList<WhenType>();
053 @XmlElement(required = false)
054 private OtherwiseType otherwise;
055
056 @Override
057 public String toString() {
058 if (getOtherwise() != null) {
059 return "Choice[ " + getWhenClauses() + " " + getOtherwise() + "]";
060 } else {
061 return "Choice[ " + getWhenClauses() + "]";
062
063 }
064 }
065 @Override
066 public String getShortName() {
067 return "choice";
068 }
069
070 @Override
071 public Processor createProcessor(RouteContext routeContext) throws Exception {
072 List<FilterProcessor> filters = new ArrayList<FilterProcessor>();
073 for (WhenType whenClaus : whenClauses) {
074 filters.add(whenClaus.createProcessor(routeContext));
075 }
076 Processor otherwiseProcessor = null;
077 if (otherwise != null) {
078 otherwiseProcessor = otherwise.createProcessor(routeContext);
079 } else {
080 LOG.warn("No otherwise clause was specified for a choice block -- any unmatched exchanges will be dropped");
081 }
082 return new ChoiceProcessor(filters, otherwiseProcessor);
083 }
084
085 // Fluent API
086 // -------------------------------------------------------------------------
087 public ChoiceType when(Predicate predicate) {
088 getWhenClauses().add(new WhenType(predicate));
089 return this;
090 }
091
092
093 public ExpressionClause<ChoiceType> when() {
094 WhenType when = new WhenType();
095 getWhenClauses().add(when);
096 ExpressionClause<ChoiceType> clause = new ExpressionClause<ChoiceType>(this);
097 when.setExpression(clause);
098 return clause;
099 }
100
101
102 public ChoiceType otherwise() {
103 OtherwiseType answer = new OtherwiseType();
104 setOtherwise(answer);
105 return this;
106 }
107
108 // Properties
109 // -------------------------------------------------------------------------
110
111 @Override
112 public String getLabel() {
113 CollectionStringBuffer buffer = new CollectionStringBuffer();
114 List<WhenType> list = getWhenClauses();
115 for (WhenType whenType : list) {
116 buffer.append(whenType.getLabel());
117 }
118 return buffer.toString();
119 }
120
121 public List<WhenType> getWhenClauses() {
122 return whenClauses;
123 }
124
125 public void setWhenClauses(List<WhenType> whenClauses) {
126 this.whenClauses = whenClauses;
127 }
128
129 public List<ProcessorType<?>> getOutputs() {
130 if (otherwise != null) {
131 return otherwise.getOutputs();
132 } else if (whenClauses.isEmpty()) {
133 return Collections.EMPTY_LIST;
134 } else {
135 WhenType when = whenClauses.get(whenClauses.size() - 1);
136 return when.getOutputs();
137 }
138 }
139
140 public OtherwiseType getOtherwise() {
141 return otherwise;
142 }
143
144 public void setOtherwise(OtherwiseType otherwise) {
145 this.otherwise = otherwise;
146 }
147 }