001/*
002 * Copyright 2006-2013 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 */
016package org.springframework.batch.core.step.skip;
017
018import java.util.Map;
019
020import org.springframework.classify.Classifier;
021import org.springframework.classify.SubclassClassifier;
022
023/**
024 * A {@link SkipPolicy} that depends on an exception classifier to make its
025 * decision, and then delegates to the classifier result.
026 *
027 * @author Dave Syer
028 *
029 * @see SubclassClassifier
030 */
031public class ExceptionClassifierSkipPolicy implements SkipPolicy {
032
033        private SubclassClassifier<Throwable, SkipPolicy> classifier;
034
035        /**
036         * The classifier that will be used to choose a delegate policy.
037         *
038         * @param classifier the classifier to use to choose a delegate policy
039         */
040        public void setExceptionClassifier(SubclassClassifier<Throwable, SkipPolicy> classifier) {
041                this.classifier = classifier;
042        }
043
044        /**
045         * Setter for policy map. This property should not be changed dynamically -
046         * set it once, e.g. in configuration, and then don't change it during a
047         * running application. Either this property or the exception classifier
048         * directly should be set, but not both.
049         *
050         * @param policyMap a map of String to {@link SkipPolicy} that will be used
051         * to create a {@link Classifier} to locate a policy.
052         */
053        public void setPolicyMap(Map<Class<? extends Throwable>, SkipPolicy> policyMap) {
054                SubclassClassifier<Throwable, SkipPolicy> subclassClassifier = new SubclassClassifier<Throwable, SkipPolicy>(
055                                policyMap, new NeverSkipItemSkipPolicy());
056                this.classifier = subclassClassifier;
057        }
058
059        /**
060         * Consult the classifier and find a delegate policy, and then use that to
061         * determine the outcome.
062         *
063         * @param t the throwable to consider
064         * @param skipCount the current skip count
065         * @return true if the exception can be skipped
066         * @throws SkipLimitExceededException if a limit is exceeded
067         */
068        @Override
069        public boolean shouldSkip(Throwable t, int skipCount) throws SkipLimitExceededException {
070                return classifier.classify(t).shouldSkip(t, skipCount);
071        }
072
073}