001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.util;
018
019/**
020 * Log4j API Constants.
021 *
022 * @since 2.6.2
023 */
024public final class Constants {
025    /**
026     * {@code true} if we think we are running in a web container, based on the boolean value of system property
027     * "log4j2.is.webapp", or (if this system property is not set) whether the  {@code javax.servlet.Servlet} class
028     * is present in the classpath.
029     */
030    public static final boolean IS_WEB_APP = PropertiesUtil.getProperties().getBooleanProperty(
031            "log4j2.is.webapp", isClassAvailable("javax.servlet.Servlet")
032                    || isClassAvailable("jakarta.servlet.Servlet"));
033
034    /**
035     * Kill switch for object pooling in ThreadLocals that enables much of the LOG4J2-1270 no-GC behaviour.
036     * <p>
037     * {@code True} for non-{@link #IS_WEB_APP web apps}, disable by setting system property
038     * "log4j2.enable.threadlocals" to "false".
039     * </p>
040     */
041    public static final boolean ENABLE_THREADLOCALS = !IS_WEB_APP && PropertiesUtil.getProperties().getBooleanProperty(
042            "log4j2.enable.threadlocals", true);
043
044    /**
045     * Java major version.
046     */
047    public static final int JAVA_MAJOR_VERSION = getMajorVersion();
048
049    /**
050     * Maximum size of the StringBuilders used in RingBuffer LogEvents to store the contents of reusable Messages.
051     * After a large message has been delivered to the appenders, the StringBuilder is trimmed to this size.
052     * <p>
053     * The default value is {@value}, which allows the StringBuilder to resize three times from its initial size.
054     * Users can override with system property "log4j.maxReusableMsgSize".
055     * </p>
056     * @since 2.9
057     */
058    public static final int MAX_REUSABLE_MESSAGE_SIZE = size("log4j.maxReusableMsgSize", (128 * 2 + 2) * 2 + 2);
059
060    /**
061     * Name of the system property that will turn on TRACE level internal log4j2 status logging.
062     * <p>
063     * If system property {@value} is either defined empty or its value equals to {@code true} (ignoring case), all internal log4j2 logging will be
064     * printed to the console. The presence of this system property overrides any value set in the configuration's
065     * {@code <Configuration status="<level>" ...>} status attribute, as well as any value set for
066     * system property {@code org.apache.logging.log4j.simplelog.StatusLogger.level}.
067     * </p>
068     */
069    public static final String LOG4J2_DEBUG = "log4j2.debug";
070
071    private static int size(final String property, final int defaultValue) {
072        return PropertiesUtil.getProperties().getIntegerProperty(property, defaultValue);
073    }
074
075    /**
076     * Determines if a named Class can be loaded or not.
077     *
078     * @param className The class name.
079     * @return {@code true} if the class could be found or {@code false} otherwise.
080     */
081    private static boolean isClassAvailable(final String className) {
082        try {
083            return LoaderUtil.loadClass(className) != null;
084        } catch (final Throwable e) {
085            return false;
086        }
087    }
088
089    /**
090     * The empty array.
091     */
092    public static final Object[] EMPTY_OBJECT_ARRAY = {};
093
094    /**
095     * The empty array.
096     */
097    public static final byte[] EMPTY_BYTE_ARRAY = {};
098
099    /**
100     * Prevent class instantiation.
101     */
102    private Constants() {
103    }
104
105    private static int getMajorVersion() {
106        return getMajorVersion(System.getProperty("java.version"));
107    }
108
109    static int getMajorVersion(final String version) {
110        final String[] parts = version.split("-|\\.");
111        boolean isJEP223;
112        try {
113            final int token = Integer.parseInt(parts[0]);
114            isJEP223 = token != 1;
115            if (isJEP223) {
116                return token;
117            }
118            return Integer.parseInt(parts[1]);
119        } catch (final Exception ex) {
120            return 0;
121        }
122    }
123}