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 */
017package org.apache.camel.reifier;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.apache.camel.ExpressionFactory;
023import org.apache.camel.Predicate;
024import org.apache.camel.Processor;
025import org.apache.camel.Route;
026import org.apache.camel.builder.ExpressionClause;
027import org.apache.camel.model.ChoiceDefinition;
028import org.apache.camel.model.ProcessorDefinition;
029import org.apache.camel.model.WhenDefinition;
030import org.apache.camel.model.language.ExpressionDefinition;
031import org.apache.camel.processor.ChoiceProcessor;
032import org.apache.camel.processor.FilterProcessor;
033
034public class ChoiceReifier extends ProcessorReifier<ChoiceDefinition> {
035
036    public ChoiceReifier(Route route, ProcessorDefinition<?> definition) {
037        super(route, ChoiceDefinition.class.cast(definition));
038    }
039
040    @Override
041    public Processor createProcessor() throws Exception {
042        List<FilterProcessor> filters = new ArrayList<>();
043        for (WhenDefinition whenClause : definition.getWhenClauses()) {
044            ExpressionDefinition exp = whenClause.getExpression();
045            if (exp.getExpressionType() != null) {
046                exp = exp.getExpressionType();
047            }
048            Predicate pre = exp.getPredicate();
049            if (pre instanceof ExpressionClause) {
050                ExpressionClause<?> clause = (ExpressionClause<?>)pre;
051                if (clause.getExpressionType() != null) {
052                    // if using the Java DSL then the expression may have been
053                    // set using the
054                    // ExpressionClause which is a fancy builder to define
055                    // expressions and predicates
056                    // using fluent builders in the DSL. However we need
057                    // afterwards a callback to
058                    // reset the expression to the expression type the
059                    // ExpressionClause did build for us
060                    ExpressionFactory model = clause.getExpressionType();
061                    if (model instanceof ExpressionDefinition) {
062                        whenClause.setExpression((ExpressionDefinition)model);
063                    }
064                }
065                exp = whenClause.getExpression();
066            }
067
068            FilterProcessor filter = (FilterProcessor)createProcessor(whenClause);
069            filters.add(filter);
070        }
071        Processor otherwiseProcessor = null;
072        if (definition.getOtherwise() != null) {
073            otherwiseProcessor = createProcessor(definition.getOtherwise());
074        }
075        return new ChoiceProcessor(filters, otherwiseProcessor);
076    }
077
078}