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.core.util;
015
016import ch.qos.logback.core.CoreConstants;
017
018import java.lang.module.ModuleDescriptor;
019import java.util.Optional;
020
021/**
022 * @author Ceki Gülcü
023 */
024public class EnvUtil {
025
026    private EnvUtil() {
027    }
028
029    static public int getJDKVersion(String javaVersionStr) {
030        int version = 0;
031
032        for (char ch : javaVersionStr.toCharArray()) {
033            if (Character.isDigit(ch)) {
034                version = (version * 10) + (ch - 48);
035            } else if (version == 1) {
036                version = 0;
037            } else {
038                break;
039            }
040        }
041        return version;
042    }
043
044    static private boolean isJDK_N_OrHigher(int n) {
045        String javaVersionStr = System.getProperty("java.version", "");
046        if (javaVersionStr.isEmpty())
047            return false;
048
049        int version = getJDKVersion(javaVersionStr);
050        return version > 0 && n <= version;
051    }
052
053    static public boolean isJDK5() {
054        return isJDK_N_OrHigher(5);
055    }
056
057    static public boolean isJDK6OrHigher() {
058        return isJDK_N_OrHigher(6);
059    }
060
061    static public boolean isJDK7OrHigher() {
062        return isJDK_N_OrHigher(7);
063    }
064
065    static public boolean isJDK16OrHigher() {
066        return isJDK_N_OrHigher(16);
067    }
068
069    static public boolean isJDK18OrHigher() {
070        return isJDK_N_OrHigher(18);
071    }
072
073    /**
074     * @since logback 1.3.12/1.4.12
075     * @return true if runtime JDK is version 21 or higher
076     */
077    static public boolean isJDK21OrHigher() {
078        return isJDK_N_OrHigher(21);
079    }
080
081    static public boolean isJaninoAvailable() {
082        ClassLoader classLoader = EnvUtil.class.getClassLoader();
083        try {
084            Class<?> bindingClass = classLoader.loadClass("org.codehaus.janino.ScriptEvaluator");
085            return (bindingClass != null);
086        } catch (ClassNotFoundException e) {
087            return false;
088        }
089    }
090
091    public static boolean isMacOs() {
092        String os = System.getProperty("os.name");
093        // expected value is "Mac OS X"
094        return os.toLowerCase().contains("mac");
095    }
096
097    public static boolean isWindows() {
098        String os = System.getProperty("os.name");
099        return os.startsWith("Windows");
100    }
101
102    static public boolean isClassAvailable(Class callerClass, String className) {
103        ClassLoader classLoader = Loader.getClassLoaderOfClass(callerClass);
104        try {
105            Class<?> bindingClass = classLoader.loadClass(className);
106            return (bindingClass != null);
107        } catch (ClassNotFoundException e) {
108            return false;
109        }
110    }
111
112}