001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 * <p>
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 * <p>
009 * or (per the licensee's choosing)
010 * <p>
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&uuml;lc&uuml;
023 */
024public class EnvUtil {
025
026    private EnvUtil() {
027    }
028
029
030    /**
031     * <p>Returns the current version of logback-core, or null if data is not
032     * available.
033     * </p>
034     *
035     * @since 1.3.0
036     * @return current version or null if missing version data
037     * @deprecated See {@link VersionUtil#getVersionOfArtifact(Class)}
038     */
039    static public String logbackVersion() {
040        return VersionUtil.getVersionOfArtifact(CoreConstants.class);
041    }
042
043    static public int getJDKVersion(String javaVersionStr) {
044        int version = 0;
045
046        for (char ch : javaVersionStr.toCharArray()) {
047            if (Character.isDigit(ch)) {
048                version = (version * 10) + (ch - 48);
049            } else if (version == 1) {
050                version = 0;
051            } else {
052                break;
053            }
054        }
055        return version;
056    }
057
058    static private boolean isJDK_N_OrHigher(int n) {
059        String javaVersionStr = System.getProperty("java.version", "");
060        if (javaVersionStr.isEmpty())
061            return false;
062
063        int version = getJDKVersion(javaVersionStr);
064        return version > 0 && n <= version;
065    }
066
067    static public boolean isJDK5() {
068        return isJDK_N_OrHigher(5);
069    }
070
071    static public boolean isJDK6OrHigher() {
072        return isJDK_N_OrHigher(6);
073    }
074
075    static public boolean isJDK7OrHigher() {
076        return isJDK_N_OrHigher(7);
077    }
078
079    static public boolean isJDK16OrHigher() {
080        return isJDK_N_OrHigher(16);
081    }
082
083    static public boolean isJDK18OrHigher() {
084        return isJDK_N_OrHigher(18);
085    }
086
087    /**
088     * @since logback 1.3.12/1.4.12
089     * @return true if runtime JDK is version 21 or higher
090     */
091    static public boolean isJDK21OrHigher() {
092        return isJDK_N_OrHigher(21);
093    }
094
095    static public boolean isJaninoAvailable() {
096        ClassLoader classLoader = EnvUtil.class.getClassLoader();
097        try {
098            Class<?> bindingClass = classLoader.loadClass("org.codehaus.janino.ScriptEvaluator");
099            return (bindingClass != null);
100        } catch (ClassNotFoundException e) {
101            return false;
102        }
103    }
104
105    public static boolean isMacOs() {
106        String os = System.getProperty("os.name");
107        // expected value is "Mac OS X"
108        return os.toLowerCase().contains("mac");
109    }
110
111    public static boolean isWindows() {
112        String os = System.getProperty("os.name");
113        return os.startsWith("Windows");
114    }
115
116    static public boolean isClassAvailable(Class callerClass, String className) {
117        ClassLoader classLoader = Loader.getClassLoaderOfClass(callerClass);
118        try {
119            Class<?> bindingClass = classLoader.loadClass(className);
120            return (bindingClass != null);
121        } catch (ClassNotFoundException e) {
122            return false;
123        }
124    }
125
126}