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.PropertyConfigurator;
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.util.OptionHelper;
028
029import java.io.InputStream;
030import java.net.URL;
031
032public class PropertiesConfiguratorModelHandler extends ResourceHandlerBase {
033    boolean inError = false;
034
035    static final boolean CREATE_CWL_IF_NOT_ALREADY_CREATED = true;
036
037    public PropertiesConfiguratorModelHandler(Context context) {
038        super(context);
039    }
040
041    static public PropertiesConfiguratorModelHandler makeInstance(Context context, ModelInterpretationContext mic) {
042        return new PropertiesConfiguratorModelHandler(context);
043    }
044
045    @Override
046    public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
047        PropertiesConfiguratorModel propertyConfiguratorModel = (PropertiesConfiguratorModel) model;
048
049        this.optional = OptionHelper.toBoolean(propertyConfiguratorModel.getOptional(), false);
050
051        if (!checkAttributes(propertyConfiguratorModel)) {
052            inError = true;
053            return;
054        }
055
056        InputStream in = getInputStream(mic, propertyConfiguratorModel);
057        if(in == null) {
058            inError = true;
059            return;
060        }
061
062        addInfo("Reading configuration from ["+getAttribureInUse()+"]");
063
064        PropertyConfigurator propertyConfigurator = new PropertyConfigurator();
065        propertyConfigurator.setContext(mic.getContext());
066        try {
067            propertyConfigurator.doConfigure(in);
068        } catch (JoranException e) {
069            addError("Could not configure from "+getAttribureInUse());
070            throw new ModelHandlerException(e);
071        }
072
073    }
074
075    protected InputStream getInputStream(ModelInterpretationContext mic, ResourceModel resourceModel) {
076        URL inputURL = getInputURL(mic, resourceModel);
077        if (inputURL == null)
078            return null;
079
080        ConfigurationWatchListUtil.addToWatchList(context, inputURL, CREATE_CWL_IF_NOT_ALREADY_CREATED);
081        return openURL(inputURL);
082    }
083
084}