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: 61064 $
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    
092        public ExpressionClause<ChoiceType> when() {
093            WhenType when = new WhenType();
094            getWhenClauses().add(when);
095            ExpressionClause<ChoiceType> clause = new ExpressionClause<ChoiceType>(this);
096            when.setExpression(clause);
097            return clause;
098        }
099    
100    
101        public ChoiceType otherwise() {
102            OtherwiseType answer = new OtherwiseType();
103            setOtherwise(answer);
104            return this;
105        }
106    
107        // Properties
108        // -------------------------------------------------------------------------
109    
110        @Override
111        public String getLabel() {
112            CollectionStringBuffer buffer = new CollectionStringBuffer();
113            List<WhenType> list = getWhenClauses();
114            for (WhenType whenType : list) {
115                buffer.append(whenType.getLabel());
116            }
117            return buffer.toString();
118        }
119    
120        public List<WhenType> getWhenClauses() {
121            return whenClauses;
122        }
123    
124        public void setWhenClauses(List<WhenType> whenClauses) {
125            this.whenClauses = whenClauses;
126        }
127    
128        public List<ProcessorType<?>> getOutputs() {
129            if (otherwise != null) {
130                return otherwise.getOutputs();
131            } else if (whenClauses.isEmpty()) {
132                return Collections.EMPTY_LIST;
133            } else {
134                WhenType when = whenClauses.get(whenClauses.size() - 1);
135                return when.getOutputs();
136            }
137        }
138    
139        public OtherwiseType getOtherwise() {
140            return otherwise;
141        }
142    
143        public void setOtherwise(OtherwiseType otherwise) {
144            this.otherwise = otherwise;
145        }
146    }