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