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.PollEnrichDefinition;
024import org.apache.camel.model.ProcessorDefinition;
025import org.apache.camel.processor.PollEnricher;
026import org.apache.camel.processor.aggregate.AggregationStrategyBeanAdapter;
027import org.apache.camel.spi.RouteContext;
028
029public class PollEnrichReifier extends ProcessorReifier<PollEnrichDefinition> {
030
031    public PollEnrichReifier(ProcessorDefinition<?> definition) {
032        super((PollEnrichDefinition)definition);
033    }
034
035    @Override
036    public Processor createProcessor(RouteContext routeContext) throws Exception {
037
038        // if no timeout then we should block, and there use a negative timeout
039        long time = definition.getTimeout() != null ? definition.getTimeout() : -1;
040        boolean isIgnoreInvalidEndpoint = definition.getIgnoreInvalidEndpoint() != null && definition.getIgnoreInvalidEndpoint();
041        Expression exp = definition.getExpression().createExpression(routeContext);
042
043        PollEnricher enricher = new PollEnricher(exp, time);
044
045        AggregationStrategy strategy = createAggregationStrategy(routeContext);
046        if (strategy == null) {
047            enricher.setDefaultAggregationStrategy();
048        } else {
049            enricher.setAggregationStrategy(strategy);
050        }
051        if (definition.getAggregateOnException() != null) {
052            enricher.setAggregateOnException(definition.getAggregateOnException());
053        }
054        if (definition.getCacheSize() != null) {
055            enricher.setCacheSize(definition.getCacheSize());
056        }
057        enricher.setIgnoreInvalidEndpoint(isIgnoreInvalidEndpoint);
058
059        return enricher;
060    }
061
062    private AggregationStrategy createAggregationStrategy(RouteContext routeContext) {
063        AggregationStrategy strategy = definition.getAggregationStrategy();
064        if (strategy == null && definition.getAggregationStrategyRef() != null) {
065            Object aggStrategy = routeContext.lookup(definition.getAggregationStrategyRef(), Object.class);
066            if (aggStrategy instanceof AggregationStrategy) {
067                strategy = (AggregationStrategy)aggStrategy;
068            } else if (aggStrategy != null) {
069                AggregationStrategyBeanAdapter adapter = new AggregationStrategyBeanAdapter(aggStrategy, definition.getAggregationStrategyMethodName());
070                if (definition.getAggregationStrategyMethodAllowNull() != null) {
071                    adapter.setAllowNullNewExchange(definition.getAggregationStrategyMethodAllowNull());
072                    adapter.setAllowNullOldExchange(definition.getAggregationStrategyMethodAllowNull());
073                }
074                strategy = adapter;
075            } else {
076                throw new IllegalArgumentException("Cannot find AggregationStrategy in Registry with name: " + definition.getAggregationStrategyRef());
077            }
078        }
079
080        if (strategy instanceof CamelContextAware) {
081            ((CamelContextAware)strategy).setCamelContext(routeContext.getCamelContext());
082        }
083
084        return strategy;
085    }
086
087}