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.core.joran;
016
017import ch.qos.logback.core.Context;
018import ch.qos.logback.core.model.DefineModel;
019import ch.qos.logback.core.model.EventEvaluatorModel;
020import ch.qos.logback.core.model.ImplicitModel;
021import ch.qos.logback.core.model.ImportModel;
022import ch.qos.logback.core.model.IncludeModel;
023import ch.qos.logback.core.model.ParamModel;
024import ch.qos.logback.core.model.PropertyModel;
025import ch.qos.logback.core.model.SequenceNumberGeneratorModel;
026import ch.qos.logback.core.model.SerializeModelModel;
027import ch.qos.logback.core.model.ShutdownHookModel;
028import ch.qos.logback.core.model.SiftModel;
029import ch.qos.logback.core.model.StatusListenerModel;
030import ch.qos.logback.core.model.TimestampModel;
031import ch.qos.logback.core.model.conditional.ElseModel;
032import ch.qos.logback.core.model.conditional.IfModel;
033import ch.qos.logback.core.model.conditional.ThenModel;
034import ch.qos.logback.core.model.processor.DefaultProcessor;
035import ch.qos.logback.core.model.processor.DefineModelHandler;
036import ch.qos.logback.core.model.processor.EventEvaluatorModelHandler;
037import ch.qos.logback.core.model.processor.ImplicitModelHandler;
038import ch.qos.logback.core.model.processor.ImportModelHandler;
039import ch.qos.logback.core.model.processor.IncludeModelHandler;
040import ch.qos.logback.core.model.processor.NOPModelHandler;
041import ch.qos.logback.core.model.processor.PropertyModelHandler;
042import ch.qos.logback.core.model.processor.SequenceNumberGeneratorModelHandler;
043import ch.qos.logback.core.model.processor.SerializeModelModelHandler;
044import ch.qos.logback.core.model.processor.ShutdownHookModelHandler;
045import ch.qos.logback.core.model.processor.StatusListenerModelHandler;
046import ch.qos.logback.core.model.processor.TimestampModelHandler;
047import ch.qos.logback.core.model.processor.conditional.ElseModelHandler;
048import ch.qos.logback.core.model.processor.conditional.IfModelHandler;
049import ch.qos.logback.core.model.processor.conditional.ThenModelHandler;
050import ch.qos.logback.core.sift.SiftModelHandler;
051
052/**
053 * For a given DefaultProcessor instance link a {@link ch.qos.logback.core.model.Model Model} class to
054 * a {@link ch.qos.logback.core.model.processor.ModelHandlerBase ModelHandler} instance in logback-core.
055 *
056 * <p>Derived classes are likely to add further links.</p>
057 *
058 * @since 1.3.9/1.4.9
059 */
060public class ModelClassToModelHandlerLinkerBase {
061
062
063    protected Context context;
064
065    public ModelClassToModelHandlerLinkerBase(Context context) {
066        this.context = context;
067    }
068
069    public void link(DefaultProcessor defaultProcessor) {
070        defaultProcessor.addHandler(ImportModel.class, ImportModelHandler::makeInstance);
071
072        defaultProcessor.addHandler(ShutdownHookModel.class, ShutdownHookModelHandler::makeInstance);
073        defaultProcessor.addHandler(SequenceNumberGeneratorModel.class, SequenceNumberGeneratorModelHandler::makeInstance);
074        defaultProcessor.addHandler(SerializeModelModel.class, SerializeModelModelHandler::makeInstance);
075
076        defaultProcessor.addHandler(EventEvaluatorModel.class, EventEvaluatorModelHandler::makeInstance);
077        defaultProcessor.addHandler(DefineModel.class, DefineModelHandler::makeInstance);
078        defaultProcessor.addHandler(IncludeModel.class, IncludeModelHandler::makeInstance);
079
080
081        defaultProcessor.addHandler(ParamModel.class, ParamModelHandler::makeInstance);
082        defaultProcessor.addHandler(PropertyModel.class, PropertyModelHandler::makeInstance);
083        defaultProcessor.addHandler(TimestampModel.class, TimestampModelHandler::makeInstance);
084        defaultProcessor.addHandler(StatusListenerModel.class, StatusListenerModelHandler::makeInstance);
085        defaultProcessor.addHandler(ImplicitModel.class, ImplicitModelHandler::makeInstance);
086
087        defaultProcessor.addHandler(IfModel.class, IfModelHandler::makeInstance);
088        defaultProcessor.addHandler(ThenModel.class, ThenModelHandler::makeInstance);
089        defaultProcessor.addHandler(ElseModel.class, ElseModelHandler::makeInstance);
090
091        defaultProcessor.addHandler(SiftModel.class, SiftModelHandler::makeInstance);
092    }
093
094    // The final filters in the two filter chain are rather crucial.
095    // They ensure that only Models attached to the firstPhaseFilter will
096    // be handled in the first phase and all models not previously handled
097    // in the second phase will be handled in a catch-all fallback case.
098    protected void sealModelFilters(DefaultProcessor defaultProcessor) {
099        defaultProcessor.getPhaseOneFilter().denyAll();
100        defaultProcessor.getPhaseTwoFilter().allowAll();
101    }
102}