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.joran; 015 016import ch.qos.logback.core.joran.action.*; 017import ch.qos.logback.core.joran.conditional.ElseAction; 018import ch.qos.logback.core.joran.conditional.IfAction; 019import ch.qos.logback.core.joran.conditional.ThenAction; 020import ch.qos.logback.core.joran.spi.ElementSelector; 021import ch.qos.logback.core.joran.spi.RuleStore; 022import ch.qos.logback.core.joran.spi.SaxEventInterpretationContext; 023import ch.qos.logback.core.joran.spi.SaxEventInterpreter; 024import ch.qos.logback.core.model.*; 025import ch.qos.logback.core.model.conditional.ElseModel; 026import ch.qos.logback.core.model.conditional.IfModel; 027import ch.qos.logback.core.model.conditional.ThenModel; 028import ch.qos.logback.core.model.processor.*; 029import ch.qos.logback.core.model.processor.conditional.ElseModelHandler; 030import ch.qos.logback.core.model.processor.conditional.IfModelHandler; 031import ch.qos.logback.core.model.processor.conditional.ThenModelHandler; 032import ch.qos.logback.core.sift.SiftModelHandler; 033 034// Based on 310985 revision 310985 as attested by http://tinyurl.com/8njps 035// see also http://tinyurl.com/c2rp5 036 037/** 038 * A JoranConfiguratorBase lays most of the groundwork for concrete 039 * configurators derived from it. Concrete configurators only need to implement 040 * the {@link #addElementSelectorAndActionAssociations} method. 041 * <p> 042 * A JoranConfiguratorBase instance should not be used more than once to 043 * configure a Context. 044 * 045 * @author Ceki Gülcü 046 */ 047abstract public class JoranConfiguratorBase<E> extends GenericXMLConfigurator { 048 049 @Override 050 protected void addElementSelectorAndActionAssociations(RuleStore rs) { 051 052 // is "*/variable" referenced in the docs? 053 rs.addRule(new ElementSelector("*/variable"), PropertyAction::new); 054 rs.addRule(new ElementSelector("*/property"), PropertyAction::new); 055 // substitutionProperty is deprecated 056 rs.addRule(new ElementSelector("*/substitutionProperty"), PropertyAction::new); 057 058 rs.addRule(new ElementSelector("configuration/import"), ImportAction::new); 059 060 061 rs.addRule(new ElementSelector("configuration/timestamp"), TimestampAction::new); 062 rs.addRule(new ElementSelector("configuration/shutdownHook"), ShutdownHookAction::new); 063 rs.addRule(new ElementSelector("configuration/sequenceNumberGenerator"), SequenceNumberGeneratorAction::new); 064 065 rs.addRule(new ElementSelector("configuration/define"), DefinePropertyAction::new); 066 rs.addRule(new ElementSelector("configuration/evaluator"), EventEvaluatorAction::new); 067 068 // the contextProperty pattern is deprecated. It is undocumented 069 // and will be dropped in future versions of logback 070 rs.addRule(new ElementSelector("configuration/contextProperty"), ContextPropertyAction::new); 071 072 rs.addRule(new ElementSelector("configuration/conversionRule"), ConversionRuleAction::new); 073 074 rs.addRule(new ElementSelector("configuration/statusListener"), StatusListenerAction::new); 075 076 rs.addRule(new ElementSelector("*/appender"), AppenderAction::new); 077 rs.addRule(new ElementSelector("configuration/appender/appender-ref"), AppenderRefAction::new); 078 rs.addRule(new ElementSelector("configuration/newRule"), NewRuleAction::new); 079 080 rs.addRule(new ElementSelector("*/param"), ParamAction::new); 081 082 // add if-then-else support 083 rs.addRule(new ElementSelector("*/if"), IfAction::new); 084 rs.addTransparentPathPart("if"); 085 rs.addRule(new ElementSelector("*/if/then"), ThenAction::new); 086 rs.addTransparentPathPart("then"); 087 rs.addRule(new ElementSelector("*/if/else"), ElseAction::new); 088 rs.addTransparentPathPart("else"); 089 090 rs.addRule(new ElementSelector("configuration/appender/sift"), SiftAction::new); 091 rs.addTransparentPathPart("sift"); 092 093 094 } 095 096 @Override 097 protected void setImplicitRuleSupplier(SaxEventInterpreter interpreter) { 098 interpreter.setImplicitActionSupplier( ImplicitModelAction::new ); 099 } 100 101 @Override 102 public void buildModelInterpretationContext() { 103 super.buildModelInterpretationContext(); 104 modelInterpretationContext.createAppenderBags(); 105 } 106 107 public SaxEventInterpretationContext getInterpretationContext() { 108 return saxEventInterpreter.getSaxEventInterpretationContext(); 109 } 110 111 @Override 112 protected void addModelHandlerAssociations(DefaultProcessor defaultProcessor) { 113 defaultProcessor.addHandler(ImportModel.class, ImportModelHandler::makeInstance); 114 115 defaultProcessor.addHandler(ShutdownHookModel.class, ShutdownHookModelHandler::makeInstance); 116 defaultProcessor.addHandler(SequenceNumberGeneratorModel.class, SequenceNumberGeneratorModelHandler::makeInstance); 117 118 defaultProcessor.addHandler(EventEvaluatorModel.class, EventEvaluatorModelHandler::makeInstance); 119 defaultProcessor.addHandler(DefineModel.class, DefineModelHandler::makeInstance); 120 defaultProcessor.addHandler(IncludeModel.class, NOPModelHandler::makeInstance); 121 122 123 defaultProcessor.addHandler(ParamModel.class, ParamModelHandler::makeInstance); 124 defaultProcessor.addHandler(PropertyModel.class, PropertyModelHandler::makeInstance); 125 defaultProcessor.addHandler(TimestampModel.class, TimestampModelHandler::makeInstance); 126 defaultProcessor.addHandler(StatusListenerModel.class, StatusListenerModelHandler::makeInstance); 127 defaultProcessor.addHandler(ImplicitModel.class, ImplicitModelHandler::makeInstance); 128 129 defaultProcessor.addHandler(IfModel.class, IfModelHandler::makeInstance); 130 defaultProcessor.addHandler(ThenModel.class, ThenModelHandler::makeInstance); 131 defaultProcessor.addHandler(ElseModel.class, ElseModelHandler::makeInstance); 132 133 defaultProcessor.addHandler(SiftModel.class, SiftModelHandler::makeInstance); 134 135 } 136 137}