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