View Javadoc
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.ModelInterpretationContext;
22  import ch.qos.logback.core.status.OnConsoleStatusListener;
23  import ch.qos.logback.core.util.ContextUtil;
24  import ch.qos.logback.core.util.Duration;
25  import ch.qos.logback.core.util.OptionHelper;
26  import ch.qos.logback.core.util.StatusListenerConfigHelper;
27  
28  import static ch.qos.logback.core.model.ModelConstants.DEBUG_SYSTEM_PROPERTY_KEY;
29  import static ch.qos.logback.core.model.ModelConstants.NULL_STR;
30  import static java.lang.Boolean.FALSE;
31  
32  /**
33   * In 1.3.9/1.49, ConfigurationModelHandler has been reduced in functionality and no
34   * longer initiates a reconfiguration task. This change was justified by the need
35   * to remove java.xml reachability. See also LOGBACK-1717.
36   *
37   * <p>
38   * See {@link ConfigurationModelHandlerFull} subclass offering configuration
39   * reloading support.
40   * </p>
41   */
42  public class ConfigurationModelHandler extends ModelHandlerBase {
43  
44      static final Duration SCAN_PERIOD_DEFAULT = Duration.buildByMinutes(1);
45  
46      public ConfigurationModelHandler(Context context) {
47          super(context);
48      }
49  
50      static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext mic) {
51          return new ConfigurationModelHandler(context);
52      }
53  
54      protected Class<ConfigurationModel> getSupportedModelClass() {
55          return ConfigurationModel.class;
56      }
57  
58      @Override
59      public void handle(ModelInterpretationContext mic, Model model) {
60  
61          ConfigurationModel configurationModel = (ConfigurationModel) model;
62  
63          // See LOGBACK-527 (the system property is looked up first). Thus, it overrides
64          // the equivalent property in the config file. This reversal of scope priority
65          // is justified by the use case: the admin trying to chase rogue config file
66          String debugAttrib = OptionHelper.getSystemProperty(DEBUG_SYSTEM_PROPERTY_KEY, null);
67          if (debugAttrib == null) {
68              debugAttrib = mic.subst(configurationModel.getDebugStr());
69          }
70          
71  
72          if (!(OptionHelper.isNullOrEmptyOrAllSpaces(debugAttrib) || debugAttrib.equalsIgnoreCase(FALSE.toString())
73                  || debugAttrib.equalsIgnoreCase(NULL_STR))) {
74              StatusListenerConfigHelper.addOnConsoleListenerInstance(context, new OnConsoleStatusListener());
75          }
76  
77          processScanAttrib(mic, configurationModel);
78  
79          LoggerContext lc = (LoggerContext) context;
80          boolean packagingData = OptionHelper.toBoolean(mic.subst(configurationModel.getPackagingDataStr()),
81                  LoggerContext.DEFAULT_PACKAGING_DATA);
82          lc.setPackagingDataEnabled(packagingData);
83  
84          ContextUtil contextUtil = new ContextUtil(context);
85          contextUtil.addGroovyPackages(lc.getFrameworkPackages());
86      }
87  
88      protected void processScanAttrib(ModelInterpretationContext mic, ConfigurationModel configurationModel) {
89          String scanStr = mic.subst(configurationModel.getScanStr());
90          if (!OptionHelper.isNullOrEmptyOrAllSpaces(scanStr) && !"false".equalsIgnoreCase(scanStr)) {
91              addInfo("Skipping ReconfigureOnChangeTask registration");
92          }
93      }
94  
95  
96  }