一、前言
关于LogBack中logback-core源码包中ch.qos.logback.core.util.EnvUtil环境工具类,对是否windows操作系统环境判断isWindows、jdk版本的判断isJDK5/isJDK6OrHigher/isJDK7OrHigher等。
二、源码说明
package ch.qos.logback.core.util;@b@ @b@public class EnvUtil {@b@@b@ private EnvUtil() {@b@ }@b@@b@ static public int getJDKVersion(String javaVersionStr) {@b@ int version = 0;@b@@b@ for (char ch : javaVersionStr.toCharArray()) {@b@ if (Character.isDigit(ch)) {@b@ version = (version * 10) + (ch - 48);@b@ } else if (version == 1) {@b@ version = 0;@b@ } else {@b@ break;@b@ }@b@ }@b@ return version;@b@ }@b@@b@ static private boolean isJDK_N_OrHigher(int n) {@b@ String javaVersionStr = System.getProperty("java.version", "");@b@ if (javaVersionStr.isEmpty())@b@ return false;@b@@b@ int version = getJDKVersion(javaVersionStr);@b@ return version > 0 && n <= version;@b@ }@b@@b@ static public boolean isJDK5() {@b@ return isJDK_N_OrHigher(5);@b@ }@b@@b@ static public boolean isJDK6OrHigher() {@b@ return isJDK_N_OrHigher(6);@b@ }@b@@b@ static public boolean isJDK7OrHigher() {@b@ return isJDK_N_OrHigher(7);@b@ }@b@@b@ static public boolean isJaninoAvailable() {@b@ ClassLoader classLoader = EnvUtil.class.getClassLoader();@b@ try {@b@ Class<?> bindingClass = classLoader.loadClass("org.codehaus.janino.ScriptEvaluator");@b@ return (bindingClass != null);@b@ } catch (ClassNotFoundException e) {@b@ return false;@b@ }@b@ }@b@@b@ public static boolean isWindows() {@b@ String os = System.getProperty("os.name");@b@ return os.startsWith("Windows");@b@ }@b@@b@}