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