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