1 /*
2 * $Id: ExceptionUtils.java 19229 2010-08-26 20:50:55Z tcarlson $
3 * --------------------------------------------------------------------------------------
4 * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
5 *
6 * The software in this package is published under the terms of the CPAL v1.0
7 * license, a copy of which has been included with this distribution in the
8 * LICENSE.txt file.
9 */
10
11 package org.mule.util;
12
13 import java.util.List;
14 import java.util.ListIterator;
15
16 /**
17 * Mule exception utilities.
18 */
19 public class ExceptionUtils extends org.apache.commons.lang.exception.ExceptionUtils
20 {
21
22 /**
23 * This method returns true if the throwable contains a {@link Throwable} that
24 * matches the specified class or subclass in the exception chain. Subclasses of
25 * the specified class do match.
26 *
27 * @param throwable the throwable to inspect, may be null
28 * @param type the type to search for, subclasses match, null returns false
29 * @return the index into the throwable chain, false if no match or null input
30 */
31 public static boolean containsType(Throwable throwable, Class<?> type)
32 {
33 return indexOfType(throwable, type) > -1;
34 }
35
36 /**
37 * This method returns the throwable closest to the root cause that matches the
38 * specified class or subclass. Any null argument will make the method return
39 * null.
40 *
41 * @param throwable the throwable to inspect, may be null
42 * @param type the type to search for, subclasses match, null returns null
43 * @return the throwablethat is closest to the root in the throwable chain that
44 * matches the type or subclass of that type.
45 */
46 public static Throwable getDeepestOccurenceOfType(Throwable throwable, Class<?> type)
47 {
48 if (throwable == null || type == null)
49 {
50 return null;
51 }
52 @SuppressWarnings("unchecked")
53 List<Throwable> throwableList = getThrowableList(throwable);
54 ListIterator<Throwable> listIterator = throwableList.listIterator(throwableList.size());
55 while (listIterator.hasPrevious())
56 {
57 Throwable candidate = listIterator.previous();
58 if (type.isAssignableFrom(candidate.getClass()))
59 {
60 return candidate;
61 }
62 }
63 return null;
64 }
65 }