View Javadoc

1   package org.apache.maven.cli.logging;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.net.URL;
24  import java.util.Enumeration;
25  import java.util.Properties;
26  
27  import org.codehaus.plexus.util.PropertyUtils;
28  import org.slf4j.ILoggerFactory;
29  
30  /**
31   * Slf4jConfiguration factory, loading implementations from <code>META-INF/maven/slf4j-configuration.properties</code>
32   * configuration files in class loader.
33   *
34   * @author Hervé Boutemy
35   */
36  public class Slf4jConfigurationFactory
37  {
38      public static final String RESOURCE = "META-INF/maven/slf4j-configuration.properties";
39  
40      public static Slf4jConfiguration getConfiguration( ILoggerFactory loggerFactory )
41      {
42          try
43          {
44              Enumeration<URL> resources = Slf4jConfigurationFactory.class.getClassLoader().getResources( RESOURCE );
45  
46              String key = loggerFactory.getClass().getCanonicalName();
47  
48              while ( resources.hasMoreElements() )
49              {
50                  URL resource = resources.nextElement();
51  
52                  Properties conf = PropertyUtils.loadProperties( resource.openStream() );
53  
54                  String impl = conf.getProperty( key );
55  
56                  if ( impl != null )
57                  {
58                      return (Slf4jConfiguration) Class.forName( impl ).newInstance();
59                  }
60              }
61          }
62          catch ( IOException e )
63          {
64              e.printStackTrace();
65          }
66          catch ( InstantiationException e )
67          {
68              e.printStackTrace();
69          }
70          catch ( IllegalAccessException e )
71          {
72              e.printStackTrace();
73          }
74          catch ( ClassNotFoundException e )
75          {
76              e.printStackTrace();
77          }
78  
79          return new BaseSlf4jConfiguration();
80      }
81  }