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      /**
44       * Determines if a named Class can be loaded or not.
45       *
46       * @param className The class name.
47       * @return {@code true} if the class could be found or {@code false} otherwise.
48       */
49      private static boolean isClassAvailable(final String className) {
50          try {
51              return LoaderUtil.loadClass(className) != null;
52          } catch (final Throwable e) {
53              return false;
54          }
55      }
56  
57      /**
58       * Prevent class instantiation.
59       */
60      private Constants() {
61      }
62  }