View Javadoc
1   package org.codehaus.plexus.container.initialization;
2   
3   /*
4    * Copyright 2001-2006 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.codehaus.plexus.PlexusConstants;
20  import org.codehaus.plexus.PlexusContainer;
21  import org.codehaus.plexus.component.composition.CycleDetectedInComponentGraphException;
22  import org.codehaus.plexus.component.repository.ComponentDescriptor;
23  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
24  import org.codehaus.plexus.logging.Logger;
25  import org.codehaus.plexus.logging.LoggerManager;
26  import org.codehaus.plexus.logging.console.ConsoleLoggerManager;
27  
28  /** @author Jason van Zyl */
29  public class InitializeLoggerManagerPhase
30      extends AbstractCoreComponentInitializationPhase
31  {
32      public void initializeCoreComponent( ContainerInitializationContext context )
33          throws ContainerInitializationException
34      {
35          LoggerManager loggerManager = context.getContainer().getLoggerManager();
36  
37          // ----------------------------------------------------------------------
38          // The logger manager may have been set programmatically so we need
39          // to check. If it hasn't then we will try to look up a logger manager
40          // that may have been specified in the plexus.xml file. If that doesn't
41          // work then we'll programmatcially stuff in the console logger.
42          // ----------------------------------------------------------------------
43  
44          if ( loggerManager == null )
45          {
46              try
47              {
48                  loggerManager = context.getContainer().lookup( LoggerManager.class );
49              }
50              catch ( ComponentLookupException e )
51              {
52                  ComponentDescriptor cd = new ComponentDescriptor();
53  
54                  cd.setRole( LoggerManager.ROLE );
55  
56                  cd.setRoleHint( PlexusConstants.PLEXUS_DEFAULT_HINT );
57  
58                  cd.setImplementation( ConsoleLoggerManager.class.getName() );
59  
60                  try
61                  {
62                      context.getContainer().addComponentDescriptor( cd );
63                  }
64                  catch ( CycleDetectedInComponentGraphException cre )
65                  {
66                      throw new ContainerInitializationException( "Error setting up logging manager.", cre );
67                  }
68  
69                  loggerManager = new ConsoleLoggerManager( "info" );
70              }
71  
72              context.getContainer().setLoggerManager( loggerManager );
73          }
74  
75          Logger logger = loggerManager.getLoggerForComponent( PlexusContainer.class.getName() );
76  
77          context.getContainer().enableLogging( logger );
78      }
79  }