001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2023, 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.joran;
016
017import ch.qos.logback.classic.model.*;
018import ch.qos.logback.classic.model.processor.*;
019import ch.qos.logback.core.Context;
020import ch.qos.logback.core.joran.ModelClassToModelHandlerLinkerBase;
021import ch.qos.logback.core.model.AppenderModel;
022import ch.qos.logback.core.model.AppenderRefModel;
023import ch.qos.logback.core.model.InsertFromJNDIModel;
024import ch.qos.logback.core.model.ModelHandlerFactoryMethod;
025import ch.qos.logback.core.model.processor.AppenderModelHandler;
026import ch.qos.logback.core.model.processor.AppenderRefDependencyAnalyser;
027import ch.qos.logback.core.model.processor.AppenderRefModelHandler;
028import ch.qos.logback.core.model.processor.DefaultProcessor;
029import ch.qos.logback.core.model.processor.InsertFromJNDIModelHandler;
030import ch.qos.logback.core.model.processor.RefContainerDependencyAnalyser;
031
032/**
033 * For a given DefaultProcessor instance link a {@link ch.qos.logback.core.model.Model Model} class to a
034 * {@link ch.qos.logback.core.model.processor.ModelHandlerBase ModelHandler} instance for
035 * logback-classic.
036 *
037 * <p>Will also use links from super class.</p>
038 *
039 * @since 1.3.9/1.4.9
040 */
041public class ModelClassToModelHandlerLinker extends ModelClassToModelHandlerLinkerBase {
042
043    public ModelClassToModelHandlerLinker(Context context) {
044        super(context);
045    }
046
047    ModelHandlerFactoryMethod configurationModelHandlerFactoryMethod;
048
049    @Override
050    public void link(DefaultProcessor defaultProcessor) {
051        super.link(defaultProcessor);
052        defaultProcessor.addHandler(ConfigurationModel.class, getConfigurationModelHandlerFactoryMethod());
053        defaultProcessor.addHandler(ContextNameModel.class, ContextNameModelHandler::makeInstance);
054        defaultProcessor.addHandler(LoggerContextListenerModel.class, LoggerContextListenerModelHandler::makeInstance);
055
056        defaultProcessor.addHandler(PropertiesConfiguratorModel.class, PropertiesConfiguratorModelHandler::makeInstance);
057        defaultProcessor.addHandler(InsertFromJNDIModel.class, InsertFromJNDIModelHandler::makeInstance);
058
059        defaultProcessor.addHandler(AppenderModel.class, AppenderModelHandler::makeInstance);
060        defaultProcessor.addHandler(AppenderRefModel.class, AppenderRefModelHandler::makeInstance);
061        defaultProcessor.addHandler(RootLoggerModel.class, RootLoggerModelHandler::makeInstance);
062        defaultProcessor.addHandler(LoggerModel.class, LoggerModelHandler::makeInstance);
063        defaultProcessor.addHandler(LevelModel.class, LevelModelHandler::makeInstance);
064
065        defaultProcessor.addAnalyser(LoggerModel.class,
066                () -> new RefContainerDependencyAnalyser(context, LoggerModel.class));
067
068        defaultProcessor.addAnalyser(RootLoggerModel.class,
069                () -> new RefContainerDependencyAnalyser(context, RootLoggerModel.class));
070
071        defaultProcessor.addAnalyser(AppenderModel.class,
072                () -> new RefContainerDependencyAnalyser(context, AppenderModel.class));
073
074        defaultProcessor.addAnalyser(AppenderRefModel.class, () -> new AppenderRefDependencyAnalyser(context));
075
076        sealModelFilters(defaultProcessor);
077
078    }
079
080    public ModelHandlerFactoryMethod getConfigurationModelHandlerFactoryMethod() {
081        if(configurationModelHandlerFactoryMethod == null) {
082            //System.out.println("returning default ConfigurationModelHandler::makeInstance;");
083            return  ConfigurationModelHandler::makeInstance;
084        } else {
085            //System.out.println("returning set "+configurationModelHandlerFactoryMethod);
086            return configurationModelHandlerFactoryMethod;
087        }
088    }
089
090
091    /**
092     * Allow configurators to override the factory method for ConfigurationModelHandler
093     *
094     */
095    public void setConfigurationModelHandlerFactoryMethod(ModelHandlerFactoryMethod cmhfm) {
096        //System.out.println("setConfigurationModelHandlerFactoryMethod called with "+cmhfm);
097        this.configurationModelHandlerFactoryMethod = cmhfm;
098    }
099
100}