001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2024, 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 */
014
015package ch.qos.logback.classic.model.processor;
016
017import ch.qos.logback.classic.joran.PropertiesConfigurator;
018import ch.qos.logback.classic.model.PropertiesConfiguratorModel;
019import ch.qos.logback.core.Context;
020import ch.qos.logback.core.joran.spi.JoranException;
021import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil;
022import ch.qos.logback.core.model.Model;
023import ch.qos.logback.core.model.ResourceModel;
024import ch.qos.logback.core.model.processor.ModelHandlerException;
025import ch.qos.logback.core.model.processor.ModelInterpretationContext;
026import ch.qos.logback.core.model.processor.ResourceHandlerBase;
027import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
028import ch.qos.logback.core.util.OptionHelper;
029
030import java.io.InputStream;
031import java.net.URL;
032
033public class PropertiesConfiguratorModelHandler extends ResourceHandlerBase {
034    boolean inError = false;
035
036    static final boolean CREATE_CWL_IF_NOT_ALREADY_CREATED = true;
037
038    public PropertiesConfiguratorModelHandler(Context context) {
039        super(context);
040    }
041
042    static public PropertiesConfiguratorModelHandler makeInstance(Context context, ModelInterpretationContext mic) {
043        return new PropertiesConfiguratorModelHandler(context);
044    }
045
046    @Override
047    public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
048        detachedHandle(mic, model);
049    }
050
051    /**
052     *
053     * Used by {@link #handle(ModelInterpretationContext, Model)} as well as logback-tyler. Note the widening of the
054     * base from {@link ModelInterpretationContext} to {@link ContextAwarePropertyContainer}.
055     *
056     * @param capc
057     * @param model
058     * @throws ModelHandlerException
059     * @since 1.5.10
060     */
061    public void detachedHandle(ContextAwarePropertyContainer capc, Model model) throws ModelHandlerException {
062
063        PropertiesConfiguratorModel propertyConfiguratorModel = (PropertiesConfiguratorModel) model;
064
065        this.optional = OptionHelper.toBoolean(propertyConfiguratorModel.getOptional(), false);
066
067        if (!checkAttributes(propertyConfiguratorModel)) {
068            inError = true;
069            return;
070        }
071
072        InputStream in = getInputStream(capc, propertyConfiguratorModel);
073        if (in == null) {
074            inError = true;
075            return;
076        }
077
078        addInfo("Reading configuration from [" + getAttribureInUse() + "]");
079
080        PropertiesConfigurator propertiesConfigurator = new PropertiesConfigurator();
081        propertiesConfigurator.setContext(capc.getContext());
082        try {
083            propertiesConfigurator.doConfigure(in);
084        } catch (JoranException e) {
085            addError("Could not configure from " + getAttribureInUse());
086            throw new ModelHandlerException(e);
087        }
088
089    }
090
091    protected InputStream getInputStream(ContextAwarePropertyContainer capc, ResourceModel resourceModel) {
092        URL inputURL = getInputURL(capc, resourceModel);
093        if (inputURL == null)
094            return null;
095
096        ConfigurationWatchListUtil.addToWatchList(context, inputURL, CREATE_CWL_IF_NOT_ALREADY_CREATED);
097        return openURL(inputURL);
098    }
099
100}