/*
 * Copyright 2005-2007 The Kuali Foundation
 * 
 * 
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.opensource.org/licenses/ecl2.php
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.rice.core.util;

import java.lang.reflect.InvocationTargetException;

/**
 * Provides some Utilities for handling common Exception scenarios.
 * 
 * @author Kuali Rice Team (rice.collab@kuali.org)
 */
public abstract class ExceptionUtils {

	/**
	 * Unwraps the appropriate Throwable from the given Throwable.  More specifically, this handles
	 * unwrapping causes from InvocationTargetException.
	 * @param throwable
	 * @return
	 */
	public static Throwable unwrapActualCause(Throwable throwable) {
		Throwable unwrappedThrowable = null;
		if (throwable instanceof InvocationTargetException) {
			InvocationTargetException ite = (InvocationTargetException)throwable;
			if (ite.getCause() != null && ite.getCause() != ite) {
				unwrappedThrowable = ite.getCause();
			}
		}
		if (unwrappedThrowable != null && unwrappedThrowable != throwable) {
			return unwrapActualCause(unwrappedThrowable);
		}
		return throwable;
	}
	
}

@@ -1565,10 +1565,714 @@
 e;%0A%09%7D%0A%09%0A
+%09/**%0A%09 * Searches the causes of the given Throwable to see if the given Throwable Class is among them;%0A%09 * if so, returns that Exception, otherwise returns null%0A%09 * %0A%09 * @param throwable the Throwable to search for causes in%0A%09 * @param soughtCause the Throwable Class to search for in the cause stack%0A%09 * @return the Throwable which is an instance of the sought for Throwable Class, or null if it could not be found%0A%09 */%0A%09public static Throwable searchForCause(Throwable throwable, Class%3C? extends Throwable%3E soughtCause) %7B%0A%09%09if (throwable.getClass().equals(soughtCause)) return throwable;%0A%09%09if (throwable.getCause() == null) return null;%0A%09%09return searchForCause(throwable.getCause(), soughtCause);%0A%09%7D%0A%09%0A
 %7D%0A

add ability to search for a specific exception class in a stack trace