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