| 1 | /* |
| 2 | * Copyright 2006-2007 the original author or authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.springframework.batch.support; |
| 17 | |
| 18 | import java.util.HashMap; |
| 19 | import java.util.Map; |
| 20 | |
| 21 | /** |
| 22 | * A {@link ExceptionClassifier} that has only two classes of exception. |
| 23 | * Provides convenient methods for setting up and querying the classification |
| 24 | * with boolean return type. |
| 25 | * |
| 26 | * @author Dave Syer |
| 27 | * |
| 28 | */ |
| 29 | public class BinaryExceptionClassifier extends ExceptionClassifierSupport { |
| 30 | |
| 31 | /** |
| 32 | * The classifier result for a non-default exception. |
| 33 | */ |
| 34 | public static final String NON_DEFAULT = "NON_DEFAULT"; |
| 35 | |
| 36 | private SubclassExceptionClassifier delegate = new SubclassExceptionClassifier(); |
| 37 | |
| 38 | /** |
| 39 | * Set the special exceptions. Any exception on the list, or subclasses |
| 40 | * thereof, will be classified as non-default. |
| 41 | * |
| 42 | * @param exceptionClasses defaults to {@link Exception}. |
| 43 | */ |
| 44 | public final void setExceptionClasses(Class[] exceptionClasses) { |
| 45 | Map temp = new HashMap(); |
| 46 | for (int i = 0; i < exceptionClasses.length; i++) { |
| 47 | temp.put(exceptionClasses[i], NON_DEFAULT); |
| 48 | } |
| 49 | this.delegate.setTypeMap(temp); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Convenience method to return boolean if the throwable is classified as |
| 54 | * default. |
| 55 | * |
| 56 | * @param throwable the Throwable to classify |
| 57 | * @return true if it is default classified (i.e. not on the list provided |
| 58 | * in {@link #setExceptionClasses(Class[])}. |
| 59 | */ |
| 60 | public boolean isDefault(Throwable throwable) { |
| 61 | return classify(throwable).equals(DEFAULT); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns either {@link ExceptionClassifierSupport#DEFAULT} or |
| 66 | * {@link #NON_DEFAULT} depending on the type of the throwable. If the type |
| 67 | * of the throwable or one of its ancestors is on the exception class list |
| 68 | * the classification is as {@link #NON_DEFAULT}. |
| 69 | * |
| 70 | * @see #setExceptionClasses(Class[]) |
| 71 | * @see ExceptionClassifierSupport#classify(Throwable) |
| 72 | */ |
| 73 | public Object classify(Throwable throwable) { |
| 74 | return delegate.classify(throwable); |
| 75 | } |
| 76 | |
| 77 | } |