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