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.core.model.processor;
016
017import ch.qos.logback.core.Context;
018import ch.qos.logback.core.joran.GenericXMLConfigurator;
019import ch.qos.logback.core.joran.event.SaxEvent;
020import ch.qos.logback.core.joran.event.SaxEventRecorder;
021import ch.qos.logback.core.joran.spi.JoranException;
022import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil;
023import ch.qos.logback.core.model.IncludeModel;
024import ch.qos.logback.core.model.Model;
025import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
026import ch.qos.logback.core.spi.ErrorCodes;
027import ch.qos.logback.core.util.Loader;
028import ch.qos.logback.core.util.OptionHelper;
029
030import java.io.File;
031import java.io.IOException;
032import java.io.InputStream;
033import java.net.MalformedURLException;
034import java.net.URI;
035import java.net.URL;
036import java.util.List;
037import java.util.function.Supplier;
038
039import static ch.qos.logback.core.joran.JoranConstants.CONFIGURATION_TAG;
040import static ch.qos.logback.core.joran.JoranConstants.INCLUDED_TAG;
041
042/**
043 * @since 1.5.5
044 */
045public class IncludeModelHandler extends ResourceHandlerBase {
046    boolean inError = false;
047
048    public IncludeModelHandler(Context context) {
049        super(context);
050    }
051
052    static public IncludeModelHandler makeInstance(Context context, ModelInterpretationContext mic) {
053        return new IncludeModelHandler(context);
054    }
055
056    @Override
057    protected Class<IncludeModel> getSupportedModelClass() {
058        return IncludeModel.class;
059    }
060
061    @Override
062    public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
063        IncludeModel includeModel = (IncludeModel) model;
064        Model modelFromIncludedFile = buildModelFromIncludedFile(mic, includeModel);
065        if (modelFromIncludedFile == null) {
066            warnIfRequired("Failed to build include model from included file");
067            return;
068        }
069        processModelFromIncludedFile(includeModel, modelFromIncludedFile);
070    }
071
072    /**
073     * This method is called by logback-tyler at TylerConfigurator run-time.
074     *
075     * @param capc
076     * @param includeModel
077     * @throws ModelHandlerException
078     * @since 1.5.11
079     */
080    public Model buildModelFromIncludedFile(ContextAwarePropertyContainer capc, IncludeModel includeModel) throws ModelHandlerException {
081
082        this.optional = OptionHelper.toBoolean(includeModel.getOptional(), false);
083
084        if (!checkAttributes(includeModel)) {
085            inError = true;
086            return null;
087        }
088
089        InputStream in = getInputStream(capc, includeModel);
090        if (in == null) {
091            inError = true;
092            return null;
093        }
094
095        SaxEventRecorder recorder = null;
096
097        try {
098            recorder = populateSaxEventRecorder(in);
099
100            List<SaxEvent> saxEvents = recorder.getSaxEventList();
101            if (saxEvents.isEmpty()) {
102                addWarn("Empty sax event list");
103                return null;
104            }
105
106            Supplier<? extends GenericXMLConfigurator> jcSupplier = capc.getConfiguratorSupplier();
107            if (jcSupplier == null) {
108                addError("null configurator supplier. Abandoning inclusion of [" + attributeInUse + "]");
109                inError = true;
110                return null;
111            }
112
113            GenericXMLConfigurator genericXMLConfigurator = jcSupplier.get();
114            genericXMLConfigurator.getRuleStore().addPathPathMapping(INCLUDED_TAG, CONFIGURATION_TAG);
115
116            Model modelFromIncludedFile = genericXMLConfigurator.buildModelFromSaxEventList(recorder.getSaxEventList());
117            return modelFromIncludedFile;
118        } catch (JoranException e) {
119            inError = true;
120            addError("Error processing XML data in [" + attributeInUse + "]", e);
121            return null;
122        }
123    }
124
125    private void processModelFromIncludedFile(IncludeModel includeModel, Model modelFromIncludedFile) {
126        includeModel.getSubModels().addAll(modelFromIncludedFile.getSubModels());
127    }
128
129    public SaxEventRecorder populateSaxEventRecorder(final InputStream inputStream) throws JoranException {
130        SaxEventRecorder recorder = new SaxEventRecorder(context);
131        recorder.recordEvents(inputStream);
132        return recorder;
133    }
134
135    private InputStream getInputStream(ContextAwarePropertyContainer capc, IncludeModel includeModel) {
136        URL inputURL = getInputURL(capc, includeModel);
137        if (inputURL == null)
138            return null;
139        ConfigurationWatchListUtil.addToWatchList(context, inputURL);
140        return openURL(inputURL);
141    }
142
143}