View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2023, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  
15  package ch.qos.logback.core.joran;
16  
17  import ch.qos.logback.core.Context;
18  import ch.qos.logback.core.model.DefineModel;
19  import ch.qos.logback.core.model.EventEvaluatorModel;
20  import ch.qos.logback.core.model.ImplicitModel;
21  import ch.qos.logback.core.model.ImportModel;
22  import ch.qos.logback.core.model.IncludeModel;
23  import ch.qos.logback.core.model.ParamModel;
24  import ch.qos.logback.core.model.PropertyModel;
25  import ch.qos.logback.core.model.SequenceNumberGeneratorModel;
26  import ch.qos.logback.core.model.SerializeModelModel;
27  import ch.qos.logback.core.model.ShutdownHookModel;
28  import ch.qos.logback.core.model.SiftModel;
29  import ch.qos.logback.core.model.StatusListenerModel;
30  import ch.qos.logback.core.model.TimestampModel;
31  import ch.qos.logback.core.model.conditional.ElseModel;
32  import ch.qos.logback.core.model.conditional.IfModel;
33  import ch.qos.logback.core.model.conditional.ThenModel;
34  import ch.qos.logback.core.model.processor.DefaultProcessor;
35  import ch.qos.logback.core.model.processor.DefineModelHandler;
36  import ch.qos.logback.core.model.processor.EventEvaluatorModelHandler;
37  import ch.qos.logback.core.model.processor.ImplicitModelHandler;
38  import ch.qos.logback.core.model.processor.ImportModelHandler;
39  import ch.qos.logback.core.model.processor.IncludeModelHandler;
40  import ch.qos.logback.core.model.processor.NOPModelHandler;
41  import ch.qos.logback.core.model.processor.PropertyModelHandler;
42  import ch.qos.logback.core.model.processor.SequenceNumberGeneratorModelHandler;
43  import ch.qos.logback.core.model.processor.SerializeModelModelHandler;
44  import ch.qos.logback.core.model.processor.ShutdownHookModelHandler;
45  import ch.qos.logback.core.model.processor.StatusListenerModelHandler;
46  import ch.qos.logback.core.model.processor.TimestampModelHandler;
47  import ch.qos.logback.core.model.processor.conditional.ElseModelHandler;
48  import ch.qos.logback.core.model.processor.conditional.IfModelHandler;
49  import ch.qos.logback.core.model.processor.conditional.ThenModelHandler;
50  import ch.qos.logback.core.sift.SiftModelHandler;
51  
52  /**
53   * For a given DefaultProcessor instance link a {@link ch.qos.logback.core.model.Model Model} class to
54   * a {@link ch.qos.logback.core.model.processor.ModelHandlerBase ModelHandler} instance in logback-core.
55   *
56   * <p>Derived classes are likely to add further links.</p>
57   *
58   * @since 1.3.9/1.4.9
59   */
60  public class ModelClassToModelHandlerLinkerBase {
61  
62  
63      protected Context context;
64  
65      public ModelClassToModelHandlerLinkerBase(Context context) {
66          this.context = context;
67      }
68  
69      public void link(DefaultProcessor defaultProcessor) {
70          defaultProcessor.addHandler(ImportModel.class, ImportModelHandler::makeInstance);
71  
72          defaultProcessor.addHandler(ShutdownHookModel.class, ShutdownHookModelHandler::makeInstance);
73          defaultProcessor.addHandler(SequenceNumberGeneratorModel.class, SequenceNumberGeneratorModelHandler::makeInstance);
74          defaultProcessor.addHandler(SerializeModelModel.class, SerializeModelModelHandler::makeInstance);
75  
76          defaultProcessor.addHandler(EventEvaluatorModel.class, EventEvaluatorModelHandler::makeInstance);
77          defaultProcessor.addHandler(DefineModel.class, DefineModelHandler::makeInstance);
78          defaultProcessor.addHandler(IncludeModel.class, IncludeModelHandler::makeInstance);
79  
80  
81          defaultProcessor.addHandler(ParamModel.class, ParamModelHandler::makeInstance);
82          defaultProcessor.addHandler(PropertyModel.class, PropertyModelHandler::makeInstance);
83          defaultProcessor.addHandler(TimestampModel.class, TimestampModelHandler::makeInstance);
84          defaultProcessor.addHandler(StatusListenerModel.class, StatusListenerModelHandler::makeInstance);
85          defaultProcessor.addHandler(ImplicitModel.class, ImplicitModelHandler::makeInstance);
86  
87          defaultProcessor.addHandler(IfModel.class, IfModelHandler::makeInstance);
88          defaultProcessor.addHandler(ThenModel.class, ThenModelHandler::makeInstance);
89          defaultProcessor.addHandler(ElseModel.class, ElseModelHandler::makeInstance);
90  
91          defaultProcessor.addHandler(SiftModel.class, SiftModelHandler::makeInstance);
92      }
93  
94      // The final filters in the two filter chain are rather crucial.
95      // They ensure that only Models attached to the firstPhaseFilter will
96      // be handled in the first phase and all models not previously handled
97      // in the second phase will be handled in a catch-all fallback case.
98      protected void sealModelFilters(DefaultProcessor defaultProcessor) {
99          defaultProcessor.getPhaseOneFilter().denyAll();
100         defaultProcessor.getPhaseTwoFilter().allowAll();
101     }
102 }