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 org.apache.camel.AggregationStrategy; 020import org.apache.camel.CamelContextAware; 021import org.apache.camel.Expression; 022import org.apache.camel.Processor; 023import org.apache.camel.model.EnrichDefinition; 024import org.apache.camel.model.ProcessorDefinition; 025import org.apache.camel.processor.Enricher; 026import org.apache.camel.processor.aggregate.AggregationStrategyBeanAdapter; 027import org.apache.camel.spi.RouteContext; 028 029public class EnrichReifier extends ExpressionReifier<EnrichDefinition> { 030 031 public EnrichReifier(ProcessorDefinition<?> definition) { 032 super(EnrichDefinition.class.cast(definition)); 033 } 034 035 @Override 036 public Processor createProcessor(RouteContext routeContext) throws Exception { 037 038 Expression exp = definition.getExpression().createExpression(routeContext); 039 boolean isShareUnitOfWork = definition.getShareUnitOfWork() != null && definition.getShareUnitOfWork(); 040 boolean isIgnoreInvalidEndpoint = definition.getIgnoreInvalidEndpoint() != null && definition.getIgnoreInvalidEndpoint(); 041 042 Enricher enricher = new Enricher(exp); 043 enricher.setShareUnitOfWork(isShareUnitOfWork); 044 enricher.setIgnoreInvalidEndpoint(isIgnoreInvalidEndpoint); 045 AggregationStrategy strategy = createAggregationStrategy(routeContext); 046 if (strategy != null) { 047 enricher.setAggregationStrategy(strategy); 048 } 049 if (definition.getAggregateOnException() != null) { 050 enricher.setAggregateOnException(definition.getAggregateOnException()); 051 } 052 return enricher; 053 } 054 055 private AggregationStrategy createAggregationStrategy(RouteContext routeContext) { 056 AggregationStrategy strategy = definition.getAggregationStrategy(); 057 if (strategy == null && definition.getAggregationStrategyRef() != null) { 058 Object aggStrategy = routeContext.lookup(definition.getAggregationStrategyRef(), Object.class); 059 if (aggStrategy instanceof AggregationStrategy) { 060 strategy = (AggregationStrategy)aggStrategy; 061 } else if (aggStrategy != null) { 062 AggregationStrategyBeanAdapter adapter = new AggregationStrategyBeanAdapter(aggStrategy, definition.getAggregationStrategyMethodName()); 063 if (definition.getAggregationStrategyMethodAllowNull() != null) { 064 adapter.setAllowNullNewExchange(definition.getAggregationStrategyMethodAllowNull()); 065 adapter.setAllowNullOldExchange(definition.getAggregationStrategyMethodAllowNull()); 066 } 067 strategy = adapter; 068 } else { 069 throw new IllegalArgumentException("Cannot find AggregationStrategy in Registry with name: " + definition.getAggregationStrategyRef()); 070 } 071 } 072 073 if (strategy instanceof CamelContextAware) { 074 ((CamelContextAware)strategy).setCamelContext(routeContext.getCamelContext()); 075 } 076 077 return strategy; 078 } 079 080}