View Javadoc
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       * <p>Returns the current version of logback, or null if data is not
29       * available.
30       * </p>
31       *
32       * @since 1.3.0
33       * @return current version or null if missing version data
34       */
35      static public String logbackVersion() {
36          String moduleVersion = logbackVersionByModule();
37          if(moduleVersion != null)
38              return moduleVersion;
39  
40          Package pkg = EnvUtil.class.getPackage();
41          if (pkg == null) {
42              return null;
43          }
44          return pkg.getImplementationVersion();
45      }
46  
47      /**
48       * <p>Returns the current version of logback via class.getModule() or null if data is not
49       * available.
50       * </p>
51       *
52       * @since 1.3.0
53       * @return current version or null if missing version data
54       */
55      static private String logbackVersionByModule() {
56          Module module = EnvUtil.class.getModule();
57          if (module == null)
58              return null;
59  
60          ModuleDescriptor md = module.getDescriptor();
61          if (md == null)
62              return null;
63          Optional<String> opt = md.rawVersion();
64          return opt.orElse(null);
65      }
66  
67      static public int getJDKVersion(String javaVersionStr) {
68          int version = 0;
69  
70          for (char ch : javaVersionStr.toCharArray()) {
71              if (Character.isDigit(ch)) {
72                  version = (version * 10) + (ch - 48);
73              } else if (version == 1) {
74                  version = 0;
75              } else {
76                  break;
77              }
78          }
79          return version;
80      }
81  
82      static private boolean isJDK_N_OrHigher(int n) {
83          String javaVersionStr = System.getProperty("java.version", "");
84          if (javaVersionStr.isEmpty())
85              return false;
86  
87          int version = getJDKVersion(javaVersionStr);
88          return version > 0 && n <= version;
89      }
90  
91      static public boolean isJDK5() {
92          return isJDK_N_OrHigher(5);
93      }
94  
95      static public boolean isJDK6OrHigher() {
96          return isJDK_N_OrHigher(6);
97      }
98  
99      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 }