1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2024, 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.model.processor;
16  
17  import ch.qos.logback.core.Context;
18  import ch.qos.logback.core.CoreConstants;
19  import ch.qos.logback.core.model.ConversionRuleModel;
20  import ch.qos.logback.core.model.Model;
21  import ch.qos.logback.core.pattern.DynamicConverter;
22  import ch.qos.logback.core.pattern.color.ConverterSupplierByClassName;
23  import ch.qos.logback.core.util.OptionHelper;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  import java.util.function.Supplier;
28  
29  public class ConversionRuleModelHandler extends ModelHandlerBase {
30  
31      private boolean inError;
32  
33      public ConversionRuleModelHandler(Context context) {
34          super(context);
35      }
36  
37      static public ConversionRuleModelHandler makeInstance(Context context, ModelInterpretationContext mic) {
38          return new ConversionRuleModelHandler(context);
39      }
40  
41      @Override
42      public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
43  
44          ConversionRuleModel conversionRuleModel = (ConversionRuleModel) model;
45          String converterClass = conversionRuleModel.getClassName();
46  
47          if (OptionHelper.isNullOrEmptyOrAllSpaces(converterClass)) {
48              addWarn("Missing className. This should have been caught earlier.");
49              inError = true;
50              return;
51          } else {
52              converterClass = mic.getImport(converterClass);
53          }
54  
55          String conversionWord = conversionRuleModel.getConversionWord();
56  
57  
58          try {
59              Map<String, Supplier<DynamicConverter>> ruleRegistry = (Map<String, Supplier<DynamicConverter>>) context
60                      .getObject(CoreConstants.PATTERN_RULE_REGISTRY_FOR_SUPPLIERS);
61              if (ruleRegistry == null) {
62                  ruleRegistry = new HashMap<>();
63                  context.putObject(CoreConstants.PATTERN_RULE_REGISTRY_FOR_SUPPLIERS, ruleRegistry);
64              }
65              // put the new rule into the rule registry
66              addInfo("registering conversion word " + conversionWord + " with class [" + converterClass + "]");
67              ConverterSupplierByClassName converterSupplierByClassName = new ConverterSupplierByClassName(conversionWord, converterClass);
68              converterSupplierByClassName.setContext(getContext());
69              ruleRegistry.put(conversionWord, converterSupplierByClassName);
70          } catch (Exception oops) {
71              inError = true;
72              String errorMsg = "Could not add conversion rule to PatternLayout.";
73              addError(errorMsg);
74          }
75  
76  
77  
78      }
79  }