001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.beans.factory.parsing;
018
019import org.apache.commons.logging.Log;
020import org.apache.commons.logging.LogFactory;
021
022/**
023 * Simple {@link ProblemReporter} implementation that exhibits fail-fast
024 * behavior when errors are encountered.
025 *
026 * <p>The first error encountered results in a {@link BeanDefinitionParsingException}
027 * being thrown.
028 *
029 * <p>Warnings are written to
030 * {@link #setLogger(org.apache.commons.logging.Log) the log} for this class.
031 *
032 * @author Rob Harrop
033 * @author Juergen Hoeller
034 * @author Rick Evans
035 * @since 2.0
036 */
037public class FailFastProblemReporter implements ProblemReporter {
038
039        private Log logger = LogFactory.getLog(getClass());
040
041
042        /**
043         * Set the {@link Log logger} that is to be used to report warnings.
044         * <p>If set to {@code null} then a default {@link Log logger} set to
045         * the name of the instance class will be used.
046         * @param logger the {@link Log logger} that is to be used to report warnings
047         */
048        public void setLogger(Log logger) {
049                this.logger = (logger != null ? logger : LogFactory.getLog(getClass()));
050        }
051
052
053        /**
054         * Throws a {@link BeanDefinitionParsingException} detailing the error
055         * that has occurred.
056         * @param problem the source of the error
057         */
058        @Override
059        public void fatal(Problem problem) {
060                throw new BeanDefinitionParsingException(problem);
061        }
062
063        /**
064         * Throws a {@link BeanDefinitionParsingException} detailing the error
065         * that has occurred.
066         * @param problem the source of the error
067         */
068        @Override
069        public void error(Problem problem) {
070                throw new BeanDefinitionParsingException(problem);
071        }
072
073        /**
074         * Writes the supplied {@link Problem} to the {@link Log} at {@code WARN} level.
075         * @param problem the source of the warning
076         */
077        @Override
078        public void warning(Problem problem) {
079                this.logger.warn(problem, problem.getRootCause());
080        }
081
082}