001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2022, 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 v1.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 */
014package ch.qos.logback.classic.model.processor;
015
016import ch.qos.logback.classic.LoggerContext;
017import ch.qos.logback.classic.model.ConfigurationModel;
018import ch.qos.logback.core.Context;
019import ch.qos.logback.core.model.Model;
020import ch.qos.logback.core.model.processor.ModelHandlerBase;
021import ch.qos.logback.core.model.processor.ModelInterpretationContext;
022import ch.qos.logback.core.status.OnConsoleStatusListener;
023import ch.qos.logback.core.util.ContextUtil;
024import ch.qos.logback.core.util.Duration;
025import ch.qos.logback.core.util.OptionHelper;
026import ch.qos.logback.core.util.StatusListenerConfigHelper;
027
028import static ch.qos.logback.core.model.ModelConstants.DEBUG_SYSTEM_PROPERTY_KEY;
029import static ch.qos.logback.core.model.ModelConstants.NULL_STR;
030import static java.lang.Boolean.FALSE;
031
032/**
033 * In 1.3.9/1.49, ConfigurationModelHandler has been reduced in functionality and no
034 * longer initiates a reconfiguration task. This change was justified by the need
035 * to remove java.xml reachability. See also LOGBACK-1717.
036 *
037 * <p>
038 * See {@link ConfigurationModelHandlerFull} subclass offering configuration
039 * reloading support.
040 * </p>
041 */
042public class ConfigurationModelHandler extends ModelHandlerBase {
043
044    static final Duration SCAN_PERIOD_DEFAULT = Duration.buildByMinutes(1);
045
046    public ConfigurationModelHandler(Context context) {
047        super(context);
048    }
049
050    static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext mic) {
051        return new ConfigurationModelHandler(context);
052    }
053
054    protected Class<ConfigurationModel> getSupportedModelClass() {
055        return ConfigurationModel.class;
056    }
057
058    @Override
059    public void handle(ModelInterpretationContext mic, Model model) {
060
061        ConfigurationModel configurationModel = (ConfigurationModel) model;
062
063        // See LOGBACK-527 (the system property is looked up first). Thus, it overrides
064        // the equivalent property in the config file. This reversal of scope priority
065        // is justified by the use case: the admin trying to chase rogue config file
066        String debugAttrib = OptionHelper.getSystemProperty(DEBUG_SYSTEM_PROPERTY_KEY, null);
067        if (debugAttrib == null) {
068            debugAttrib = mic.subst(configurationModel.getDebugStr());
069        }
070        
071
072        if (!(OptionHelper.isNullOrEmptyOrAllSpaces(debugAttrib) || debugAttrib.equalsIgnoreCase(FALSE.toString())
073                || debugAttrib.equalsIgnoreCase(NULL_STR))) {
074            StatusListenerConfigHelper.addOnConsoleListenerInstance(context, new OnConsoleStatusListener());
075        }
076
077        processScanAttrib(mic, configurationModel);
078
079        LoggerContext lc = (LoggerContext) context;
080        boolean packagingData = OptionHelper.toBoolean(mic.subst(configurationModel.getPackagingDataStr()),
081                LoggerContext.DEFAULT_PACKAGING_DATA);
082        lc.setPackagingDataEnabled(packagingData);
083
084        ContextUtil contextUtil = new ContextUtil(context);
085        contextUtil.addGroovyPackages(lc.getFrameworkPackages());
086    }
087
088    protected void processScanAttrib(ModelInterpretationContext mic, ConfigurationModel configurationModel) {
089        String scanStr = mic.subst(configurationModel.getScanStr());
090        if (!OptionHelper.isNullOrEmptyOrAllSpaces(scanStr) && !"false".equalsIgnoreCase(scanStr)) {
091            addInfo("Skipping ReconfigureOnChangeTask registration");
092        }
093    }
094
095
096}