dlr         01/08/09 15:06:56

  Modified:    util/src/java/org/apache/commons/util/exception
                        Nestable.java NestableDelegate.java
                        NestableException.java
                        NestableRuntimeException.java
  Log:
  * Incorporated patch by Kasper Nielsen to make the Nestable family of exceptions 
more closely emulate the interface of exceptions found in JDK 1.4.
  
  * Fix by myself for infinite recursion issue reported by Jon Stevens and Jason van 
Zyl.  I must have introduced this problem to these TurbineException decendants when I 
added NestableDelegate.
  
  Revision  Changes    Path
  1.3       +12 -7     
jakarta-commons-sandbox/util/src/java/org/apache/commons/util/exception/Nestable.java
  
  Index: Nestable.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/util/src/java/org/apache/commons/util/exception/Nestable.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -u -r1.2 -r1.3
  --- Nestable.java     2001/08/08 23:56:48     1.2
  +++ Nestable.java     2001/08/09 22:06:56     1.3
  @@ -62,6 +62,7 @@
    * inside themselves.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Daniel Rall</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Kasper Nielsen</a>
    */
   public interface Nestable
   {
  @@ -69,7 +70,7 @@
        * Returns the reference to the exception or error that caused the
        * exception implementing the <code>Nestable</code> to be thrown.
        */
  -    public Throwable getNested();
  +    public Throwable getCause();
   
       /**
        * Returns the error message of this and any nested
  @@ -81,18 +82,22 @@
   
       /**
        * Prints the stack trace of this exception to the specified print
  -     * writer.
  +     * writer.  Includes inforamation from the exception--if
  +     * any--which caused this exception.
        *
        * @param out <code>PrintWriter</code> to use for output.
        */
       public void printStackTrace(PrintWriter out);
   
       /**
  -     * Prints the stack trace of this exception skiping a specified number
  -     * of stack frames.
  +     * Prints the stack trace for this exception only--root cause not
  +     * included--using the provided writer.  Used by {@link
  +     * org.apache.commons.util.exception.NestableDelegate} to write
  +     * individual stack traces to a buffer.  The implementation of
  +     * this method should call
  +     * <code>super.printStackTrace(out);</code> in most cases.
        *
  -     * @param out  <code>PrintWriter</code> to use for output.
  -     * @param skip The numbere of stack frames to skip.
  +     * @param out The writer to use.
        */
  -    public void printStackTrace(PrintWriter out, int skip);
  +    public void printPartialStackTrace(PrintWriter out);
   }
  
  
  
  1.3       +43 -43    
