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