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     */
017    package org.apache.camel.impl;
018    
019    import org.apache.camel.Consumer;
020    import org.apache.camel.Exchange;
021    import org.apache.camel.Route;
022    import org.apache.camel.spi.ExceptionHandler;
023    import org.apache.camel.spi.RoutePolicy;
024    import org.apache.camel.util.ServiceHelper;
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    
028    /**
029     * A base class for developing custom {@link RoutePolicy} implementations.
030     *
031     * @version $Revision: 16089 $
032     */
033    public abstract class RoutePolicySupport extends ServiceSupport implements RoutePolicy {
034    
035        protected final transient Log log = LogFactory.getLog(getClass());
036        private ExceptionHandler exceptionHandler;
037    
038        public void onExchangeBegin(Route route, Exchange exchange) {
039        }
040    
041        public void onExchangeDone(Route route, Exchange exchange) {
042        }
043    
044        protected boolean startConsumer(Consumer consumer) throws Exception {
045            boolean resumed = ServiceHelper.resumeService(consumer);
046            if (resumed && log.isDebugEnabled()) {
047                log.debug("Resuming consumer " + consumer);
048            }
049            return resumed;
050        }
051    
052        protected boolean stopConsumer(Consumer consumer) throws Exception {
053            boolean suspended = ServiceHelper.suspendService(consumer);
054            if (suspended && log.isDebugEnabled()) {
055                log.debug("Suspended consumer " + consumer);
056            }
057            return suspended;
058        }
059    
060        /**
061         * Handles the given exception using the {@link #getExceptionHandler()}
062         *
063         * @param t the exception to handle
064         */
065        protected void handleException(Throwable t) {
066            getExceptionHandler().handleException(t);
067        }
068    
069        @Override
070        protected void doStart() throws Exception {
071        }
072    
073        @Override
074        protected void doStop() throws Exception {
075        }
076    
077        public ExceptionHandler getExceptionHandler() {
078            if (exceptionHandler == null) {
079                exceptionHandler = new LoggingExceptionHandler(getClass());
080            }
081            return exceptionHandler;
082        }
083    
084        public void setExceptionHandler(ExceptionHandler exceptionHandler) {
085            this.exceptionHandler = exceptionHandler;
086        }
087    
088    }