1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2022, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.model.processor;
15  
16  import ch.qos.logback.classic.LoggerContext;
17  import ch.qos.logback.classic.model.ConfigurationModel;
18  import ch.qos.logback.core.Context;
19  import ch.qos.logback.core.model.Model;
20  import ch.qos.logback.core.model.processor.ModelHandlerBase;
21  import ch.qos.logback.core.model.processor.ModelHandlerException;
22  import ch.qos.logback.core.model.processor.ModelInterpretationContext;
23  import ch.qos.logback.core.status.OnConsoleStatusListener;
24  import ch.qos.logback.core.util.ContextUtil;
25  import ch.qos.logback.core.util.Duration;
26  import ch.qos.logback.core.util.OptionHelper;
27  import ch.qos.logback.core.util.StatusListenerConfigHelper;
28  
29  import static ch.qos.logback.core.model.ModelConstants.DEBUG_SYSTEM_PROPERTY_KEY;
30  import static ch.qos.logback.core.model.ModelConstants.NULL_STR;
31  import static java.lang.Boolean.FALSE;
32  
33  /**
34   * In 1.3.9/1.49, ConfigurationModelHandler has been reduced in functionality and no
35   * longer initiates a reconfiguration task. This change was justified by the need
36   * to remove java.xml reachability. See also LOGBACK-1717.
37   *
38   * <p>
39   * See {@link ConfigurationModelHandlerFull} subclass offering configuration
40   * reloading support.
41   * </p>
42   */
43  public class ConfigurationModelHandler extends ModelHandlerBase {
44  
45      static final Duration SCAN_PERIOD_DEFAULT = Duration.buildByMinutes(1);
46  
47      public ConfigurationModelHandler(Context context) {
48          super(context);
49      }
50  
51      static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext mic) {
52          return new ConfigurationModelHandler(context);
53      }
54  
55      protected Class<ConfigurationModel> getSupportedModelClass() {
56          return ConfigurationModel.class;
57      }
58  
59      @Override
60      public void handle(ModelInterpretationContext mic, Model model) {
61  
62          ConfigurationModel configurationModel = (ConfigurationModel) model;
63  
64          // See LOGBACK-527 (the system property is looked up first). Thus, it overrides
65          // the equivalent property in the config file. This reversal of scope priority
66          // is justified by the use case: the admin trying to chase rogue config file
67          String debugAttrib = OptionHelper.getSystemProperty(DEBUG_SYSTEM_PROPERTY_KEY, null);
68          if (debugAttrib == null) {
69              debugAttrib = mic.subst(configurationModel.getDebugStr());
70          }
71          
72  
73          if (!(OptionHelper.isNullOrEmptyOrAllSpaces(debugAttrib) || debugAttrib.equalsIgnoreCase(FALSE.toString())
74                  || debugAttrib.equalsIgnoreCase(NULL_STR))) {
75              StatusListenerConfigHelper.addOnConsoleListenerInstance(context, new OnConsoleStatusListener());
76          }
77  
78          processScanAttrib(mic, configurationModel);
79  
80          LoggerContext lc = (LoggerContext) context;
81          boolean packagingData = OptionHelper.toBoolean(mic.subst(configurationModel.getPackagingDataStr()),
82                  LoggerContext.DEFAULT_PACKAGING_DATA);
83          lc.setPackagingDataEnabled(packagingData);
84  
85          ContextUtil contextUtil = new ContextUtil(context);
86          contextUtil.addGroovyPackages(lc.getFrameworkPackages());
87  
88  
89      }
90  
91      protected void processScanAttrib(ModelInterpretationContext mic, ConfigurationModel configurationModel) {
92          String scanStr = mic.subst(configurationModel.getScanStr());
93          if (!OptionHelper.isNullOrEmptyOrAllSpaces(scanStr) && !"false".equalsIgnoreCase(scanStr)) {
94              addInfo("Skipping ReconfigureOnChangeTask registration");
95          }
96      }
97  
98      protected void postProcessScanAttrib(ModelInterpretationContext mic, ConfigurationModel configurationModel) {
99  
100     }
101 
102     @Override
103     public void postHandle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
104         ConfigurationModel configurationModel = (ConfigurationModel) model;
105 
106     }
107 }