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.Route;
024import org.apache.camel.model.OnCompletionDefinition;
025import org.apache.camel.model.OnCompletionMode;
026import org.apache.camel.model.ProcessorDefinition;
027import org.apache.camel.processor.CamelInternalProcessor;
028import org.apache.camel.processor.OnCompletionProcessor;
029
030public class OnCompletionReifier extends ProcessorReifier<OnCompletionDefinition> {
031
032    public OnCompletionReifier(Route route, ProcessorDefinition<?> definition) {
033        super(route, (OnCompletionDefinition)definition);
034    }
035
036    @Override
037    public Processor createProcessor() throws Exception {
038        boolean isOnCompleteOnly = parseBoolean(definition.getOnCompleteOnly(), false);
039        boolean isOnFailureOnly = parseBoolean(definition.getOnFailureOnly(), false);
040        boolean isParallelProcessing = parseBoolean(definition.getParallelProcessing(), false);
041        boolean original = parseBoolean(definition.getUseOriginalMessage(), false);
042
043        if (isOnCompleteOnly && isOnFailureOnly) {
044            throw new IllegalArgumentException("Both onCompleteOnly and onFailureOnly cannot be true. Only one of them can be true. On node: " + this);
045        }
046        if (original) {
047            // ensure allow original is turned on
048            route.setAllowUseOriginalMessage(true);
049        }
050
051        Processor childProcessor = this.createChildProcessor(true);
052
053        // wrap the on completion route in a unit of work processor
054        CamelInternalProcessor internal = new CamelInternalProcessor(camelContext, childProcessor);
055        internal.addAdvice(new CamelInternalProcessor.UnitOfWorkProcessorAdvice(route, camelContext));
056
057        route.setOnCompletion(getId(definition), internal);
058
059        Predicate when = null;
060        if (definition.getOnWhen() != null) {
061            when = createPredicate(definition.getOnWhen().getExpression());
062        }
063
064        boolean shutdownThreadPool = willCreateNewThreadPool(definition, isParallelProcessing);
065        ExecutorService threadPool = getConfiguredExecutorService("OnCompletion", definition, isParallelProcessing);
066
067        // should be after consumer by default
068        boolean afterConsumer = definition.getMode() == null || parse(OnCompletionMode.class, definition.getMode()) == OnCompletionMode.AfterConsumer;
069
070        OnCompletionProcessor answer = new OnCompletionProcessor(camelContext, internal, threadPool, shutdownThreadPool, isOnCompleteOnly, isOnFailureOnly, when,
071                                                                 original, afterConsumer);
072        return answer;
073    }
074
075}