You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by mc...@apache.org on 2003/09/09 11:40:30 UTC

cvs commit: avalon-sandbox/merlin/kernel/bootstrap/src/java BootstrapHelper.java Merlin.java ExceptionHelper.java

mcconnell    2003/09/09 02:40:30

  Modified:    merlin/kernel/bootstrap/src/java Merlin.java
  Added:       merlin/kernel/bootstrap/src/java BootstrapHelper.java
  Removed:     merlin/kernel/bootstrap/src/java ExceptionHelper.java
  Log:
  Updaye to add verbose content into the bootstrap loader.
  
  Revision  Changes    Path
  1.10      +71 -8     avalon-sandbox/merlin/kernel/bootstrap/src/java/Merlin.java
  
  Index: Merlin.java
  ===================================================================
  RCS file: /home/cvs/avalon-sandbox/merlin/kernel/bootstrap/src/java/Merlin.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- Merlin.java	7 Sep 2003 11:34:14 -0000	1.9
  +++ Merlin.java	9 Sep 2003 09:40:30 -0000	1.10
  @@ -113,6 +113,8 @@
       */
       public static void main( String[] args ) throws Exception
       {
  +        boolean debug = isDebugEnabled( args );
  +
           //
           // the base directory is the directory containing the 
           // the merlin jar repository
  @@ -131,6 +133,11 @@
               base.mkdirs();
           }
   
  +        if( debug )
  +        {
  +            System.out.println( "MERLIN BOOTSTRAP REPOSITORY: " + base );
  +        }
  +
           //
           // Establish the repository.  If a system property corresponding to 
           // MERLIN_REPOSITORY_REMOTE_KEY is not null then setup the repository
  @@ -144,6 +151,11 @@
             System.getProperty( MERLIN_REPOSITORY_REMOTE_KEY );
           if( remote != null )
           {
  +            if( debug )
  +            {
  +                System.out.println( "MERLIN BOOTSTRAP REMOTE: " + remote );
  +            }
  +
               URL remoteURL = new URL( remote );
               repository = 
                 new DefaultFileRepository( base, null, new URL[]{ remoteURL } );
  @@ -168,13 +180,20 @@
           try
           {
               api = getURLs( repository, properties, MERLIN_API_CLASSPATH_KEY );
  +            if( debug )
  +            {
  +                for( int i=0; i<api.length; i++ )
  +                {
  +                    System.out.println( "API: (" + i + ") " + api[i] );
  +                }
  +            }
           }
           catch( Throwable e )
           {
               final String error =
                 "\nInternal error while attempting to build api classloader.";
               String msg = 
  -              ExceptionHelper.packException( error, e, true );
  +              BootstrapHelper.packException( error, e, true );
               System.err.println( msg );
               return;
           }
  @@ -183,13 +202,20 @@
           try
           {
               spi = getURLs( repository, properties, MERLIN_SPI_CLASSPATH_KEY );
  +            if( debug )
  +            {
  +                for( int i=0; i<spi.length; i++ )
  +                {
  +                    System.out.println( "SPI: (" + i + ") " + spi[i] );
  +                }
  +            }
           }
           catch( Throwable e )
           {
               final String error =
                 "\nInternal error while attempting to build api classloader.";
               String msg = 
  -              ExceptionHelper.packException( error, e, true );
  +              BootstrapHelper.packException( error, e, true );
               System.err.println( msg );
               return;
           }
  @@ -198,13 +224,20 @@
           try
           {
               impl = getURLs( repository, properties, MERLIN_IMPL_CLASSPATH_KEY );
  +            if( debug )
  +            {
  +                for( int i=0; i<impl.length; i++ )
  +                {
  +                    System.out.println( "IMP: (" + i + ") " + impl[i] );
  +                }
  +            }
           }
           catch( Throwable e )
           {
               final String error =
                 "\nInternal error while attempting to build implementation classloader.";
               String msg = 
  -              ExceptionHelper.packException( error, e, true );
  +              BootstrapHelper.packException( error, e, true );
               System.err.println( msg );
               return;
           }
  @@ -216,9 +249,15 @@
   
           ClassLoader apiLoader = new URLClassLoader( api );
           ClassLoader spiLoader = new URLClassLoader( spi, apiLoader );
  -        ClassLoader loader = new URLClassLoader( impl, spiLoader );
  +        URLClassLoader loader = new URLClassLoader( impl, spiLoader );
           Thread.currentThread().setContextClassLoader( loader );
   
  +        if( debug )
  +        {
  +            System.out.println( "CONTEXT CLASSLOADER DUMP" );
  +            printClassLoader( loader );
  +        }
  +
           //
           // get the bootstrap kernel loader class
           //
  @@ -236,7 +275,7 @@
               final String error =
                 "\nInternal error during loader class creation.";
               String msg = 
  -              ExceptionHelper.packException( error, e, true );
  +              BootstrapHelper.packException( error, e, true );
               System.err.println( msg );
               return;
           }
  @@ -268,7 +307,7 @@
                   final String error =
                     "\nInternal error during kernel instantiation.";
                   String msg = 
  -                  ExceptionHelper.packException( 
  +                  BootstrapHelper.packException( 
                       error, target, true );
                   System.err.println( msg );
               }
  @@ -279,7 +318,7 @@
               final String error =
                 "\nInternal error during kernel instantiation.";
               String msg = 
  -              ExceptionHelper.packException( error, e, true );
  +              BootstrapHelper.packException( error, e, true );
               System.err.println( msg );
               return;
           }
  @@ -292,8 +331,9 @@
           String label = getProperty( properties, key, i );
           while( label != null )
           {
  +            i++;
               list.add( getURL( repository, label ) );
  -            label = getProperty( properties, key, i++ );
  +            label = getProperty( properties, key, i );
           }
           return (URL[]) list.toArray( new URL[0] );
       }
  @@ -332,5 +372,28 @@
                 "Internal bootstrap error.  Unable to load item: " + item;
                throw new BootstrapRuntimeException( error, e );
            }
  +    }
  +
  +    private static boolean isDebugEnabled( final String[] args )
  +    {
  +        for( int i=0; i<args.length; i++ )
  +        {
  +            final String arg = args[i];
  +            if( arg.equals( "-debug" ) ) return true;
  +        }
  +        return false;
  +    }
  +
  +    private static void printClassLoader( URLClassLoader loader )
  +    {
  +        URL[] urls = loader.getURLs();
  +        for( int i=0; i<urls.length; i++ )
  +        {
  +            System.out.println( urls[i] );
  +        }
  +        if( loader.getParent() instanceof URLClassLoader )
  +        {
  +            printClassLoader( (URLClassLoader) loader.getParent() );
  +        }
       }
   }
  
  
  
  1.1                  avalon-sandbox/merlin/kernel/bootstrap/src/java/BootstrapHelper.java
  
  Index: BootstrapHelper.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/>.
   */
  
  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 BootstrapHelper
  {
      private static final String LINE_SEPARATOR = System.getProperty( "line.separator" );
  
      /**
       * 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 );
          if( e == null )
          {
              return buffer.toString();
          } else
          {
              buffer.append( "\n" );
              buffer.append( 
  "-------------------------------------------------------------------" );
              buffer.append( "\nException: " + e.getClass().getName() );
              buffer.append( "\nMessage: " + e.getMessage() );
              packCause( buffer, getCause( e ) ).toString();
          }
          Throwable root = getLastThrowable( e );
          if( (root != null) && stack )
          {
              buffer.append( 
  "\n\n---- stack trace --------------------------------------------------" );
              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( "\nMessage: " + 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 );
          }
      }
  }
  
  
  

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