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