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