jakarta-commons-sandbox/util/src/java/org/apache/commons/util/exception/NestableDelegate.java
  
  Index: NestableDelegate.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/util/src/java/org/apache/commons/util/exception/NestableDelegate.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -u -r1.2 -r1.3
  --- NestableDelegate.java     2001/08/08 23:56:48     1.2
  +++ NestableDelegate.java     2001/08/09 22:06:56     1.3
  @@ -65,6 +65,7 @@
   /**
    * @author <a href="mailto:[EMAIL PROTECTED]";>Rafal Krzewski</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Daniel Rall</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Kasper Nielsen</a>
    */
   public class NestableDelegate
   {
  @@ -79,17 +80,17 @@
        * Holds the reference to the exception or error that caused
        * this exception to be thrown.
        */
  -    private Nestable t = null;
  +    private Nestable cause = null;
   
       /**
  -     * @param t The Nestable implementation to get a stack trace for
  +     * @param cause The Nestable implementation to get a stack trace for
        * (<i>must</i> extend {@link java.lang.Throwable}).
        */
  -    public NestableDelegate(Nestable t)
  +    public NestableDelegate(Nestable cause)
       {
  -        if (t instanceof Throwable)
  +        if (cause instanceof Throwable)
           {
  -            this.t = t;
  +            this.cause = cause;
           }
           else
           {
  @@ -114,17 +115,17 @@
               msg.append(baseMsg);
           }
   
  -        Throwable nested = t.getNested();
  -        if (nested != null)
  +        Throwable nestedCause = cause.getCause();
  +        if (nestedCause != null)
           {
  -            String nestedMsg = nested.getMessage();
  -            if (nestedMsg != null)
  +            String causeMsg = nestedCause.getMessage();
  +            if (causeMsg != null)
               {
                   if (baseMsg != null)
                   {
                       msg.append(": ");
                   }
  -                msg.append(nestedMsg);
  +                msg.append(causeMsg);
               }
   
           }
  @@ -168,43 +169,31 @@
       {
           synchronized (out)
           {
  -            printStackTrace(out, 0);
  -        }
  -    }
  -
  -    /**
  -     * Prints the stack trace of the provided exception, skiping the
  -     * specified number of stack frames.
  -     *
  -     * @param out  <code>PrintWriter</code> to use for output.
  -     * @param skip The number of stack frames to skip.
  -     */
  -    public void printStackTrace(PrintWriter out, int skip)
  -    {
  -        // Retreive full stack trace.
  -        String[] st = decompose((Throwable) t);
  -        Throwable nested = t.getNested();
  -        if (nested != null)
  -        {
  -            if (nested instanceof Nestable)
  -            {
  -                ((Nestable) nested).printStackTrace(out, st.length - 2);
  -            }
  -            else
  +            String[] st = decompose((Throwable) cause);
  +            Throwable nestedCause = cause.getCause();
  +            if (nestedCause != null)
               {
  -                String[] nst = decompose(nested);
  -                for (int i = 0; i < nst.length - st.length + 2; i++)
  +                if (nestedCause instanceof Nestable)
  +                {
  +                    // Recurse until a non-Nestable is encountered.
  +                    ((Nestable) nestedCause).printStackTrace(out);
  +                }
  +                else
                   {
  -                    out.println(nst[i]);
  +                    String[] nst = decompose(nestedCause);
  +                    for (int i = 0; i < nst.length; i++)
  +                    {
  +                        out.println(nst[i]);
  +                    }
                   }
  +                out.print("rethrown as ");
               }
  -            out.print("rethrown as ");
  -        }
   
  -        // Output desired frames from stack trace.
  -        for (int i = 0; i < st.length - skip; i++)
  -        {
  -            out.println(st[i]);
  +            // Output desired frames from stack trace.
  +            for (int i = 0; i < st.length; i++)
  +            {
  +                out.println(st[i]);
  +            }
           }
       }
   
  @@ -218,7 +207,18 @@
       private String[] decompose(Throwable t)
       {
           StringWriter sw = new StringWriter();
  -        t.printStackTrace(new PrintWriter(sw, true));
  +        PrintWriter pw = new PrintWriter(sw, true);
  +
  +        // Avoid infinite loop between decompose() and printStackTrace().
  +        if (t instanceof Nestable)
  +        {
  +            ((Nestable) t).printPartialStackTrace(pw);
  +        }
  +        else
  +        {
  +            t.printStackTrace(pw);
  +        }
  +
           String linebreak = System.getProperty("line.separator");
           StringTokenizer st = new StringTokenizer(sw.getBuffer().toString(),
                                                    linebreak);
  
  
  
  1.3       +16 -15    
jakarta-commons-sandbox/util/src/java/org/apache/commons/util/exception/NestableException.java
  
  Index: NestableException.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/util/src/java/org/apache/commons/util/exception/NestableException.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -u -r1.2 -r1.3
  --- NestableException.java    2001/08/08 23:56:48     1.2
  +++ NestableException.java    2001/08/09 22:06:56     1.3
  @@ -123,6 +123,7 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Rafal Krzewski</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Daniel Rall</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Kasper Nielsen</a>
    */
   public class NestableException extends Exception implements Nestable
   {
  @@ -136,7 +137,7 @@
        * Holds the reference to the exception or error that caused
        * this exception to be thrown.
        */
  -    private Throwable nested = null;
  +    private Throwable cause = null;
   
       /**
        * Constructs a new <code>NestableException</code> without specified
  @@ -165,10 +166,10 @@
        * @param nested The exception or error that caused this exception
        *               to be thrown.
        */
  -    public NestableException(Throwable nested)
  +    public NestableException(Throwable cause)
       {
           super();
  -        this.nested = nested;
  +        this.cause = cause;
       }
   
       /**
  @@ -179,18 +180,18 @@
        * @param nested The exception or error that caused this exception
        *               to be thrown.
        */
  -    public NestableException(String msg, Throwable nested)
  +    public NestableException(String msg, Throwable cause)
       {
           super(msg);
  -        this.nested = nested;
  +        this.cause = cause;
       }
   
       /**
  -     * @see org.apache.commons.util.exception.Nestable#getNested()
  +     * @see org.apache.commons.util.exception.Nestable#getCause()
        */
  -    public Throwable getNested()
  +    public Throwable getCause()
       {
  -        return nested;
  +        return cause;
       }
   
       /**
  @@ -204,16 +205,16 @@
           {
               msg.append(ourMsg);
           }
  -        if (nested != null)
  +        if (cause != null)
           {
  -            String nestedMsg = nested.getMessage();
  -            if (nestedMsg != null)
  +            String causeMsg = cause.getMessage();
  +            if (causeMsg != null)
               {
                   if (ourMsg != null)
                   {
                       msg.append(": ");
                   }
  -                msg.append(nestedMsg);
  +                msg.append(causeMsg);
               }
   
           }
  @@ -248,10 +249,10 @@
       }
   
       /**
  -     * @see org.apache.commons.util.exception.Nestable#printStackTrace(PrintWriter 
out, int skip)
  +     * @see 
org.apache.commons.util.exception.Nestable#printPartialStackTrace(PrintWriter out)
        */
  -    public void printStackTrace(PrintWriter out, int skip)
  +    public final void printPartialStackTrace(PrintWriter out)
       {
  -        delegate.printStackTrace(out, skip);
  +        super.printStackTrace(out);
       }
   }
  
  
  
  1.3       +16 -15    
jakarta-commons-sandbox/util/src/java/org/apache/commons/util/exception/NestableRuntimeException.java
  
  Index: NestableRuntimeException.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/util/src/java/org/apache/commons/util/exception/NestableRuntimeException.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -u -r1.2 -r1.3
  --- NestableRuntimeException.java     2001/08/08 23:56:48     1.2
  +++ NestableRuntimeException.java     2001/08/09 22:06:56     1.3
  @@ -69,6 +69,7 @@
    * @see org.apache.commons.util.exception.NestableException
    * @author <a href="mailto:[EMAIL PROTECTED]";>Rafal Krzewski</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Daniel Rall</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Kasper Nielsen</a>
    */
   public class NestableRuntimeException extends RuntimeException
       implements Nestable
  @@ -83,7 +84,7 @@
        * Holds the reference to the exception or error that caused
        * this exception to be thrown.
        */
  -    private Throwable nested = null;
  +    private Throwable cause = null;
   
       /**
        * Constructs a new <code>NestableRuntimeException</code> without specified
  @@ -112,10 +113,10 @@
        * @param nested The exception or error that caused this exception
        *               to be thrown.
        */
  -    public NestableRuntimeException(Throwable nested)
  +    public NestableRuntimeException(Throwable cause)
       {
           super();
  -        this.nested = nested;
  +        this.cause = cause;
       }
   
       /**
  @@ -126,18 +127,18 @@
        * @param nested The exception or error that caused this exception
        *               to be thrown.
        */
  -    public NestableRuntimeException(String msg, Throwable nested)
  +    public NestableRuntimeException(String msg, Throwable cause)
       {
           super(msg);
  -        this.nested = nested;
  +        this.cause = cause;
       }
   
       /**
  -     * @see org.apache.commons.util.exception.Nestable#getNested()
  +     * @see org.apache.commons.util.exception.Nestable#getCause()
        */
  -    public Throwable getNested()
  +    public Throwable getCause()
       {
  -        return nested;
  +        return cause;
       }
   
       /**
  @@ -151,16 +152,16 @@
           {
               msg.append(ourMsg);
           }
  -        if (nested != null)
  +        if (cause != null)
           {
  -            String nestedMsg = nested.getMessage();
  -            if (nestedMsg != null)
  +            String causeMsg = cause.getMessage();
  +            if (causeMsg != null)
               {
                   if (ourMsg != null)
                   {
                       msg.append(": ");
                   }
  -                msg.append(nestedMsg);
  +                msg.append(causeMsg);
               }
   
           }
  @@ -195,10 +196,10 @@
       }
   
       /**
  -     * @see org.apache.commons.util.exception.Nestable#printStackTrace(PrintWriter 
out, int skip)
  +     * @see 
org.apache.commons.util.exception.Nestable#printPartialStackTrace(PrintWriter out)
        */
  -    public void printStackTrace(PrintWriter out, int skip)
  +    public final void printPartialStackTrace(PrintWriter out)
       {
  -        delegate.printStackTrace(out, skip);
  +        super.printStackTrace(out);
       }
   }
  
  
  

Reply via email to