View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.util;
18  
19  /**
20   * Log4j API Constants.
21   *
22   * @since 2.6.2
23   */
24  public final class Constants {
25      /**
26       * {@code true} if we think we are running in a web container, based on the boolean value of system property
27       * "log4j2.is.webapp", or (if this system property is not set) whether the  {@code javax.servlet.Servlet} class
28       * is present in the classpath.
29       */
30      public static final boolean IS_WEB_APP = PropertiesUtil.getProperties().getBooleanProperty(
31              "log4j2.is.webapp", isClassAvailable("javax.servlet.Servlet"));
32  
33      /**
34       * Kill switch for object pooling in ThreadLocals that enables much of the LOG4J2-1270 no-GC behaviour.
35       * <p>
36       * {@code True} for non-{@link #IS_WEB_APP web apps}, disable by setting system property
37       * "log4j2.enable.threadlocals" to "false".
38       * </p>
39       */
40      public static final boolean ENABLE_THREADLOCALS = !IS_WEB_APP && PropertiesUtil.getProperties().getBooleanProperty(
41              "log4j2.enable.threadlocals", true);
42  
43      public static final int JAVA_MAJOR_VERSION = getMajorVersion();
44  
45      /**
46       * Determines if a named Class can be loaded or not.
47       *
48       * @param className The class name.
49       * @return {@code true} if the class could be found or {@code false} otherwise.
50       */
51      private static boolean isClassAvailable(final String className) {
52          try {
53              return LoaderUtil.loadClass(className) != null;
54          } catch (final Throwable e) {
55              return false;
56          }
57      }
58  
59      /**
60       * Prevent class instantiation.
61       */
62      private Constants() {
63      }
64  
65      private static int getMajorVersion() {
66          final String version = System.getProperty("java.version");
67          final String[] parts = version.split("-|\\.");
68          boolean isJEP223;
69          try {
70              final int token = Integer.parseInt(parts[0]);
71              isJEP223 = token != 1;
72              if (isJEP223) {
73                  return token;
74              }
75              return Integer.parseInt(parts[1]);
76          } catch (final Exception ex) {
77              return 0;
78          }
79      }
80  }