001package ch.qos.logback.core.joran;
002
003import ch.qos.logback.core.Context;
004import ch.qos.logback.core.joran.util.PropertySetter;
005import ch.qos.logback.core.joran.util.beans.BeanDescriptionCache;
006import ch.qos.logback.core.model.Model;
007import ch.qos.logback.core.model.ParamModel;
008import ch.qos.logback.core.model.processor.ModelHandlerBase;
009import ch.qos.logback.core.model.processor.ModelHandlerException;
010import ch.qos.logback.core.model.processor.ModelInterpretationContext;
011
012public class ParamModelHandler extends ModelHandlerBase {
013
014    private final BeanDescriptionCache beanDescriptionCache;
015
016    public ParamModelHandler(Context context, BeanDescriptionCache beanDescriptionCache) {
017        super(context);
018        this.beanDescriptionCache = beanDescriptionCache;
019    }
020
021    static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
022        return new ParamModelHandler(context, ic.getBeanDescriptionCache());
023    }
024
025    @Override
026    protected Class<ParamModel> getSupportedModelClass() {
027        return ParamModel.class;
028    }
029
030    @Override
031    public void handle(ModelInterpretationContext intercon, Model model) throws ModelHandlerException {
032
033        ParamModel paramModel = (ParamModel) model;
034
035        String valueStr = intercon.subst(paramModel.getValue());
036
037        Object o = intercon.peekObject();
038
039        PropertySetter propSetter = new PropertySetter(beanDescriptionCache, o);
040        propSetter.setContext(context);
041
042        // allow for variable substitution for name as well
043        String finalName = intercon.subst(paramModel.getName());
044        propSetter.setProperty(finalName, valueStr);
045    }
046
047}