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.model.SequenceNumberGeneratorModel; 019import ch.qos.logback.core.spi.BasicSequenceNumberGenerator; 020import ch.qos.logback.core.spi.SequenceNumberGenerator; 021import ch.qos.logback.core.util.OptionHelper; 022 023public class SequenceNumberGeneratorModelHandler extends ModelHandlerBase { 024 025 SequenceNumberGenerator sequenceNumberGenerator; 026 private boolean inError; 027 028 public SequenceNumberGeneratorModelHandler(Context context) { 029 super(context); 030 } 031 032 static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) { 033 return new SequenceNumberGeneratorModelHandler(context); 034 } 035 036 @Override 037 protected Class<SequenceNumberGeneratorModel> getSupportedModelClass() { 038 return SequenceNumberGeneratorModel.class; 039 } 040 041 @Override 042 public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException { 043 044 SequenceNumberGeneratorModel sequenceNumberGeneratorModel = (SequenceNumberGeneratorModel) model; 045 String className = sequenceNumberGeneratorModel.getClassName(); 046 if (OptionHelper.isNullOrEmpty(className)) { 047 addWarn("Missing className. This should have been caught earlier."); 048 inError = true; 049 return; 050 } else { 051 className = mic.getImport(className); 052 } 053 054 try { 055 addInfo("About to instantiate SequenceNumberGenerator of type [" + className + "]"); 056 057 sequenceNumberGenerator = (SequenceNumberGenerator) OptionHelper.instantiateByClassName(className, 058 SequenceNumberGenerator.class, context); 059 sequenceNumberGenerator.setContext(context); 060 061 mic.pushObject(sequenceNumberGenerator); 062 } catch (Exception e) { 063 inError = true; 064 addError("Could not create a SequenceNumberGenerator of type [" + className + "].", e); 065 throw new ModelHandlerException(e); 066 } 067 } 068 069 public void postHandle(ModelInterpretationContext mic, Model model) throws ModelHandlerException { 070 if (inError) { 071 return; 072 } 073 074 Object o = mic.peekObject(); 075 if (o != sequenceNumberGenerator) { 076 addWarn("The object at the of the stack is not the hook pushed earlier."); 077 } else { 078 mic.popObject(); 079 080 addInfo("Registering "+o+" with context."); 081 context.setSequenceNumberGenerator(sequenceNumberGenerator); 082 } 083 } 084 085}