View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.cli.logging;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.net.URL;
24  import java.util.Enumeration;
25  import java.util.Properties;
26  
27  import org.apache.maven.cli.logging.impl.UnsupportedSlf4jBindingConfiguration;
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   * @since 3.1.0
36   */
37  public class Slf4jConfigurationFactory {
38      public static final String RESOURCE = "META-INF/maven/slf4j-configuration.properties";
39  
40      public static Slf4jConfiguration getConfiguration(ILoggerFactory loggerFactory) {
41          String slf4jBinding = loggerFactory.getClass().getCanonicalName();
42  
43          try {
44              Enumeration<URL> resources =
45                      Slf4jConfigurationFactory.class.getClassLoader().getResources(RESOURCE);
46  
47              while (resources.hasMoreElements()) {
48                  URL resource = resources.nextElement();
49                  try {
50                      InputStream is = resource.openStream();
51                      final Properties properties = new Properties();
52                      if (is != null) {
53                          try (InputStream in = is) {
54                              properties.load(in);
55                          }
56                      }
57                      String impl = properties.getProperty(slf4jBinding);
58                      if (impl != null) {
59                          return (Slf4jConfiguration) Class.forName(impl).newInstance();
60                      }
61                  } catch (IOException | ClassNotFoundException | IllegalAccessException | InstantiationException ex) {
62                      // ignore and move on to the next
63                  }
64              }
65          } catch (IOException ex) {
66              // ignore
67          }
68  
69          return new UnsupportedSlf4jBindingConfiguration();
70      }
71  }