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
082        Boolean localScan = OptionHelper.toBooleanObject(propertyConfiguratorModel.getScanStr());
083
084        InputStream in = openURL(inputURL);
085        if (in == null) {
086            inError = true;
087            return;
088        }
089
090        if(localScan == Boolean.TRUE || topScanBoolean == Boolean.TRUE) {
091            if(topScanBoolean != Boolean.TRUE) {
092                // if topScanBoolean ia not TRUE, then a ConfigurationWatchList has not been created and registered, yet
093                // we need to do so now
094                ConfigurationWatchListUtil.registerNewConfigurationWatchListWithContext(context);
095            }
096            ConfigurationWatchListUtil.addToWatchList(context, inputURL, CREATE_CWL_IF_NOT_ALREADY_CREATED);
097        }
098
099
100
101        addInfo("Reading configuration from [" + getAttribureInUse() + "]");
102
103        PropertiesConfigurator propertiesConfigurator = new PropertiesConfigurator();
104        propertiesConfigurator.setContext(capc.getContext());
105        try {
106            propertiesConfigurator.doConfigure(in);
107        } catch (JoranException e) {
108            addError("Could not configure from " + getAttribureInUse());
109            throw new ModelHandlerException(e);
110        }
111
112    }
113
114    protected InputStream getInputStream(ContextAwarePropertyContainer capc, ResourceModel resourceModel) {
115        URL inputURL = getInputURL(capc, resourceModel);
116        if (inputURL == null)
117            return null;
118
119
120
121        ConfigurationWatchListUtil.addToWatchList(context, inputURL, CREATE_CWL_IF_NOT_ALREADY_CREATED);
122        return openURL(inputURL);
123    }
124
125}