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.lang.module.ModuleDescriptor;
017import java.util.*;
018
019import ch.qos.logback.classic.ClassicConstants;
020import ch.qos.logback.core.util.EnvUtil;
021import ch.qos.logback.core.util.VersionUtil;
022
023/**
024 * @author Ceki Gülcü
025 */
026public class ClassicEnvUtil {
027
028    /*
029     * Used to replace the ClassLoader that the ServiceLoader uses for unit testing.
030     * We need this to mock the resources the ServiceLoader attempts to load from
031     * /META-INF/services thus keeping the projects src/test/resources clean (see
032     * src/test/resources/README.txt).
033     */
034    //static ClassLoader testServiceLoaderClassLoader = null;
035
036    static public boolean isGroovyAvailable() {
037        return EnvUtil.isClassAvailable(ClassicEnvUtil.class, "groovy.lang.Binding");
038    }
039//
040//    private static ClassLoader getServiceLoaderClassLoader() {
041//        return testServiceLoaderClassLoader == null ? Loader.getClassLoaderOfClass(ClassicEnvUtil.class)
042//                : testServiceLoaderClassLoader;
043//    }
044
045    public static <T> List<T> loadFromServiceLoader(Class<T> c, ClassLoader classLoader) {
046        ServiceLoader<T> loader = ServiceLoader.load(c, classLoader);
047        List<T> listOfT = new ArrayList<>();
048        Iterator<T> it = loader.iterator();
049        while(it.hasNext()) {
050            T t = it.next();
051            listOfT.add(t);
052        }
053        return listOfT;
054    }
055
056    /**
057     * <p>Returns the current version of logback-classic, or null if data is not
058     * available.
059     * </p>
060     *
061     * @since 1.5.15
062     * @return current version or null if missing version data
063     * @deprecated See {@link ch.qos.logback.core.util.VersionUtil#getVersionOfArtifact(Class)}
064     */
065    @Deprecated
066    static public String getVersionOfLogbackClassic() {
067        try {
068            return VersionUtil.getVersionOfArtifact(ClassicConstants.class);
069        } catch (NoClassDefFoundError e) {
070            return null;
071        }
072    }
073
074}