View Javadoc
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.util.ArrayList;
17  import java.util.Iterator;
18  import java.util.List;
19  import java.util.ServiceLoader;
20  
21  import ch.qos.logback.core.util.EnvUtil;
22  import ch.qos.logback.core.util.Loader;
23  
24  /**
25   * @author Ceki Gülcü
26   */
27  public class ClassicEnvUtil {
28  
29      /*
30       * Used to replace the ClassLoader that the ServiceLoader uses for unit testing.
31       * We need this to mock the resources the ServiceLoader attempts to load from
32       * /META-INF/services thus keeping the projects src/test/resources clean (see
33       * src/test/resources/README.txt).
34       */
35      //static ClassLoader testServiceLoaderClassLoader = null;
36  
37      static public boolean isGroovyAvailable() {
38          return EnvUtil.isClassAvailable(ClassicEnvUtil.class, "groovy.lang.Binding");
39      }
40  //
41  //    private static ClassLoader getServiceLoaderClassLoader() {
42  //        return testServiceLoaderClassLoader == null ? Loader.getClassLoaderOfClass(ClassicEnvUtil.class)
43  //                : testServiceLoaderClassLoader;
44  //    }
45  
46      public static <T> List<T> loadFromServiceLoader(Class<T> c, ClassLoader classLoader) {
47          ServiceLoader<T> loader = ServiceLoader.load(c, classLoader);
48          List<T> listOfT = new ArrayList<>();
49          Iterator<T> it = loader.iterator();
50          while(it.hasNext()) {
51              T t = it.next();
52              listOfT.add(t);
53          }
54          return listOfT;
55      }
56  
57  }