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  import org.apache.maven.cli.logging.impl.UnsupportedSlf4jBindingConfiguration;
29  import org.codehaus.plexus.util.PropertyUtils;
30  import org.slf4j.ILoggerFactory;
31  
32  
33  
34  
35  
36  
37  
38  
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  }