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