001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2022, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.model.processor;
015
016import ch.qos.logback.core.Context;
017import ch.qos.logback.core.model.Model;
018import ch.qos.logback.core.spi.ContextAwareBase;
019
020abstract public class ModelHandlerBase extends ContextAwareBase {
021
022    public ModelHandlerBase(Context context) {
023        setContext(context);
024    }
025
026    /**
027     * Subclasses should return the subclass of Model that they expect to handle.
028     * 
029     * The default implementation assumes that all Model classes are supported. This
030     * a very lax assumption which is usually not true.
031     * 
032     * @return supported model class
033     * @see ModelHandlerBase#isSupportedModelType(Model)
034     */
035    protected Class<? extends Model> getSupportedModelClass() {
036        // Assume lax default where all model objects are supported
037        return Model.class;
038    }
039
040    protected boolean isSupportedModelType(Model model) {
041        Class<? extends Model> modelClass = getSupportedModelClass();
042        if (modelClass.isInstance(model)) {
043            return true;
044        } else {
045            addError("This handler can only handle models of type [" + modelClass + "]");
046            return false;
047        }
048    }
049
050    abstract public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException;
051
052    public void postHandle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
053        // let specialized handlers override
054    }
055
056    public String toString() {
057        return this.getClass().getName();
058    }
059
060}