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