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.classic.joran;
16  
17  import ch.qos.logback.classic.model.ConfigurationModel;
18  import ch.qos.logback.classic.model.ContextNameModel;
19  import ch.qos.logback.classic.model.LevelModel;
20  import ch.qos.logback.classic.model.LoggerContextListenerModel;
21  import ch.qos.logback.classic.model.LoggerModel;
22  import ch.qos.logback.classic.model.RootLoggerModel;
23  import ch.qos.logback.classic.model.processor.ConfigurationModelHandler;
24  import ch.qos.logback.classic.model.processor.ContextNameModelHandler;
25  import ch.qos.logback.classic.model.processor.LevelModelHandler;
26  import ch.qos.logback.classic.model.processor.LoggerContextListenerModelHandler;
27  import ch.qos.logback.classic.model.processor.LoggerModelHandler;
28  import ch.qos.logback.classic.model.processor.RootLoggerModelHandler;
29  import ch.qos.logback.core.Context;
30  import ch.qos.logback.core.joran.ModelClassToModelHandlerLinkerBase;
31  import ch.qos.logback.core.model.AppenderModel;
32  import ch.qos.logback.core.model.AppenderRefModel;
33  import ch.qos.logback.core.model.InsertFromJNDIModel;
34  import ch.qos.logback.core.model.ModelHandlerFactoryMethod;
35  import ch.qos.logback.core.model.processor.AppenderModelHandler;
36  import ch.qos.logback.core.model.processor.AppenderRefDependencyAnalyser;
37  import ch.qos.logback.core.model.processor.AppenderRefModelHandler;
38  import ch.qos.logback.core.model.processor.DefaultProcessor;
39  import ch.qos.logback.core.model.processor.InsertFromJNDIModelHandler;
40  import ch.qos.logback.core.model.processor.RefContainerDependencyAnalyser;
41  
42  /**
43   * For a given DefaultProcessor instance link a {@link ch.qos.logback.core.model.Model Model} class to a
44   * {@link ch.qos.logback.core.model.processor.ModelHandlerBase ModelHandler} instance for
45   * logback-classic.
46   *
47   * <p>Will also use links from super class.</p>
48   *
49   * @since 1.3.9/1.4.9
50   */
51  public class ModelClassToModelHandlerLinker extends ModelClassToModelHandlerLinkerBase {
52  
53      public ModelClassToModelHandlerLinker(Context context) {
54          super(context);
55      }
56  
57      ModelHandlerFactoryMethod configurationModelHandlerFactoryMethod;
58  
59      @Override
60      public void link(DefaultProcessor defaultProcessor) {
61          super.link(defaultProcessor);
62          defaultProcessor.addHandler(ConfigurationModel.class, getConfigurationModelHandlerFactoryMethod());
63          defaultProcessor.addHandler(ContextNameModel.class, ContextNameModelHandler::makeInstance);
64          defaultProcessor.addHandler(LoggerContextListenerModel.class, LoggerContextListenerModelHandler::makeInstance);
65  
66          defaultProcessor.addHandler(InsertFromJNDIModel.class, InsertFromJNDIModelHandler::makeInstance);
67  
68          defaultProcessor.addHandler(AppenderModel.class, AppenderModelHandler::makeInstance);
69          defaultProcessor.addHandler(AppenderRefModel.class, AppenderRefModelHandler::makeInstance);
70          defaultProcessor.addHandler(RootLoggerModel.class, RootLoggerModelHandler::makeInstance);
71          defaultProcessor.addHandler(LoggerModel.class, LoggerModelHandler::makeInstance);
72          defaultProcessor.addHandler(LevelModel.class, LevelModelHandler::makeInstance);
73  
74          defaultProcessor.addAnalyser(LoggerModel.class,
75                  () -> new RefContainerDependencyAnalyser(context, LoggerModel.class));
76  
77          defaultProcessor.addAnalyser(RootLoggerModel.class,
78                  () -> new RefContainerDependencyAnalyser(context, RootLoggerModel.class));
79  
80          defaultProcessor.addAnalyser(AppenderModel.class,
81                  () -> new RefContainerDependencyAnalyser(context, AppenderModel.class));
82  
83          defaultProcessor.addAnalyser(AppenderRefModel.class, () -> new AppenderRefDependencyAnalyser(context));
84  
85          sealModelFilters(defaultProcessor);
86  
87      }
88  
89      public ModelHandlerFactoryMethod getConfigurationModelHandlerFactoryMethod() {
90          if(configurationModelHandlerFactoryMethod == null) {
91              //System.out.println("returning default ConfigurationModelHandler::makeInstance;");
92              return  ConfigurationModelHandler::makeInstance;
93          } else {
94              //System.out.println("returning set "+configurationModelHandlerFactoryMethod);
95              return configurationModelHandlerFactoryMethod;
96          }
97      }
98  
99  
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 }