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