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.spi.ContextAwareBase;
19  
20  abstract public class ModelHandlerBase extends ContextAwareBase {
21  
22      public ModelHandlerBase(Context context) {
23          setContext(context);
24      }
25  
26      /**
27       * Subclasses should return the subclass of Model that they expect to handle.
28       * 
29       * The default implementation assumes that all Model classes are supported. This
30       * a very lax assumption which is usually not true.
31       * 
32       * @return supported model class
33       * @see ModelHandlerBase#isSupportedModelType(Model)
34       */
35      protected Class<? extends Model> getSupportedModelClass() {
36          // Assume lax default where all model objects are supported
37          return Model.class;
38      }
39  
40      protected boolean isSupportedModelType(Model model) {
41          Class<? extends Model> modelClass = getSupportedModelClass();
42          if (modelClass.isInstance(model)) {
43              return true;
44          } else {
45              addError("This handler can only handle models of type [" + modelClass + "]");
46              return false;
47          }
48      }
49  
50      abstract public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException;
51  
52      public void postHandle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
53          // let specialized handlers override
54      }
55  
56      public String toString() {
57          return this.getClass().getName();
58      }
59  
60  }