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.joran.ReconfigureOnChangeTask;
17  import ch.qos.logback.classic.model.ConfigurationModel;
18  import ch.qos.logback.core.Context;
19  import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil;
20  import ch.qos.logback.core.model.processor.ModelHandlerBase;
21  import ch.qos.logback.core.model.processor.ModelInterpretationContext;
22  import ch.qos.logback.core.spi.ConfigurationEvent;
23  import ch.qos.logback.core.util.Duration;
24  import ch.qos.logback.core.util.OptionHelper;
25  
26  import java.net.URL;
27  import java.util.concurrent.ScheduledExecutorService;
28  import java.util.concurrent.ScheduledFuture;
29  import java.util.concurrent.TimeUnit;
30  
31  /**
32   * This is a subclass of {@link ConfigurationModelHandler} offering configuration reloading support.
33   *
34   */
35  public class ConfigurationModelHandlerFull extends  ConfigurationModelHandler {
36  
37      public ConfigurationModelHandlerFull(Context context) {
38          super(context);
39      }
40  
41  
42  
43      static public ModelHandlerBase makeInstance2(Context context, ModelInterpretationContext mic) {
44          return new ConfigurationModelHandlerFull(context);
45      }
46  
47  
48      protected void processScanAttrib(ModelInterpretationContext mic, ConfigurationModel configurationModel) {
49          String scanStr = mic.subst(configurationModel.getScanStr());
50          if (!OptionHelper.isNullOrEmptyOrAllSpaces(scanStr) && !"false".equalsIgnoreCase(scanStr)) {
51  
52              ScheduledExecutorService scheduledExecutorService = context.getScheduledExecutorService();
53              URL mainURL = ConfigurationWatchListUtil.getMainWatchURL(context);
54              if (mainURL == null) {
55                  addWarn("Due to missing top level configuration file, reconfiguration on change (configuration file scanning) cannot be done.");
56                  return;
57              }
58              ReconfigureOnChangeTask rocTask = new ReconfigureOnChangeTask();
59              rocTask.setContext(context);
60  
61              addInfo("Registering a new ReconfigureOnChangeTask "+ rocTask);
62  
63              context.fireConfigurationEvent(ConfigurationEvent.newConfigurationChangeDetectorRegisteredEvent(rocTask));
64  
65              String scanPeriodStr = mic.subst(configurationModel.getScanPeriodStr());
66              Duration duration = getDurationOfScanPeriodAttribute(scanPeriodStr, SCAN_PERIOD_DEFAULT);
67  
68              addInfo("Will scan for changes in [" + mainURL + "] ");
69              // Given that included files are encountered at a later phase, the complete list
70              // of files
71              // to scan can only be determined when the configuration is loaded in full.
72              // However, scan can be active if mainURL is set. Otherwise, when changes are
73              // detected
74              // the top level config file cannot be accessed.
75              addInfo("Setting ReconfigureOnChangeTask scanning period to " + duration);
76  
77              ScheduledFuture<?> scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(rocTask,
78                      duration.getMilliseconds(), duration.getMilliseconds(), TimeUnit.MILLISECONDS);
79              rocTask.setScheduredFuture(scheduledFuture);
80              context.addScheduledFuture(scheduledFuture);
81          }
82      }
83  
84      private Duration getDurationOfScanPeriodAttribute(String scanPeriodAttrib, Duration defaultDuration) {
85          Duration duration = null;
86  
87          if (!OptionHelper.isNullOrEmptyOrAllSpaces(scanPeriodAttrib)) {
88              try {
89                  duration = Duration.valueOf(scanPeriodAttrib);
90              } catch (IllegalStateException | IllegalArgumentException e) {
91                  addWarn("Failed to parse 'scanPeriod' attribute [" + scanPeriodAttrib + "]", e);
92                  // default duration will be set below
93              }
94          }
95  
96          if (duration == null) {
97              addInfo("No 'scanPeriod' specified. Defaulting to " + defaultDuration.toString());
98              duration = defaultDuration;
99          }
100         return duration;
101     }
102 
103 }