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.CamelExchangeException;
020    import org.apache.camel.Exchange;
021    import org.apache.camel.LoggingLevel;
022    import org.apache.camel.RollbackExchangeException;
023    import org.apache.camel.processor.Logger;
024    import org.apache.camel.spi.ExceptionHandler;
025    import org.apache.camel.util.ExchangeHelper;
026    import org.apache.commons.logging.LogFactory;
027    
028    /**
029     * A default implementation of {@link ExceptionHandler} which uses a {@link Logger} to
030     * log to an arbitrary {@link org.apache.commons.logging.Log Log} with some {@link LoggingLevel}
031     *
032     * @version $Revision: 891253 $
033     */
034    public class LoggingExceptionHandler implements ExceptionHandler {
035        private final Logger logger;
036    
037        public LoggingExceptionHandler(Class<?> ownerType) {
038            this(new Logger(LogFactory.getLog(ownerType), LoggingLevel.ERROR));
039        }
040    
041        public LoggingExceptionHandler(Logger logger) {
042            this.logger = logger;
043        }
044    
045        public void handleException(Throwable exception) {
046            handleException(null, null, exception);
047        }
048    
049        public void handleException(String message, Throwable exception) {
050            handleException(message, null, exception);
051        }
052    
053        public void handleException(String message, Exchange exchange, Throwable exception) {
054            String msg = ExchangeHelper.createExceptionMessage(message, exchange, exception);
055            if (isCausedByRollbackExchangeException(exception)) {
056                // do not log stacktrace for intended rollbacks
057                logger.log(msg);
058            } else {
059                logger.log(msg, exception);
060            }
061        }
062    
063        protected boolean isCausedByRollbackExchangeException(Throwable exception) {
064            if (exception instanceof RollbackExchangeException) {
065                return true;
066            } else if (exception.getCause() != null) {
067                // recursive children
068                return isCausedByRollbackExchangeException(exception.getCause());
069            }
070    
071            return false;
072        }
073    }