/*
 * Copyright 2008 the original author or authors.
 *
 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
 *
 * 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.rioproject.resources.util;

import com.sun.jini.constants.ThrowableConstants;
import org.rioproject.deploy.ServiceBeanInstantiationException;

/**
 * Utility for getting things from a Throwable
 *
 * @author Dennis Reedy
 */
@SuppressWarnings("PMD.AvoidThrowingRawExceptionTypes")
public class ThrowableUtil {
    public static Throwable getRootCause(Throwable e) {
        if(e instanceof ServiceBeanInstantiationException) {
            if(((ServiceBeanInstantiationException)e).getCauseExceptionDescriptor()!=null) {
                ServiceBeanInstantiationException.ExceptionDescriptor exDesc =
                    ((ServiceBeanInstantiationException)e).getCauseExceptionDescriptor();

                if(exDesc.getCauses().size()>0) {
                    exDesc = exDesc.getCauses().get(0);
                }
                Throwable t = new Throwable(exDesc.getMessage());
                t.setStackTrace(exDesc.getStacktrace());
                return t;
            }
        }
        Throwable cause = e;
        Throwable t = cause;
        while(t != null) {
            t = cause.getCause();
            if(t != null)
                cause = t;
        }
        return (cause);
    }

    public static boolean isRetryable(Throwable t) {
        boolean retryable = true;
        final int category = ThrowableConstants.retryable(t);
        Throwable cause = getRootCause(t);
        if (category == ThrowableConstants.BAD_INVOCATION ||
            category == ThrowableConstants.BAD_OBJECT ||
            cause instanceof java.net.ConnectException) {
            retryable = false;
        }
        return retryable;
    }
}

@@ -764,16 +764,69 @@
 ption;%0A%0A
+import java.lang.reflect.InvocationTargetException;%0A%0A
 /**%0A * U
@@ -1044,24 +1044,220 @@
 ) %7B%0A        
+Throwable cause = e;%0A        if(e instanceof InvocationTargetException) %7B%0A            cause = e.getCause()==null? ((InvocationTargetException)e).getTargetException(): e.getCause();%0A        %7D else 
 if(e instanc
@@ -1860,38 +1860,20 @@
    %7D
-%0A        Throwable cause = e;%0A
+ else %7B%0A    
     
@@ -1905,16 +1905,20 @@
         
+    
 while(t 
@@ -1920,32 +1920,36 @@
 le(t != null) %7B%0A
+    
             t = 
@@ -1978,16 +1978,20 @@
         
+    
 if(t != 
@@ -2004,32 +2004,36 @@
                 
+   
+ 
 cause = t;%0A     
@@ -2023,24 +2023,38 @@
  cause = t;%0A
+            %7D%0A
         %7D%0A  

Add support for interpreting an InvocationTargetException