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