001package ch.qos.logback.classic.model.processor;
002
003import ch.qos.logback.classic.model.ContextNameModel;
004import ch.qos.logback.core.Context;
005import ch.qos.logback.core.model.Model;
006import ch.qos.logback.core.model.processor.ModelHandlerBase;
007import ch.qos.logback.core.model.processor.ModelHandlerException;
008import ch.qos.logback.core.model.processor.ModelInterpretationContext;
009
010public class ContextNameModelHandler extends ModelHandlerBase {
011
012    public ContextNameModelHandler(Context context) {
013        super(context);
014    }
015
016    static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
017        return new ContextNameModelHandler(context);
018    }
019
020    @Override
021    protected Class<ContextNameModel> getSupportedModelClass() {
022        return ContextNameModel.class;
023    }
024
025    @Override
026    public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
027        ContextNameModel contextNameModel = (ContextNameModel) model;
028
029        String finalBody = mic.subst(contextNameModel.getBodyText());
030        addInfo("Setting logger context name as [" + finalBody + "]");
031        try {
032            context.setName(finalBody);
033        } catch (IllegalStateException e) {
034            addError("Failed to rename context [" + context.getName() + "] as [" + finalBody + "]", e);
035        }
036
037    }
038
039}