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.concurrent.ScheduledExecutorService;
020
021import org.apache.camel.Expression;
022import org.apache.camel.Processor;
023import org.apache.camel.model.DelayDefinition;
024import org.apache.camel.model.ProcessorDefinition;
025import org.apache.camel.model.ProcessorDefinitionHelper;
026import org.apache.camel.model.language.ExpressionDefinition;
027import org.apache.camel.processor.Delayer;
028import org.apache.camel.spi.RouteContext;
029
030public class DelayReifier extends ExpressionReifier<DelayDefinition> {
031
032    public DelayReifier(ProcessorDefinition<?> definition) {
033        super(DelayDefinition.class.cast(definition));
034    }
035
036    @Override
037    public Processor createProcessor(RouteContext routeContext) throws Exception {
038        Processor childProcessor = this.createChildProcessor(routeContext, false);
039        Expression delay = createAbsoluteTimeDelayExpression(routeContext);
040
041        boolean async = definition.getAsyncDelayed() == null || definition.getAsyncDelayed();
042        boolean shutdownThreadPool = ProcessorDefinitionHelper.willCreateNewThreadPool(routeContext, definition, async);
043        ScheduledExecutorService threadPool = ProcessorDefinitionHelper.getConfiguredScheduledExecutorService(routeContext, "Delay", definition, async);
044
045        Delayer answer = new Delayer(routeContext.getCamelContext(), childProcessor, delay, threadPool, shutdownThreadPool);
046        if (definition.getAsyncDelayed() != null) {
047            answer.setAsyncDelayed(definition.getAsyncDelayed());
048        }
049        if (definition.getCallerRunsWhenRejected() == null) {
050            // should be default true
051            answer.setCallerRunsWhenRejected(true);
052        } else {
053            answer.setCallerRunsWhenRejected(definition.getCallerRunsWhenRejected());
054        }
055        return answer;
056    }
057
058    private Expression createAbsoluteTimeDelayExpression(RouteContext routeContext) {
059        ExpressionDefinition expr = definition.getExpression();
060        if (expr != null) {
061            return expr.createExpression(routeContext);
062        }
063        return null;
064    }
065
066}