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.spi.ConfigurationWatchList; 020import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil; 021import ch.qos.logback.core.model.Model; 022import ch.qos.logback.core.model.processor.ModelHandlerBase; 023import ch.qos.logback.core.model.processor.ModelHandlerException; 024import ch.qos.logback.core.model.processor.ModelInterpretationContext; 025import ch.qos.logback.core.spi.ConfigurationEvent; 026import ch.qos.logback.core.util.Duration; 027import ch.qos.logback.core.util.OptionHelper; 028 029import java.util.concurrent.ScheduledExecutorService; 030import java.util.concurrent.ScheduledFuture; 031import java.util.concurrent.TimeUnit; 032 033/** 034 * This is a subclass of {@link ConfigurationModelHandler} offering configuration reloading support. 035 * 036 */ 037public class ConfigurationModelHandlerFull extends ConfigurationModelHandler { 038 039 public static String FAILED_WATCH_PREDICATE_MESSAGE_1 = "Missing watchable .xml or .properties files."; 040 public static String FAILED_WATCH_PREDICATE_MESSAGE_2 = "Watching .xml files requires that the main configuration file is reachable as a URL"; 041 042 public ConfigurationModelHandlerFull(Context context) { 043 super(context); 044 } 045 046 static public ModelHandlerBase makeInstance2(Context context, ModelInterpretationContext mic) { 047 return new ConfigurationModelHandlerFull(context); 048 } 049 050 @Override 051 protected void processScanAttrib(ModelInterpretationContext mic, ConfigurationModel configurationModel) { 052 // override parent to do nothing 053 } 054 055 @Override 056 public void postHandle(ModelInterpretationContext mic, Model model) throws ModelHandlerException { 057 ConfigurationModel configurationModel = (ConfigurationModel) model; 058 // post handling of scan attribute works even we need to watch for included files because the main url is 059 // set in GenericXMLConfigurator very early in the configuration process 060 postProcessScanAttrib(mic, configurationModel); 061 062 ConfigurationWatchList cwl = ConfigurationWatchListUtil.getConfigurationWatchList(getContext()); 063 if (cwl != null) { 064 try { 065 addInfo("Main configuration file URL: " + cwl.getMainURL()); 066 addInfo("FileWatchList= {" + cwl.getFileWatchListAsStr()+"}"); 067 addInfo("URLWatchList= {" + cwl.getUrlWatchListAsStr()+"}"); 068 } catch(NoSuchMethodError e) { 069 addWarn("It looks like the version of logback-classic is more recent than"); 070 addWarn("the version of logback-core. Please align the two versions."); 071 } 072 } 073 } 074 075 protected void postProcessScanAttrib(ModelInterpretationContext mic, ConfigurationModel configurationModel) { 076 String scanStr = mic.subst(configurationModel.getScanStr()); 077 String scanPeriodStr = mic.subst(configurationModel.getScanPeriodStr()); 078 detachedPostProcess(scanStr, scanPeriodStr); 079 } 080 081 /** 082 * This method is called from this class but also from logback-tyler. 083 * 084 * This method assumes that the variables scanStr and scanPeriodStr have undergone variable substitution 085 * as applicable to their current environment 086 * 087 * @param scanStr 088 * @param scanPeriodStr 089 * @since 1.5.0 090 */ 091 public void detachedPostProcess(String scanStr, String scanPeriodStr) { 092 if (!OptionHelper.isNullOrEmptyOrAllSpaces(scanStr) && !"false".equalsIgnoreCase(scanStr)) { 093 ScheduledExecutorService scheduledExecutorService = context.getScheduledExecutorService(); 094 boolean watchPredicateFulfilled = ConfigurationWatchListUtil.watchPredicateFulfilled(context); 095 if (!watchPredicateFulfilled) { 096 addWarn(FAILED_WATCH_PREDICATE_MESSAGE_1); 097 addWarn(FAILED_WATCH_PREDICATE_MESSAGE_2); 098 return; 099 } 100 ReconfigureOnChangeTask rocTask = new ReconfigureOnChangeTask(); 101 rocTask.setContext(context); 102 103 addInfo("Registering a new ReconfigureOnChangeTask " + rocTask); 104 105 context.fireConfigurationEvent(ConfigurationEvent.newConfigurationChangeDetectorRegisteredEvent(rocTask)); 106 107 Duration duration = getDurationOfScanPeriodAttribute(scanPeriodStr, SCAN_PERIOD_DEFAULT); 108 109 addInfo("Will scan for changes in [" + ConfigurationWatchListUtil.getConfigurationWatchList(context) + "] "); 110 // Given that included files are encountered at a later phase, the complete list 111 // of files to scan can only be determined when the configuration is loaded in full. 112 // However, scan can be active if mainURL is set. Otherwise, when changes are 113 // detected the top level config file cannot be accessed. 114 addInfo("Setting ReconfigureOnChangeTask scanning period to " + duration); 115 116 ScheduledFuture<?> scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(rocTask, duration.getMilliseconds(), duration.getMilliseconds(), 117 TimeUnit.MILLISECONDS); 118 rocTask.setScheduledFuture(scheduledFuture); 119 context.addScheduledFuture(scheduledFuture); 120 } 121 122 } 123 124 private Duration getDurationOfScanPeriodAttribute(String scanPeriodAttrib, Duration defaultDuration) { 125 Duration duration = null; 126 127 if (!OptionHelper.isNullOrEmptyOrAllSpaces(scanPeriodAttrib)) { 128 try { 129 duration = Duration.valueOf(scanPeriodAttrib); 130 } catch (IllegalStateException | IllegalArgumentException e) { 131 addWarn("Failed to parse 'scanPeriod' attribute [" + scanPeriodAttrib + "]", e); 132 // default duration will be set below 133 } 134 } 135 136 if (duration == null) { 137 addInfo("No 'scanPeriod' specified. Defaulting to " + defaultDuration.toString()); 138 duration = defaultDuration; 139 } 140 return duration; 141 } 142}