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