001/* 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2026, 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 v2.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 */ 014 015package ch.qos.logback.classic.model.processor; 016 017import ch.qos.logback.classic.joran.PropertiesConfigurator; 018import ch.qos.logback.classic.model.ConfigurationModel; 019import ch.qos.logback.classic.model.PropertiesConfiguratorModel; 020import ch.qos.logback.core.Context; 021import ch.qos.logback.core.joran.spi.JoranException; 022import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil; 023import ch.qos.logback.core.model.Model; 024import ch.qos.logback.core.model.ResourceModel; 025import ch.qos.logback.core.model.processor.ModelHandlerException; 026import ch.qos.logback.core.model.processor.ModelInterpretationContext; 027import ch.qos.logback.core.model.processor.ResourceHandlerBase; 028import ch.qos.logback.core.spi.ContextAwarePropertyContainer; 029import ch.qos.logback.core.util.OptionHelper; 030 031import java.io.InputStream; 032import java.net.URL; 033 034public class PropertiesConfiguratorModelHandler extends ResourceHandlerBase { 035 boolean inError = false; 036 037 static final boolean CREATE_CWL_IF_NOT_ALREADY_CREATED = true; 038 039 /** 040 * Key under which a {@link Boolean#TRUE} value is placed in the object map of the 041 * {@link ModelInterpretationContext} when a <propertiesConfigurator> element requests scanning 042 * while the top-level <configuration> element does not. 043 * 044 * <p>Read by {@link ConfigurationModelHandlerFull#postHandle(ModelInterpretationContext, Model)} which is the 045 * only place where a ReconfigureOnChangeTask is scheduled.</p> 046 * 047 * @since 1.6.7 048 */ 049 public static final String LOCAL_SCAN_REQUESTED_KEY = "PROPERTIES_CONFIGURATOR_LOCAL_SCAN_REQUESTED"; 050 051 public PropertiesConfiguratorModelHandler(Context context) { 052 super(context); 053 } 054 055 static public PropertiesConfiguratorModelHandler makeInstance(Context context, ModelInterpretationContext mic) { 056 return new PropertiesConfiguratorModelHandler(context); 057 } 058 059 @Override 060 public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException { 061 062 Boolean topScanBoolean = mic.getTopScanBoolean(); 063 detachedHandle(mic, model, topScanBoolean); 064 } 065 066 /** 067 * 068 * Used by {@link #handle(ModelInterpretationContext, Model)} as well as logback-tyler. Note the widening of the 069 * base from {@link ModelInterpretationContext} to {@link ContextAwarePropertyContainer}. 070 * 071 * @param capc 072 * @param model 073 * @throws ModelHandlerException 074 * @since 1.5.10 075 */ 076 public void detachedHandle(ContextAwarePropertyContainer capc, Model model, Boolean topScanBoolean) throws ModelHandlerException { 077 078 PropertiesConfiguratorModel propertyConfiguratorModel = (PropertiesConfiguratorModel) model; 079 080 this.optional = OptionHelper.toBoolean(propertyConfiguratorModel.getOptional(), false); 081 082 if (!checkAttributes(propertyConfiguratorModel)) { 083 inError = true; 084 return; 085 } 086 087 URL inputURL = getInputURL(capc, propertyConfiguratorModel); 088 if (inputURL == null) { 089 inError = true; 090 return; 091 } 092 093 String substitutedScanStr = capc.subst(propertyConfiguratorModel.getScanStr()); 094 Boolean localScan = OptionHelper.toLopsidedBooleanObject(substitutedScanStr, Boolean.TRUE); 095 096 // a local scan attribute, if set, overrides the scan attribute of the top-level <configuration> element 097 boolean watch = (localScan != null) ? localScan : (topScanBoolean == Boolean.TRUE); 098 099 if (watch) { 100 if (topScanBoolean != Boolean.TRUE) { 101 // if topScanBoolean is not TRUE, then a ConfigurationWatchList has not been created and registered 102 // for this configuration run. We need to do so now, but only once per run so that previous 103 // <propertiesConfigurator> elements are not dropped from the watch list. 104 if (!isLocalScanAlreadyRequested(capc)) { 105 ConfigurationWatchListUtil.registerNewConfigurationWatchListWithContext(context); 106 markLocalScanRequested(capc); 107 } 108 } 109 ConfigurationWatchListUtil.addToWatchList(context, inputURL, CREATE_CWL_IF_NOT_ALREADY_CREATED); 110 } else if (localScan == Boolean.FALSE && topScanBoolean == Boolean.TRUE) { 111 addInfo("Scan attribute set to false for [" + inputURL + "]. Will not add it to configuration watch list."); 112 } 113 114 InputStream in = openURL(inputURL); 115 if (in == null) { 116 inError = true; 117 return; 118 } 119 120 addInfo("Reading configuration from [" + getAttribureInUse() + "]"); 121 122 PropertiesConfigurator propertiesConfigurator = new PropertiesConfigurator(); 123 propertiesConfigurator.setContext(capc.getContext()); 124 try { 125 propertiesConfigurator.doConfigure(in); 126 } catch (JoranException e) { 127 addError("Could not configure from " + getAttribureInUse()); 128 throw new ModelHandlerException(e); 129 } 130 131 } 132 133 private boolean isLocalScanAlreadyRequested(ContextAwarePropertyContainer capc) { 134 if (capc instanceof ModelInterpretationContext) { 135 ModelInterpretationContext mic = (ModelInterpretationContext) capc; 136 return mic.getObjectMap().get(LOCAL_SCAN_REQUESTED_KEY) == Boolean.TRUE; 137 } 138 return false; 139 } 140 141 private void markLocalScanRequested(ContextAwarePropertyContainer capc) { 142 if (capc instanceof ModelInterpretationContext) { 143 ModelInterpretationContext mic = (ModelInterpretationContext) capc; 144 mic.getObjectMap().put(LOCAL_SCAN_REQUESTED_KEY, Boolean.TRUE); 145 } 146 } 147 148 protected InputStream getInputStream(ContextAwarePropertyContainer capc, ResourceModel resourceModel) { 149 URL inputURL = getInputURL(capc, resourceModel); 150 if (inputURL == null) 151 return null; 152 153 154 155 ConfigurationWatchListUtil.addToWatchList(context, inputURL, CREATE_CWL_IF_NOT_ALREADY_CREATED); 156 return openURL(inputURL); 157 } 158 159}