001/*
002 * Copyright 2002-2018 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
022import org.springframework.lang.Nullable;
023
024/**
025 * Simple {@link ProblemReporter} implementation that exhibits fail-fast
026 * behavior when errors are encountered.
027 *
028 * <p>The first error encountered results in a {@link BeanDefinitionParsingException}
029 * being thrown.
030 *
031 * <p>Warnings are written to
032 * {@link #setLogger(org.apache.commons.logging.Log) the log} for this class.
033 *
034 * @author Rob Harrop
035 * @author Juergen Hoeller
036 * @author Rick Evans
037 * @since 2.0
038 */
039public class FailFastProblemReporter implements ProblemReporter {
040
041        private Log logger = LogFactory.getLog(getClass());
042
043
044        /**
045         * Set the {@link Log logger} that is to be used to report warnings.
046         * <p>If set to {@code null} then a default {@link Log logger} set to
047         * the name of the instance class will be used.
048         * @param logger the {@link Log logger} that is to be used to report warnings
049         */
050        public void setLogger(@Nullable Log logger) {
051                this.logger = (logger != null ? logger : LogFactory.getLog(getClass()));
052        }
053
054
055        /**
056         * Throws a {@link BeanDefinitionParsingException} detailing the error
057         * that has occurred.
058         * @param problem the source of the error
059         */
060        @Override
061        public void fatal(Problem problem) {
062                throw new BeanDefinitionParsingException(problem);
063        }
064
065        /**
066         * Throws a {@link BeanDefinitionParsingException} detailing the error
067         * that has occurred.
068         * @param problem the source of the error
069         */
070        @Override
071        public void error(Problem problem) {
072                throw new BeanDefinitionParsingException(problem);
073        }
074
075        /**
076         * Writes the supplied {@link Problem} to the {@link Log} at {@code WARN} level.
077         * @param problem the source of the warning
078         */
079        @Override
080        public void warning(Problem problem) {
081                logger.warn(problem, problem.getRootCause());
082        }
083
084}