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.ExecutorService;
020
021import org.apache.camel.Predicate;
022import org.apache.camel.Processor;
023import org.apache.camel.model.OnCompletionDefinition;
024import org.apache.camel.model.OnCompletionMode;
025import org.apache.camel.model.ProcessorDefinition;
026import org.apache.camel.processor.CamelInternalProcessor;
027import org.apache.camel.processor.OnCompletionProcessor;
028import org.apache.camel.spi.RouteContext;
029
030public class OnCompletionReifier extends ProcessorReifier<OnCompletionDefinition> {
031
032    public OnCompletionReifier(RouteContext routeContext, ProcessorDefinition<?> definition) {
033        super(routeContext, (OnCompletionDefinition)definition);
034    }
035
036    @Override
037    public Processor createProcessor() throws Exception {
038        // assign whether this was a route scoped onCompletion or not
039        // we need to know this later when setting the parent, as only route
040        // scoped should have parent
041        // Note: this logic can possible be removed when the Camel routing
042        // engine decides at runtime
043        // to apply onCompletion in a more dynamic fashion than current code
044        // base
045        // and therefore is in a better position to decide among context/route
046        // scoped OnCompletion at runtime
047        Boolean routeScoped = definition.getRouteScoped();
048        if (routeScoped == null) {
049            routeScoped = definition.getParent() != null;
050        }
051
052        boolean isOnCompleteOnly = parseBoolean(definition.getOnCompleteOnly(), false);
053        boolean isOnFailureOnly = parseBoolean(definition.getOnFailureOnly(), false);
054        boolean isParallelProcessing = parseBoolean(definition.getParallelProcessing(), false);
055        boolean original = parseBoolean(definition.getUseOriginalMessage(), false);
056
057        if (isOnCompleteOnly && isOnFailureOnly) {
058            throw new IllegalArgumentException("Both onCompleteOnly and onFailureOnly cannot be true. Only one of them can be true. On node: " + this);
059        }
060        if (original) {
061            // ensure allow original is turned on
062            routeContext.setAllowUseOriginalMessage(true);
063        }
064
065        Processor childProcessor = this.createChildProcessor(true);
066
067        // wrap the on completion route in a unit of work processor
068        CamelInternalProcessor internal = new CamelInternalProcessor(camelContext, childProcessor);
069        internal.addAdvice(new CamelInternalProcessor.UnitOfWorkProcessorAdvice(routeContext, camelContext));
070
071        routeContext.setOnCompletion(getId(definition, routeContext), internal);
072
073        Predicate when = null;
074        if (definition.getOnWhen() != null) {
075            when = createPredicate(definition.getOnWhen().getExpression());
076        }
077
078        boolean shutdownThreadPool = willCreateNewThreadPool(definition, isParallelProcessing);
079        ExecutorService threadPool = getConfiguredExecutorService("OnCompletion", definition, isParallelProcessing);
080
081        // should be after consumer by default
082        boolean afterConsumer = definition.getMode() == null || parse(OnCompletionMode.class, definition.getMode()) == OnCompletionMode.AfterConsumer;
083
084        OnCompletionProcessor answer = new OnCompletionProcessor(camelContext, internal, threadPool, shutdownThreadPool, isOnCompleteOnly, isOnFailureOnly, when,
085                                                                 original, afterConsumer);
086        return answer;
087    }
088
089}