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.joran.ReconfigureOnChangeTask; 017import ch.qos.logback.classic.model.ConfigurationModel; 018import ch.qos.logback.core.Context; 019import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil; 020import ch.qos.logback.core.model.processor.ModelHandlerBase; 021import ch.qos.logback.core.model.processor.ModelInterpretationContext; 022import ch.qos.logback.core.spi.ConfigurationEvent; 023import ch.qos.logback.core.util.Duration; 024import ch.qos.logback.core.util.OptionHelper; 025 026import java.net.URL; 027import java.util.concurrent.ScheduledExecutorService; 028import java.util.concurrent.ScheduledFuture; 029import java.util.concurrent.TimeUnit; 030 031/** 032 * This is a subclass of {@link ConfigurationModelHandler} offering configuration reloading support. 033 * 034 */ 035public class ConfigurationModelHandlerFull extends ConfigurationModelHandler { 036 037 public ConfigurationModelHandlerFull(Context context) { 038 super(context); 039 } 040 041 042 043 static public ModelHandlerBase makeInstance2(Context context, ModelInterpretationContext mic) { 044 return new ConfigurationModelHandlerFull(context); 045 } 046 047 048 protected void processScanAttrib(ModelInterpretationContext mic, ConfigurationModel configurationModel) { 049 String scanStr = mic.subst(configurationModel.getScanStr()); 050 if (!OptionHelper.isNullOrEmptyOrAllSpaces(scanStr) && !"false".equalsIgnoreCase(scanStr)) { 051 052 ScheduledExecutorService scheduledExecutorService = context.getScheduledExecutorService(); 053 URL mainURL = ConfigurationWatchListUtil.getMainWatchURL(context); 054 if (mainURL == null) { 055 addWarn("Due to missing top level configuration file, reconfiguration on change (configuration file scanning) cannot be done."); 056 return; 057 } 058 ReconfigureOnChangeTask rocTask = new ReconfigureOnChangeTask(); 059 rocTask.setContext(context); 060 061 addInfo("Registering a new ReconfigureOnChangeTask "+ rocTask); 062 063 context.fireConfigurationEvent(ConfigurationEvent.newConfigurationChangeDetectorRegisteredEvent(rocTask)); 064 065 String scanPeriodStr = mic.subst(configurationModel.getScanPeriodStr()); 066 Duration duration = getDurationOfScanPeriodAttribute(scanPeriodStr, SCAN_PERIOD_DEFAULT); 067 068 addInfo("Will scan for changes in [" + mainURL + "] "); 069 // Given that included files are encountered at a later phase, the complete list 070 // of files 071 // to scan can only be determined when the configuration is loaded in full. 072 // However, scan can be active if mainURL is set. Otherwise, when changes are 073 // detected 074 // the top level config file cannot be accessed. 075 addInfo("Setting ReconfigureOnChangeTask scanning period to " + duration); 076 077 ScheduledFuture<?> scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(rocTask, 078 duration.getMilliseconds(), duration.getMilliseconds(), TimeUnit.MILLISECONDS); 079 rocTask.setScheduredFuture(scheduledFuture); 080 context.addScheduledFuture(scheduledFuture); 081 } 082 } 083 084 private Duration getDurationOfScanPeriodAttribute(String scanPeriodAttrib, Duration defaultDuration) { 085 Duration duration = null; 086 087 if (!OptionHelper.isNullOrEmptyOrAllSpaces(scanPeriodAttrib)) { 088 try { 089 duration = Duration.valueOf(scanPeriodAttrib); 090 } catch (IllegalStateException | IllegalArgumentException e) { 091 addWarn("Failed to parse 'scanPeriod' attribute [" + scanPeriodAttrib + "]", e); 092 // default duration will be set below 093 } 094 } 095 096 if (duration == null) { 097 addInfo("No 'scanPeriod' specified. Defaulting to " + defaultDuration.toString()); 098 duration = defaultDuration; 099 } 100 return duration; 101 } 102 103}