avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mcconn...@apache.org
Subject cvs commit: avalon-sandbox/assembly-spi/src/java/org/apache/avalon/assembly/util ExceptionHelper.java package.html
Date Sat, 08 Mar 2003 02:37:11 GMT
mcconnell    2003/03/07 18:37:11

  Added:       assembly-spi/src/java/org/apache/avalon/assembly/util
                        ExceptionHelper.java package.html
  Log:
  Moved exception helper class from assembly into SPI.
  
  Revision  Changes    Path
  1.1                  avalon-sandbox/assembly-spi/src/java/org/apache/avalon/assembly/util/ExceptionHelper.java
  
  Index: ExceptionHelper.java
  ===================================================================
  /* ==================================================================== 
   * The Apache Software License, Version 1.1 
   * 
   * Copyright (c) 2002 The Apache Software Foundation. All rights 
   * reserved. 
   * 
   * Redistribution and use in source and binary forms, with or without 
   * modification, are permitted provided that the following conditions 
   * are met: 
   * 
   * 1. Redistributions of source code must retain the above copyright 
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *    "This product includes software developed by the
   *    Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software 
   *    itself, if and wherever such third-party acknowledgments  
   *    normally appear.
   *
   * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation" 
   *    must not be used to endorse or promote products derived from this
   *    software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation. For more
   * information on the Apache Software Foundation, please see 
   * <http://www.apache.org/>.
   */ 
  
  package org.apache.avalon.assembly.util;
  
  import java.io.PrintWriter;
  import java.io.StringWriter;
  import java.lang.reflect.Method;
  import java.util.StringTokenizer;
  
  /**
   * General utilities supporting the packaging of exception messages.
   * @author <a href="mailto:mcconnell@osm.net">Stephen McConnell</a>
   */
  public class ExceptionHelper
  {
      private static final String LINE_SEPARATOR = System.getProperty( "line.separator" );
  
      /**
       * Prints the exception to the standard error out together with
       * cause statements.
       * @param e the exception to print
       */
      public static void printException( Throwable e )
      {
          printException( null, e );
      }
  
      /**
       * Prints the exception to the standard error out together with
       * cause statements.
       * @param label label identifying the error
       * @param e the exception to print
       */
      public static void printException( String label, Throwable e )
      {
          printException( label, e, null );
      }
  
      /**
       * Prints the exception to the standard error out together with
       * source and cause statements.
       * @param label label identifying the error
       * @param e the exception to print
       * @param source the source of the request
       */
      public static void printException( String label, Throwable e, Object source )
      {
          printException( label, e, source, false );
      }
  
      /**
       * Prints the exception to the standard error out together with
       * source and cause statements.
       * @param label label identifying the error
       * @param e the exception to print
       * @param source the source of the request
       * @param trace boolean value inidication if a stack trace though be included
       */
      public static void printException( String label, Throwable e, Object source, boolean
trace )
      {
          if( e == null )
          {
              throw new NullPointerException( "throwable" );
          }
          java.io.PrintStream out = System.err;
          synchronized( out )
          {
              out.println( "==================================================================="
);
              if( label != null )
              {
                  out.println( "Message: " + label );
              }
              if( source != null )
              {
                  out.println( "Source: " + source );
              }
              out.println( "Exception: " + e.toString() );
              if( getCause( e ) != null )
              {
                  printCause( out, e );
              }
              out.println( "==================================================================="
);
              if( trace )
              {
                  Throwable root = getLastThrowable( e );
                  root.printStackTrace();
                  out.println( "==================================================================="
);
              }
          }
      }
  
      /**
       * Returns the exception and causal exceptions as a formatted string.
       * @param message the header message
       * @param e the exception
       * @return String the formatting string
       */
      public static String packException( final String message, final Throwable e )
      {
          return packException( message, e, true );
      }
  
      /**
       * Returns the exception and causal exceptions as a formatted string.
       * @param message the header message
       * @param e the exception
       * @return String the formatting string
       */
      public static String packException( final String message, final Throwable e, boolean
stack )
      {
          StringBuffer buffer = new StringBuffer();
          buffer.append( "Message: " + message );
          buffer.append( "\n==================================================================="
);
          if( e == null )
          {
              return buffer.toString();
          }
          else
          {
              buffer.append( "\n\nException: " + e.getClass().getName() );
              buffer.append( "\n" + e.getMessage() );
              packCause( buffer, getCause( e ) ).toString();
              buffer.append( "\n\n==================================================================="
);
          }
          Throwable root = getLastThrowable( e );
          if(( root != null ) && stack )
          {
              String[] trace = captureStackTrace( root );
              for( int i = 0; i < trace.length; i++ )
              {
                  buffer.append( "\n" + trace[ i ] );
              }
          }
          buffer.append( "\n==================================================================="
);
          return buffer.toString();
      }
  
      private static StringBuffer packCause( StringBuffer buffer, Throwable cause )
      {
          if( cause == null )
          {
              return buffer;
          }
          buffer.append( "\n\nCause: " + cause.getClass().getName() );
          buffer.append( "\n" + cause.getMessage() );
          return packCause( buffer, getCause( cause ) );
      }
  
      private static Throwable getLastThrowable( Throwable exception )
      {
          Throwable cause = getCause( exception );
          if( cause != null )
          {
              return getLastThrowable( cause );
          }
          return exception;
      }
  
      private static Throwable getCause( Throwable exception )
      {
          if( exception == null )
          {
              throw new NullPointerException( "exception" );
          }
  
          try
          {
              Method method = exception.getClass().getMethod( "getCause", new Class[ 0 ] );
              return (Throwable)method.invoke( exception, new Object[ 0 ] );
          }
          catch( Throwable e )
          {
              return null;
          }
      }
  
      /**
       * Captures the stack trace associated with this exception.
       *
       * @param throwable a <code>Throwable</code>
       * @return an array of Strings describing stack frames.
       */
      private static String[] captureStackTrace( final Throwable throwable )
      {
          final StringWriter sw = new StringWriter();
          throwable.printStackTrace( new PrintWriter( sw, true ) );
          return splitString( sw.toString(), LINE_SEPARATOR );
      }
  
      /**
       * Splits the string on every token into an array of stack frames.
       *
       * @param string the string to split
       * @param onToken the token to split on
       * @return the resultant array
       */
      private static String[] splitString( final String string, final String onToken )
      {
          final StringTokenizer tokenizer = new StringTokenizer( string, onToken );
          final String[] result = new String[ tokenizer.countTokens() ];
  
          for( int i = 0; i < result.length; i++ )
          {
              result[ i ] = tokenizer.nextToken();
          }
  
          return result;
      }
  
      private static void printCause( java.io.PrintStream out, Throwable e )
      {
          Throwable cause = getCause( e );
          out.println( "Cause: " + cause.toString() );
          if( getCause( cause ) != null )
          {
              printCause( out, cause );
          }
      }
  }
  
  
  
  1.1                  avalon-sandbox/assembly-spi/src/java/org/apache/avalon/assembly/util/package.html
  
  Index: package.html
  ===================================================================
  
  <body>
  <p>
  The <code>util</code> package contains a number of general component utilities.</p>
  </body>
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org


Mime
View raw message