1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    * <p>
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    * <p>
9    * or (per the licensee's choosing)
10   * <p>
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.util;
15  
16  import java.lang.module.ModuleDescriptor;
17  import java.util.Optional;
18  
19  /**
20   * @author Ceki G&uuml;lc&uuml;
21   */
22  public class EnvUtil {
23  
24      private EnvUtil() {
25      }
26  
27  
28      /**
29       * <p>Returns the current version of logback, or null if data is not
30       * available.
31       * </p>
32       *
33       * @since 1.3.0
34       * @return current version or null if missing version data
35       */
36      static public String logbackVersion() {
37          String moduleVersion = getVersionOfLogbackCoreByModule();
38          if (moduleVersion != null)
39              return moduleVersion;
40  
41          Package pkg = EnvUtil.class.getPackage();
42          if (pkg == null) {
43              return null;
44          }
45          return pkg.getImplementationVersion();
46      }
47  
48      /**
49       * <p>Returns the current version of logback via class.getModule() or null if data is not
50       * available.
51       * </p>
52       *
53       * @since 1.3.0
54       * @return current version or null if missing version data
55       */
56      static private String getVersionOfLogbackCoreByModule() {
57          Module module = EnvUtil.class.getModule();
58          if (module == null)
59              return null;
60  
61          ModuleDescriptor md = module.getDescriptor();
62          if (md == null)
63              return null;
64          Optional<String> opt = md.rawVersion();
65          return opt.orElse(null);
66      }
67  
68      static public int getJDKVersion(String javaVersionStr) {
69          int version = 0;
70  
71          for (char ch : javaVersionStr.toCharArray()) {
72              if (Character.isDigit(ch)) {
73                  version = (version * 10) + (ch - 48);
74              } else if (version == 1) {
75                  version = 0;
76              } else {
77                  break;
78              }
79          }
80          return version;
81      }
82  
83      static private boolean isJDK_N_OrHigher(int n) {
84          String javaVersionStr = System.getProperty("java.version", "");
85          if (javaVersionStr.isEmpty())
86              return false;
87  
88          int version = getJDKVersion(javaVersionStr);
89          return version > 0 && n <= version;
90      }
91  
92      static public boolean isJDK5() {
93          return isJDK_N_OrHigher(5);
94      }
95  
96      static public boolean isJDK6OrHigher() {
97          return isJDK_N_OrHigher(6);
98      }
99  
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 }