001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2026, 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 v2.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    public static <T> List<T> loadFromServiceLoader(Class<T> c, ClassLoader classLoader) {
041        ServiceLoader<T> loader = ServiceLoader.load(c, classLoader);
042        List<T> listOfT = new ArrayList<>();
043        Iterator<T> it = loader.iterator();
044        while(it.hasNext()) {
045            T t = it.next();
046            listOfT.add(t);
047        }
048        return listOfT;
049    }
050
051    /**
052     * <p>Returns the current version of logback-classic, or null if data is not
053     * available.
054     * </p>
055     *
056     * TODO: remove in 1.6
057     *
058     * @since 1.5.15
059     * @return current version or null if missing version data
060     * @deprecated See {@link ch.qos.logback.classic.util.ClassicVersionUtil#getVersionBySelfDeclaredProperties()}
061     *
062     */
063    @Deprecated
064    static public String getVersionOfLogbackClassic() {
065        try {
066            return VersionUtil.getVersionOfArtifact(ClassicConstants.class);
067        } catch (NoClassDefFoundError e) {
068            return null;
069        }
070    }
071
072}