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 java.lang.module.ModuleDescriptor;
017import java.util.Optional;
018
019/**
020 * @author Ceki G&uuml;lc&uuml;
021 */
022public class EnvUtil {
023
024    private EnvUtil() {
025    }
026
027    /**
028     * <p>Returns the current version of logback, or null if data is not
029     * available.
030     * </p>
031     *
032     * @since 1.3.0
033     * @return current version or null if missing version data
034     */
035    static public String logbackVersion() {
036        String moduleVersion = logbackVersionByModule();
037        if(moduleVersion != null)
038            return moduleVersion;
039
040        Package pkg = EnvUtil.class.getPackage();
041        if (pkg == null) {
042            return null;
043        }
044        return pkg.getImplementationVersion();
045    }
046
047    /**
048     * <p>Returns the current version of logback via class.getModule() or null if data is not
049     * available.
050     * </p>
051     *
052     * @since 1.3.0
053     * @return current version or null if missing version data
054     */
055    static private String logbackVersionByModule() {
056        Module module = EnvUtil.class.getModule();
057        if (module == null)
058            return null;
059
060        ModuleDescriptor md = module.getDescriptor();
061        if (md == null)
062            return null;
063        Optional<String> opt = md.rawVersion();
064        return opt.orElse(null);
065    }
066
067    static public int getJDKVersion(String javaVersionStr) {
068        int version = 0;
069
070        for (char ch : javaVersionStr.toCharArray()) {
071            if (Character.isDigit(ch)) {
072                version = (version * 10) + (ch - 48);
073            } else if (version == 1) {
074                version = 0;
075            } else {
076                break;
077            }
078        }
079        return version;
080    }
081
082    static private boolean isJDK_N_OrHigher(int n) {
083        String javaVersionStr = System.getProperty("java.version", "");
084        if (javaVersionStr.isEmpty())
085            return false;
086
087        int version = getJDKVersion(javaVersionStr);
088        return version > 0 && n <= version;
089    }
090
091    static public boolean isJDK5() {
092        return isJDK_N_OrHigher(5);
093    }
094
095    static public boolean isJDK6OrHigher() {
096        return isJDK_N_OrHigher(6);
097    }
098
099    static public boolean isJDK7OrHigher() {
100        return isJDK_N_OrHigher(7);
101    }
102
103    static public boolean isJDK16OrHigher() {
104        return isJDK_N_OrHigher(16);
105    }
106
107    static public boolean isJDK18OrHigher() {
108        return isJDK_N_OrHigher(18);
109    }
110
111    /**
112     * @since logback 1.3.12/1.4.12
113     * @return true if runtime JDK is version 21 or higher
114     */
115    static public boolean isJDK21OrHigher() {
116        return isJDK_N_OrHigher(21);
117    }
118
119    static public boolean isJaninoAvailable() {
120        ClassLoader classLoader = EnvUtil.class.getClassLoader();
121        try {
122            Class<?> bindingClass = classLoader.loadClass("org.codehaus.janino.ScriptEvaluator");
123            return (bindingClass != null);
124        } catch (ClassNotFoundException e) {
125            return false;
126        }
127    }
128
129    public static boolean isWindows() {
130        String os = System.getProperty("os.name");
131        return os.startsWith("Windows");
132    }
133
134    static public boolean isClassAvailable(Class callerClass, String className) {
135        ClassLoader classLoader = Loader.getClassLoaderOfClass(callerClass);
136        try {
137            Class<?> bindingClass = classLoader.loadClass(className);
138            return (bindingClass != null);
139        } catch (ClassNotFoundException e) {
140            return false;
141        }
142    }
143
144}