1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
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  
35  
36  
37  
38  
39  
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  }