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.errorhandler;
018
019import java.util.concurrent.ScheduledExecutorService;
020
021import org.apache.camel.ErrorHandlerFactory;
022import org.apache.camel.Processor;
023import org.apache.camel.Route;
024import org.apache.camel.builder.DefaultErrorHandlerBuilder;
025import org.apache.camel.processor.errorhandler.DefaultErrorHandler;
026import org.apache.camel.processor.errorhandler.ExceptionPolicyStrategy;
027import org.apache.camel.spi.ExecutorServiceManager;
028import org.apache.camel.spi.ThreadPoolProfile;
029
030public class DefaultErrorHandlerReifier<T extends DefaultErrorHandlerBuilder> extends ErrorHandlerReifier<T> {
031
032    public DefaultErrorHandlerReifier(Route route, ErrorHandlerFactory definition) {
033        super(route, (T)definition);
034    }
035
036    @Override
037    public Processor createErrorHandler(Processor processor) throws Exception {
038        DefaultErrorHandler answer = new DefaultErrorHandler(camelContext, processor, definition.getLogger(),
039                                                             getBean(Processor.class, definition.getOnRedelivery(), definition.getOnRedeliveryRef()),
040                                                             definition.getRedeliveryPolicy(),
041                                                             getBean(ExceptionPolicyStrategy.class, definition.getExceptionPolicyStrategy(), definition.getExceptionPolicyStrategyRef()),
042                                                             getPredicate(definition.getRetryWhile(), definition.getRetryWhileRef()),
043                                                             getExecutorService(definition.getExecutorService(), definition.getExecutorServiceRef()),
044                                                             getBean(Processor.class, definition.getOnPrepareFailure(), definition.getOnPrepareFailureRef()),
045                                                             getBean(Processor.class, definition.getOnExceptionOccurred(), definition.getOnExceptionOccurredRef()));
046        // configure error handler before we can use it
047        configure(answer);
048        return answer;
049    }
050
051    protected synchronized ScheduledExecutorService getExecutorService(ScheduledExecutorService executorService, String executorServiceRef) {
052        if (executorService == null || executorService.isShutdown()) {
053            // camel context will shutdown the executor when it shutdown so no
054            // need to shut it down when stopping
055            if (executorServiceRef != null) {
056                executorService = lookup(executorServiceRef, ScheduledExecutorService.class);
057                if (executorService == null) {
058                    ExecutorServiceManager manager = camelContext.getExecutorServiceManager();
059                    ThreadPoolProfile profile = manager.getThreadPoolProfile(executorServiceRef);
060                    executorService = manager.newScheduledThreadPool(this, executorServiceRef, profile);
061                }
062                if (executorService == null) {
063                    throw new IllegalArgumentException("ExecutorServiceRef " + executorServiceRef + " not found in registry.");
064                }
065            } else {
066                // no explicit configured thread pool, so leave it up to the
067                // error handler to decide if it need
068                // a default thread pool from
069                // CamelContext#getErrorHandlerExecutorService
070                executorService = null;
071            }
072        }
073        return executorService;
074    }
075
076}