001package ch.qos.logback.core.model.processor; 002 003import java.util.Map; 004 005import ch.qos.logback.core.Appender; 006import ch.qos.logback.core.Context; 007import ch.qos.logback.core.joran.JoranConstants; 008import ch.qos.logback.core.model.AppenderRefModel; 009import ch.qos.logback.core.model.Model; 010import ch.qos.logback.core.spi.AppenderAttachable; 011 012public class AppenderRefModelHandler extends ModelHandlerBase { 013 boolean inError = false; 014 015 public AppenderRefModelHandler(Context context) { 016 super(context); 017 } 018 019 static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) { 020 return new AppenderRefModelHandler(context); 021 } 022 023 @Override 024 protected Class<? extends AppenderRefModel> getSupportedModelClass() { 025 return AppenderRefModel.class; 026 } 027 028 @Override 029 public void handle(ModelInterpretationContext interpContext, Model model) throws ModelHandlerException { 030 031 Object o = interpContext.peekObject(); 032 033 if (!(o instanceof AppenderAttachable)) { 034 inError = true; 035 String errMsg = "Could not find an AppenderAttachable at the top of execution stack. Near " 036 + model.idString(); 037 addError(errMsg); 038 return; 039 } 040 041 AppenderRefModel appenderRefModel = (AppenderRefModel) model; 042 AppenderAttachable<?> appenderAttachable = (AppenderAttachable<?>) o; 043 044 attachRefencedAppenders(interpContext, appenderRefModel, appenderAttachable); 045 046 } 047 048 @SuppressWarnings({ "unchecked", "rawtypes" }) 049 void attachRefencedAppenders(ModelInterpretationContext mic, AppenderRefModel appenderRefModel, 050 AppenderAttachable<?> appenderAttachable) { 051 String appenderName = mic.subst(appenderRefModel.getRef()); 052 053 Map<String, Appender> appenderBag = (Map<String, Appender>) mic.getObjectMap().get(JoranConstants.APPENDER_BAG); 054 055 Appender appender = appenderBag.get(appenderName); 056 if (appender == null) { 057 addError("Failed to find appender named [" + appenderName + "]"); 058 } else { 059 addInfo("Attaching appender named [" + appenderName + "] to " + appenderAttachable); 060 appenderAttachable.addAppender(appender); 061 } 062 063 } 064}