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.LinkedHashMap;
26  import java.util.Map;
27  import java.util.Properties;
28  import java.util.Set;
29  
30  import org.apache.maven.cli.logging.impl.UnsupportedSlf4jBindingConfiguration;
31  import org.codehaus.plexus.util.PropertyUtils;
32  import org.slf4j.ILoggerFactory;
33  
34  /**
35   * Slf4jConfiguration factory, loading implementations from <code>META-INF/maven/slf4j-configuration.properties</code>
36   * configuration files in class loader: key is the class name of the ILoggerFactory, value is the class name of
37   * the corresponding Slf4jConfiguration.
38   *
39   * @author Hervé Boutemy
40   * @since 3.1.0
41   */
42  public class Slf4jConfigurationFactory
43  {
44      public static final String RESOURCE = "META-INF/maven/slf4j-configuration.properties";
45  
46      public static Slf4jConfiguration getConfiguration( ILoggerFactory loggerFactory )
47      {
48          Map<URL, Set<Object>> supported = new LinkedHashMap<URL, Set<Object>>();
49  
50          String slf4jBinding = loggerFactory.getClass().getCanonicalName();
51  
52          try
53          {
54              Enumeration<URL> resources = Slf4jConfigurationFactory.class.getClassLoader().getResources( RESOURCE );
55  
56              while ( resources.hasMoreElements() )
57              {
58                  URL resource = resources.nextElement();
59  
60                  Properties conf = PropertyUtils.loadProperties( resource.openStream() );
61  
62                  String impl = conf.getProperty( slf4jBinding );
63  
64                  if ( impl != null )
65                  {
66                      return (Slf4jConfiguration) Class.forName( impl ).newInstance();
67                  }
68  
69                  supported.put( resource, conf.keySet() );
70              }
71          }
72          catch ( IOException e )
73          {
74              e.printStackTrace();
75          }
76          catch ( InstantiationException e )
77          {
78              e.printStackTrace();
79          }
80          catch ( IllegalAccessException e )
81          {
82              e.printStackTrace();
83          }
84          catch ( ClassNotFoundException e )
85          {
86              e.printStackTrace();
87          }
88  
89          return new UnsupportedSlf4jBindingConfiguration( slf4jBinding, supported );
90      }
91  }