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.ComponentRepositoryException;
24  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
25  import org.codehaus.plexus.logging.Logger;
26  import org.codehaus.plexus.logging.LoggerManager;
27  import org.codehaus.plexus.logging.console.ConsoleLoggerManager;
28  
29  /** @author Jason van Zyl */
30  public class InitializeLoggerManagerPhase
31      extends AbstractCoreComponentInitializationPhase
32  {
33      public void initializeCoreComponent( ContainerInitializationContext context )
34          throws ContainerInitializationException
35      {
36          LoggerManager loggerManager = context.getContainer().getLoggerManager();
37  
38          // ----------------------------------------------------------------------
39          // The logger manager may have been set programmatically so we need
40          // to check. If it hasn't then we will try to look up a logger manager
41          // that may have been specified in the plexus.xml file. If that doesn't
42          // work then we'll programmatcially stuff in the console logger.
43          // ----------------------------------------------------------------------
44  
45          if ( loggerManager == null )
46          {
47              try
48              {
49                  loggerManager = context.getContainer().lookup( LoggerManager.class );
50              }
51              catch ( ComponentLookupException e )
52              {
53                  ComponentDescriptor cd = new ComponentDescriptor();
54  
55                  cd.setRole( LoggerManager.ROLE );
56  
57                  cd.setRoleHint( PlexusConstants.PLEXUS_DEFAULT_HINT );
58  
59                  cd.setImplementation( ConsoleLoggerManager.class.getName() );
60  
61                  try
62                  {
63                      context.getContainer().addComponentDescriptor( cd );
64                  }
65                  catch ( CycleDetectedInComponentGraphException cre )
66                  {
67                      throw new ContainerInitializationException( "Error setting up logging manager.", cre );
68                  }
69  
70                  loggerManager = new ConsoleLoggerManager( "info" );
71              }
72  
73              context.getContainer().setLoggerManager( loggerManager );
74          }
75  
76          Logger logger = loggerManager.getLoggerForComponent( PlexusContainer.class.getName() );
77  
78          context.getContainer().enableLogging( logger );
79      }
80  }