View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2022, 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  package ch.qos.logback.core.model.processor;
15  
16  import ch.qos.logback.core.Context;
17  import ch.qos.logback.core.model.Model;
18  import ch.qos.logback.core.model.SequenceNumberGeneratorModel;
19  import ch.qos.logback.core.spi.BasicSequenceNumberGenerator;
20  import ch.qos.logback.core.spi.SequenceNumberGenerator;
21  import ch.qos.logback.core.util.OptionHelper;
22  
23  public class SequenceNumberGeneratorModelHandler extends ModelHandlerBase {
24  
25      SequenceNumberGenerator sequenceNumberGenerator;
26      private boolean inError;
27  
28      public SequenceNumberGeneratorModelHandler(Context context) {
29          super(context);
30      }
31  
32      static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
33          return new SequenceNumberGeneratorModelHandler(context);
34      }
35  
36      @Override
37      protected Class<SequenceNumberGeneratorModel> getSupportedModelClass() {
38          return SequenceNumberGeneratorModel.class;
39      }
40  
41      @Override
42      public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
43  
44          SequenceNumberGeneratorModel sequenceNumberGeneratorModel = (SequenceNumberGeneratorModel) model;
45          String className = sequenceNumberGeneratorModel.getClassName();
46          if (OptionHelper.isNullOrEmptyOrAllSpaces(className)) {
47              addWarn("Missing className. This should have been caught earlier.");
48              inError = true;
49              return;
50          } else {
51              className = mic.getImport(className);
52          }
53  
54          try {
55              addInfo("About to instantiate SequenceNumberGenerator of type [" + className + "]");
56  
57              sequenceNumberGenerator = (SequenceNumberGenerator) OptionHelper.instantiateByClassName(className,
58                      SequenceNumberGenerator.class, context);
59              sequenceNumberGenerator.setContext(context);
60  
61              mic.pushObject(sequenceNumberGenerator);
62          } catch (Exception e) {
63              inError = true;
64              addError("Could not create a SequenceNumberGenerator of type [" + className + "].", e);
65              throw new ModelHandlerException(e);
66          }
67      }
68  
69      public void postHandle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
70          if (inError) {
71              return;
72          }
73  
74          Object o = mic.peekObject();
75          if (o != sequenceNumberGenerator) {
76              addWarn("The object at the of the stack is not the hook pushed earlier.");
77          } else {
78              mic.popObject();
79  
80              addInfo("Registering "+o+" with context.");
81              context.setSequenceNumberGenerator(sequenceNumberGenerator);
82          }
83      }
84  
85  }