1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.util;
15  
16  import java.lang.module.ModuleDescriptor;
17  import java.util.*;
18  
19  import ch.qos.logback.core.util.EnvUtil;
20  
21  /**
22   * @author Ceki Gülcü
23   */
24  public class ClassicEnvUtil {
25  
26      /*
27       * Used to replace the ClassLoader that the ServiceLoader uses for unit testing.
28       * We need this to mock the resources the ServiceLoader attempts to load from
29       * /META-INF/services thus keeping the projects src/test/resources clean (see
30       * src/test/resources/README.txt).
31       */
32      //static ClassLoader testServiceLoaderClassLoader = null;
33  
34      static public boolean isGroovyAvailable() {
35          return EnvUtil.isClassAvailable(ClassicEnvUtil.class, "groovy.lang.Binding");
36      }
37  //
38  //    private static ClassLoader getServiceLoaderClassLoader() {
39  //        return testServiceLoaderClassLoader == null ? Loader.getClassLoaderOfClass(ClassicEnvUtil.class)
40  //                : testServiceLoaderClassLoader;
41  //    }
42  
43      public static <T> List<T> loadFromServiceLoader(Class<T> c, ClassLoader classLoader) {
44          ServiceLoader<T> loader = ServiceLoader.load(c, classLoader);
45          List<T> listOfT = new ArrayList<>();
46          Iterator<T> it = loader.iterator();
47          while(it.hasNext()) {
48              T t = it.next();
49              listOfT.add(t);
50          }
51          return listOfT;
52      }
53  
54      /**
55       * <p>Returns the current version of logback-classic, or null if data is not
56       * available.
57       * </p>
58       *
59       * @since 1.5.15
60       * @return current version or null if missing version data
61       */
62      static public String getVersionOfLogbackClassic() {
63          String moduleVersion = getVersionOfLogbackClassicByModule();
64          if (moduleVersion != null)
65              return moduleVersion;
66  
67          Package pkg = ClassicEnvUtil.class.getPackage();
68          if (pkg == null) {
69              return null;
70          }
71          return pkg.getImplementationVersion();
72      }
73  
74      /**
75       * <p>Returns the current version of logback-classic via class.getModule() or null
76       * if data is not available.
77       * </p>
78       *
79       * @since 1.5.15
80       * @return current version or null if missing version data
81       */
82      static private String getVersionOfLogbackClassicByModule() {
83          Module module = ClassicEnvUtil.class.getModule();
84          if (module == null)
85              return null;
86  
87          ModuleDescriptor md = module.getDescriptor();
88          if (md == null)
89              return null;
90          Optional<String> opt = md.rawVersion();
91          return opt.orElse(null);
92      }
93  }