001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v2.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014
015package ch.qos.logback.classic.servlet;
016
017import static ch.qos.logback.core.CoreConstants.DISABLE_SERVLET_CONTAINER_INITIALIZER_KEY;
018
019import java.util.Set;
020
021import jakarta.servlet.ServletContainerInitializer;
022import jakarta.servlet.ServletContext;
023import jakarta.servlet.ServletException;
024
025import ch.qos.logback.classic.util.StatusViaSLF4JLoggerFactory;
026import ch.qos.logback.core.util.OptionHelper;
027
028/**
029 * Attaches a new instance of {@link LogbackServletContextListener} to the
030 * current web-applications {@link ServletContext}.
031 * 
032 * @author Ceki Gulcu
033 * @since 1.1.10
034 */
035public class LogbackServletContainerInitializer implements ServletContainerInitializer {
036
037    @Override
038    public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
039
040        if (isDisabledByConfiguration(ctx)) {
041            StatusViaSLF4JLoggerFactory.addInfo("Due to deployment instructions will NOT register an instance of "
042                    + LogbackServletContextListener.class + " to the current web-app", this);
043
044            return;
045        }
046
047        StatusViaSLF4JLoggerFactory.addInfo(
048                "Adding an instance of  " + LogbackServletContextListener.class + " to the current web-app", this);
049        LogbackServletContextListener lscl = new LogbackServletContextListener();
050        ctx.addListener(lscl);
051    }
052
053    /**
054     * Search for value of DISABLE_SERVLET_CONTAINER_INITIALIZER_KEY in the web-app
055     * first, then as a system property and as an environment variable last.
056     *
057     * @param ctx
058     * @return True if value of DISABLE_SERVLET_CONTAINER_INITIALIZER_KEY is
059     *         available and set to "true", false otherwise.
060     */
061    boolean isDisabledByConfiguration(ServletContext ctx) {
062        String disableAttributeStr = null;
063        Object disableAttribute = ctx.getInitParameter(DISABLE_SERVLET_CONTAINER_INITIALIZER_KEY);
064        if (disableAttribute instanceof String) {
065            disableAttributeStr = (String) disableAttribute;
066        }
067
068        if (OptionHelper.isNullOrEmptyOrAllSpaces(disableAttributeStr)) {
069            disableAttributeStr = OptionHelper.getSystemProperty(DISABLE_SERVLET_CONTAINER_INITIALIZER_KEY);
070        }
071
072        if (OptionHelper.isNullOrEmptyOrAllSpaces(disableAttributeStr)) {
073            disableAttributeStr = OptionHelper.getEnv(DISABLE_SERVLET_CONTAINER_INITIALIZER_KEY);
074        }
075
076        return Boolean.parseBoolean(disableAttributeStr);
077    }
078
079}