mcconnell    2002/07/09 03:59:36

  Modified:    assembly/src/java/org/apache/excalibur/merlin/kernel
                        DefaultContainer.java DefaultKernel.java Main.java
                        Resources.properties
  Added:       assembly/src/java/org/apache/excalibur/merlin/kernel
                        DefaultLoggerManager.java KernelException.java
                        KernelRuntimeException.java
  Log:
  enhanced logging support
  
  Revision  Changes    Path
  1.10      +6 -3      
jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/merlin/kernel/DefaultContainer.java
  
  Index: DefaultContainer.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/merlin/kernel/DefaultContainer.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- DefaultContainer.java     8 Jul 2002 09:57:36 -0000       1.9
  +++ DefaultContainer.java     9 Jul 2002 10:59:35 -0000       1.10
  @@ -208,7 +208,9 @@
           // before starting up any of the nested containers
           //
   
  -        getLogger().debug("startup");
  +        if( getLogger().isDebugEnabled() )
  +          getLogger().debug("startup");
  +
           start();
           Iterator iterator = m_containers.iterator();
           while( iterator.hasNext() )
  @@ -225,7 +227,8 @@
           // the components in this container 
           //
   
  -        getLogger().debug("shutdown");
  +        if( getLogger().isDebugEnabled() )
  +          getLogger().debug("shutdown");
           Iterator iterator = m_containers.iterator();
           while( iterator.hasNext() )
           {
  
  
  
  1.9       +54 -15    
jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/merlin/kernel/DefaultKernel.java
  
  Index: DefaultKernel.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/merlin/kernel/DefaultKernel.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- DefaultKernel.java        8 Jul 2002 09:57:36 -0000       1.8
  +++ DefaultKernel.java        9 Jul 2002 10:59:35 -0000       1.9
  @@ -57,16 +57,13 @@
   import org.apache.avalon.excalibur.extension.Extension;
   import org.apache.avalon.excalibur.extension.OptionalPackage;
   import org.apache.avalon.excalibur.extension.DefaultPackageRepository;
  +import org.apache.avalon.excalibur.logger.LoggerManager;
   import org.apache.excalibur.meta.info.DefaultType;
   import org.apache.excalibur.meta.info.Type;
   import org.apache.excalibur.meta.info.ServiceDescriptor;
   import org.apache.excalibur.meta.info.DependencyDescriptor;
   import org.apache.excalibur.meta.info.ServiceDesignator;
   import org.apache.excalibur.meta.data.Profile;
  -import org.apache.log.Hierarchy;
  -import org.apache.log.Priority;
  -import org.apache.log.output.io.StreamTarget;
  -
   
   /**
    * Default kernel implementation.
  @@ -88,6 +85,8 @@
   
       private boolean m_verified = false;
   
  +    private LoggerManager m_manager;
  +
       //=======================================================================
       // Configurable
       //=======================================================================
  @@ -99,7 +98,6 @@
       public void configure( Configuration config)
       {
           m_config = config;
  -        getLogger().debug("kernel configuration");
       }
   
       //=======================================================================
  @@ -108,6 +106,10 @@
   
       public void initialize() throws Exception
       {
  +        m_manager = createLoggingManager();
  +        Logger logger = m_manager.getLoggerForCategory("kernel") ;
  +        enableLogging( logger );
  +
           final ContainerClassLoader loader = new ContainerClassLoader(
             new DefaultPackageRepository( 
               Fileset.expandExtensions( 
  @@ -116,16 +118,26 @@
             ),
             Thread.currentThread().getContextClassLoader(), 
             m_config.getChild("container").getChild("classpath"),
  -          getLogger().getChildLogger( m_config.getName())
  +          getLogger().getChildLogger( "loader" )
           );
   
  -        DefaultContext context = new DefaultContext();
  -        context.put( DefaultContainer.CLASSLOADER_KEY, loader );
  -        m_container.enableLogging( getLogger().getChildLogger("container") );
  -        m_container.contextualize( context );
  -        m_container.configure( m_config.getChild("container" ) );
  -        m_container.initialize( );
  -        m_container.verify();
  +        try
  +        {
  +            DefaultContext context = new DefaultContext();
  +            context.put( DefaultContainer.CLASSLOADER_KEY, loader );
  +            m_container.enableLogging( getLogger().getChildLogger("container") );
  +            m_container.contextualize( context );
  +            m_container.configure( m_config.getChild("container" ) );
  +            m_container.initialize( );
  +            m_container.verify();
  +        }
  +        catch( Throwable e )
  +        {
  +            final String error = "Kernel initialization failure.";
  +            if( getLogger() != null )
  +              getLogger().error( error, e );
  +            throw new KernelException( error, e );
  +        }
       }
   
       //=======================================================================
  @@ -135,12 +147,39 @@
       public void startup() throws Exception
       {
           //listProfiles();
  -        m_container.startup();
  +        if( getLogger().isInfoEnabled() )
  +          getLogger().info("startup");
  +
  +        try
  +        {
  +            m_container.startup();
  +        }
  +        catch( Throwable e )
  +        {
  +            final String error = "Kernel startup failure.";
  +            getLogger().error( error, e );
  +            throw new KernelException( error, e );
  +        }
       }
   
       public void shutdown()
       {
  +        if( getLogger().isInfoEnabled() )
  +          getLogger().info("shutdown");
           m_container.shutdown();
       }
   
  +    //=======================================================================
  +    // internals
  +    //=======================================================================
  +
  +    private LoggerManager createLoggingManager() throws Exception
  +    {
  +        final File home = new File( System.getProperty( "user.dir" ) );
  +        final Configuration config = m_config.getChild( "logging" );
  +        final DefaultLoggerManager manager = new DefaultLoggerManager( 
  +           home, config );
  +        manager.initialize( );
  +        return manager;
  +    }
   }
  
  
  
  1.6       +8 -41     
jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/merlin/kernel/Main.java
  
  Index: Main.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/merlin/kernel/Main.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Main.java 7 Jul 2002 23:17:07 -0000       1.5
  +++ Main.java 9 Jul 2002 10:59:35 -0000       1.6
  @@ -77,11 +77,6 @@
       // static
       //=======================================================================
   
  -    private static final String DEFAULT_FORMAT =
  -        "[%7.7{priority}] (%{category}): %{message}\\n%{throwable}";
  -    private static final Resources REZ =
  -        ResourceManager.getPackageResources( Main.class );
  -
      /**
       * Creation of a root type registry.
       */
  @@ -121,60 +116,32 @@
               throw new RuntimeException("Missing kernel configuration path 
argument.");
           }
   
  -        // create a bootstrap logger - this needs to be replaced with
  -        // the Avalon Exvalibur Logkit package
  +        // run it up
   
  -        final Logger logger = getLogger( config ); 
  -        DefaultContext context = new DefaultContext();
           try
           {
  -            kernel.enableLogging( logger );
               kernel.configure( config );
               kernel.initialize( );
           }
  -        catch( Throwable e )
  +        catch( KernelException e )
           {
  -            logger.error("Unexpected error while processing kernel lifecycle.", e);
               System.exit(0);
           }
  -
  -        // invoke the registry demo
  -        try
  -        {
  -            kernel.startup();
  -        }
           catch( Throwable e )
           {
  -            logger.error("Kernel startup failure.", e );
  +            System.out.println("Unexpected error while initilizing kernel.");
  +            e.printStackTrace();
               System.exit(0);
           }
  -    }
   
  -    private static Logger getLogger( Configuration config )
  -    {
  -        // create an internal logger for the registry
  -        final Hierarchy hierarchy = createHierarchy(
  -           Priority.getPriorityForName( 
  -              config.getChild("logger").getAttribute("priority","INFO") 
  -           )
  -        );
  -        return new LogKitLogger( hierarchy.getLoggerFor( "" ) );
  -    }
  -
  -    private static Hierarchy createHierarchy( Priority priority )
  -    {
  +        // invoke the registry demo
           try
           {
  -            Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
  -            hierarchy.setDefaultLogTarget(
  -                new StreamTarget( System.out, new AvalonFormatter( DEFAULT_FORMAT ) 
) );
  -            hierarchy.setDefaultPriority( priority );
  -            return hierarchy;
  +            kernel.startup();
           }
           catch( Throwable e )
           {
  -            final String error = "Unexpected exception while creating bootstrap 
logger.";
  -            throw new CascadingRuntimeException( error, e );
  +            System.exit(0);
           }
       }
   
  
  
  
  1.6       +3 -0      
jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/merlin/kernel/Resources.properties
  
  Index: Resources.properties
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/merlin/kernel/Resources.properties,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Resources.properties      8 Jul 2002 09:57:36 -0000       1.5
  +++ Resources.properties      9 Jul 2002 10:59:35 -0000       1.6
  @@ -27,3 +27,6 @@
   resource.missing-parameters.error=Missing Parameters object for component named 
"{0}".
   resource.missing-parameters.error=Missing Configuration for component named "{0}".
   
  +target.nocreate=Error creating LogTarget named "{0}" for file {0}. (Reason: {2}).
  +unknown-priority=Unknown priority "{0}" for Logger named "{1}".
  +category-create=Creating a log category named "{0}" that writes to "{1}" target at 
priority "{2}".
  
  
  
  1.1                  
jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/merlin/kernel/DefaultLoggerManager.java
  
  Index: DefaultLoggerManager.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.excalibur.merlin.kernel;
  
  import java.io.File;
  import java.io.IOException;
  import java.util.HashMap;
  import org.apache.avalon.excalibur.i18n.ResourceManager;
  import org.apache.avalon.excalibur.i18n.Resources;
  import org.apache.avalon.excalibur.logger.LoggerManager;
  import org.apache.avalon.framework.activity.Initializable;
  import org.apache.avalon.framework.configuration.Configurable;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.context.Context;
  import org.apache.avalon.framework.context.ContextException;
  import org.apache.avalon.framework.context.Contextualizable;
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.apache.avalon.framework.logger.AvalonFormatter;
  import org.apache.avalon.framework.logger.LogKitLogger;
  import org.apache.log.Hierarchy;
  import org.apache.log.LogTarget;
  import org.apache.log.Logger;
  import org.apache.log.Priority;
  import org.apache.log.output.io.FileTarget;
  import org.apache.log.output.io.StreamTarget;
  
  /**
   * A <code>LoggerManager</code> that supports a verifiable logging hierarchy 
creation directive.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Stephen McConnell</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Eung-ju Park</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Peter Donald</a>
   */
  class DefaultLoggerManager
      implements LoggerManager, Initializable
  {
      public static final String HOME_KEY = "app.home";
  
      private static final Resources REZ =
          ResourceManager.getPackageResources( DefaultLoggerManager.class );
  
      private static final String DEFAULT_FORMAT =
        "[%7.7{priority}] (%{category}): %{message}\\n%{throwable}";
  
     /**
      * Base directory of file targets.
      */
      private File m_baseDirectory;
  
     /**
      * log hierarchy.
      */
      private Hierarchy m_logHierarchy = new Hierarchy();
  
     /**
      * default logger
      */
      private org.apache.avalon.framework.logger.Logger m_logger;
  
     /**
      * default logger target
      */
      private StreamTarget m_stream;
  
     /**
      * the default priority if undefined
      */
      private String m_defaultPriority = "INFO";
  
     /**
      * the supplied configuration
      */
      private Configuration m_config;
  
      //===============================================================
      // constructor
      //===============================================================
  
     /**
      * Creation of a new LoggerManager.
      * @param base the base directory against which file targets are 
      *   logicaly created
      * @param config the logging configuration
      */
      public DefaultLoggerManager( File base, Configuration config )
      {
          m_baseDirectory = base;
          m_config = config;
      }
  
      //===============================================================
      // Initializable
      //===============================================================
  
     /**
      * Initialization of the logging manager.
      * @exception Exception if an error furing the initialization process occurs
      */
      public void initialize( ) throws Exception
      {
          m_defaultPriority = m_config.getAttribute("priority", "INFO" );
          Priority priority = Priority.getPriorityForName( 
              m_defaultPriority
          );
          m_stream = new StreamTarget( System.out, new AvalonFormatter( DEFAULT_FORMAT 
) );
          getHierarchy().setDefaultLogTarget( m_stream );
          getHierarchy().setDefaultPriority( priority );
  
          final Configuration[] targets = m_config.getChildren( "target" );
          final HashMap targetSet = configureTargets( targets );
          final Configuration[] categories = m_config.getChildren( "category" );
          configureCategories( categories, targetSet );
      }
  
      //===============================================================
      // LoggerManager
      //===============================================================
  
      /**
       * Return the Logger for the specified category.
       */
      public org.apache.avalon.framework.logger.Logger getLoggerForCategory( final 
String category )
      {
          return new LogKitLogger( getHierarchy().getLoggerFor( category ) );
      }
  
      /**
       * Return the default Logger.  This is the same
       * as getting the Logger for the "" category.
       */
      public org.apache.avalon.framework.logger.Logger getDefaultLogger( )
      {
          if( m_logger == null )
            m_logger = getLoggerForCategory("");
          return m_logger;
      }
  
      //===============================================================
      // internals
      //===============================================================
  
      private Hierarchy getHierarchy()
      {
          return m_logHierarchy;
      }
  
      /**
       * Configure a set of logtargets based on config data.
       *
       * @param targets the target configuration data
       * @return a Map of target-name to target
       * @throws ConfigurationException if an error occurs
       */
      private HashMap configureTargets( final Configuration[] targets )
          throws ConfigurationException
      {
  
          final HashMap targetSet = new HashMap();
  
          //  
          // Create the default target.
          //
  
          targetSet.put( "default", m_stream );
  
          for( int i = 0; i < targets.length; i++ )
          {
              final Configuration target = targets[ i ];
              final String name = target.getAttribute( "name" );
              final Configuration fileDef = target.getChild( "file", false );
              if( fileDef != null )
              {
                  String location = fileDef.getAttribute( "location" ).trim();
                  final String format = fileDef.getAttribute( "format", DEFAULT_FORMAT 
);
                  if( '/' == location.charAt( 0 ) )
                      location = location.substring( 1 );
  
                  final AvalonFormatter formatter = new AvalonFormatter( format );
  
                  //Specify output location for logging
                  final File file = new File( m_baseDirectory, location );
  
                  //Setup logtarget
                  FileTarget logTarget = null;
                  try
                  {
                      logTarget = new FileTarget( file.getAbsoluteFile(), false, 
formatter );
                  }
                  catch( final IOException ioe )
                  {
                      final String message =
                          REZ.getString( "target.nocreate", name, file, 
ioe.getMessage() );
                      throw new ConfigurationException( message, ioe );
                  }
  
                  targetSet.put( name, logTarget );
              }
              else
              {
                  final String error = 
                    "Unrecognized elements declaration in the logging target named: " 
+ name;
                  throw new IllegalArgumentException( error );
              }
          }
  
          return targetSet;
      }
  
      /**
       * Configure Logging categories.
       *
       * @param categories configuration data for categories
       * @param targets a hashmap containing the already existing taregt
       * @throws ConfigurationException if an error occurs
       */
      private void configureCategories( final Configuration[] categories, final 
HashMap targets )
          throws ConfigurationException
      {
          for( int i = 0; i < categories.length; i++ )
          {
              final Configuration category = categories[ i ];
              final String name = category.getAttribute( "name", "" );
              final String target = category.getAttribute( "target" , "default" );
              final String priorityName = category.getAttribute( "priority", 
m_defaultPriority );
  
              final Logger logger = getHierarchy().getLoggerFor( name );
  
              final LogTarget logTarget = (LogTarget)targets.get( target );
  
              final Priority priority = Priority.getPriorityForName( priorityName );
              if( !priority.getName().equals( priorityName ) )
              {
                  final String message = REZ.getString( "unknown-priority", 
priorityName, name );
                  throw new ConfigurationException( message );
              }
  
              logger.setPriority( priority );
              if( logTarget != null )
                logger.setLogTargets( new LogTarget[]{logTarget} );
  
              if( getDefaultLogger().isDebugEnabled() )
              {
                  final String message =
                      REZ.getString( "category-create", name, target, priorityName );
                  getDefaultLogger().debug( message );
              }
          }
      }
  
  }
  
  
  
  1.1                  
jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/merlin/kernel/KernelException.java
  
  Index: KernelException.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.excalibur.merlin.kernel;
  
  import org.apache.avalon.framework.CascadingException;
  
  /**
   * Exception to indicate that there was a kernel related error.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Stephen McConnell</a>
   * @version $Revision: 1.1 $ $Date: 2002/07/09 10:59:35 $
   */
  public final class KernelException
      extends CascadingException
  {
  
      /**
       * Construct a new <code>KernelException</code> instance.
       *
       * @param message The detail message for this exception.
       */
      public KernelException( final String message )
      {
          this( message, null );
      }
  
      /**
       * Construct a new <code>KernelException</code> instance.
       *
       * @param message The detail message for this exception.
       * @param throwable the root cause of the exception
       */
      public KernelException( final String message, final Throwable throwable )
      {
          super( message, throwable );
      }
  }
  
  
  
  
  1.1                  
jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/merlin/kernel/KernelRuntimeException.java
  
  Index: KernelRuntimeException.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.excalibur.merlin.kernel;
  
  import org.apache.avalon.framework.CascadingRuntimeException;
  
  /**
   * Exception to indicate that there was a kernel related runtime error.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Stephen McConnell</a>
   * @version $Revision: 1.1 $ $Date: 2002/07/09 10:59:35 $
   */
  public final class KernelRuntimeException
      extends CascadingRuntimeException
  {
  
      /**
       * Construct a new <code>KernelRuntimeException</code> instance.
       *
       * @param message The detail message for this exception.
       */
      public KernelRuntimeException( final String message )
      {
          this( message, null );
      }
  
      /**
       * Construct a new <code>KernelRuntimeException</code> instance.
       *
       * @param message The detail message for this exception.
       * @param throwable the root cause of the exception
       */
      public KernelRuntimeException( final String message, final Throwable throwable )
      {
          super( message, throwable );
      }
  }
  
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to