001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.util;
015
016import java.util.ArrayList;
017import java.util.Iterator;
018import java.util.List;
019import java.util.ServiceLoader;
020
021import ch.qos.logback.core.util.EnvUtil;
022import ch.qos.logback.core.util.Loader;
023
024/**
025 * @author Ceki Gülcü
026 */
027public class ClassicEnvUtil {
028
029    /*
030     * Used to replace the ClassLoader that the ServiceLoader uses for unit testing.
031     * We need this to mock the resources the ServiceLoader attempts to load from
032     * /META-INF/services thus keeping the projects src/test/resources clean (see
033     * src/test/resources/README.txt).
034     */
035    //static ClassLoader testServiceLoaderClassLoader = null;
036
037    static public boolean isGroovyAvailable() {
038        return EnvUtil.isClassAvailable(ClassicEnvUtil.class, "groovy.lang.Binding");
039    }
040//
041//    private static ClassLoader getServiceLoaderClassLoader() {
042//        return testServiceLoaderClassLoader == null ? Loader.getClassLoaderOfClass(ClassicEnvUtil.class)
043//                : testServiceLoaderClassLoader;
044//    }
045
046    public static <T> List<T> loadFromServiceLoader(Class<T> c, ClassLoader classLoader) {
047        ServiceLoader<T> loader = ServiceLoader.load(c, classLoader);
048        List<T> listOfT = new ArrayList<>();
049        Iterator<T> it = loader.iterator();
050        while(it.hasNext()) {
051            T t = it.next();
052            listOfT.add(t);
053        }
054        return listOfT;
055    }
056
057}