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: key is the class name of the ILoggerFactory, value is the class name of
33   * the corresponding Slf4jConfiguration.
34   *
35   * @author Hervé Boutemy
36   * @since 3.1.0
37   */
38  public class Slf4jConfigurationFactory
39  {
40      public static final String RESOURCE = "META-INF/maven/slf4j-configuration.properties";
41  
42      public static Slf4jConfiguration getConfiguration( ILoggerFactory loggerFactory )
43      {
44          try
45          {
46              Enumeration<URL> resources = Slf4jConfigurationFactory.class.getClassLoader().getResources( RESOURCE );
47  
48              String key = loggerFactory.getClass().getCanonicalName();
49  
50              while ( resources.hasMoreElements() )
51              {
52                  URL resource = resources.nextElement();
53  
54                  Properties conf = PropertyUtils.loadProperties( resource.openStream() );
55  
56                  String impl = conf.getProperty( key );
57  
58                  if ( impl != null )
59                  {
60                      return (Slf4jConfiguration) Class.forName( impl ).newInstance();
61                  }
62              }
63          }
64          catch ( IOException e )
65          {
66              e.printStackTrace();
67          }
68          catch ( InstantiationException e )
69          {
70              e.printStackTrace();
71          }
72          catch ( IllegalAccessException e )
73          {
74              e.printStackTrace();
75          }
76          catch ( ClassNotFoundException e )
77          {
78              e.printStackTrace();
79          }
80  
81          return new BaseSlf4jConfiguration();
82      }
83  }