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 public PropertiesConfiguratorModelHandler(Context context) { 040 super(context); 041 } 042 043 static public PropertiesConfiguratorModelHandler makeInstance(Context context, ModelInterpretationContext mic) { 044 return new PropertiesConfiguratorModelHandler(context); 045 } 046 047 @Override 048 public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException { 049 050 Boolean topScanBoolean = mic.getTopScanBoolean(); 051 detachedHandle(mic, model, topScanBoolean); 052 } 053 054 /** 055 * 056 * Used by {@link #handle(ModelInterpretationContext, Model)} as well as logback-tyler. Note the widening of the 057 * base from {@link ModelInterpretationContext} to {@link ContextAwarePropertyContainer}. 058 * 059 * @param capc 060 * @param model 061 * @throws ModelHandlerException 062 * @since 1.5.10 063 */ 064 public void detachedHandle(ContextAwarePropertyContainer capc, Model model, Boolean topScanBoolean) throws ModelHandlerException { 065 066 PropertiesConfiguratorModel propertyConfiguratorModel = (PropertiesConfiguratorModel) model; 067 068 this.optional = OptionHelper.toBoolean(propertyConfiguratorModel.getOptional(), false); 069 070 if (!checkAttributes(propertyConfiguratorModel)) { 071 inError = true; 072 return; 073 } 074 075 URL inputURL = getInputURL(capc, propertyConfiguratorModel); 076 if (inputURL == null) { 077 inError = true; 078 return; 079 } 080 081 Boolean localScan = ConfigurationModelHandler.scanStringToBoolean( 082 capc.subst(propertyConfiguratorModel.getScanStr())); 083 084 if(localScan == Boolean.TRUE || topScanBoolean == Boolean.TRUE) { 085 if(topScanBoolean != Boolean.TRUE) { 086 // if topScanBoolean is not TRUE, then a ConfigurationWatchList has not been created and registered yet. 087 // We need to do so now 088 ConfigurationWatchListUtil.registerNewConfigurationWatchListWithContext(context); 089 } 090 ConfigurationWatchListUtil.addToWatchList(context, inputURL, CREATE_CWL_IF_NOT_ALREADY_CREATED); 091 } 092 093 InputStream in = openURL(inputURL); 094 if (in == null) { 095 inError = true; 096 return; 097 } 098 099 addInfo("Reading configuration from [" + getAttribureInUse() + "]"); 100 101 PropertiesConfigurator propertiesConfigurator = new PropertiesConfigurator(); 102 propertiesConfigurator.setContext(capc.getContext()); 103 try { 104 propertiesConfigurator.doConfigure(in); 105 } catch (JoranException e) { 106 addError("Could not configure from " + getAttribureInUse()); 107 throw new ModelHandlerException(e); 108 } 109 110 } 111 112 protected InputStream getInputStream(ContextAwarePropertyContainer capc, ResourceModel resourceModel) { 113 URL inputURL = getInputURL(capc, resourceModel); 114 if (inputURL == null) 115 return null; 116 117 118 119 ConfigurationWatchListUtil.addToWatchList(context, inputURL, CREATE_CWL_IF_NOT_ALREADY_CREATED); 120 return openURL(inputURL); 121 } 122 123}