001package ch.qos.logback.core.model.processor;
002
003import static ch.qos.logback.core.joran.action.Action.CLASS_ATTRIBUTE;
004
005import java.util.Map;
006
007import ch.qos.logback.core.Context;
008import ch.qos.logback.core.CoreConstants;
009import ch.qos.logback.core.boolex.EventEvaluator;
010import ch.qos.logback.core.joran.spi.DefaultNestedComponentRegistry;
011import ch.qos.logback.core.model.EventEvaluatorModel;
012import ch.qos.logback.core.model.Model;
013import ch.qos.logback.core.spi.LifeCycle;
014import ch.qos.logback.core.util.OptionHelper;
015
016public class EventEvaluatorModelHandler extends ModelHandlerBase {
017
018    EventEvaluator<?> evaluator;
019    boolean inError = false;
020
021    public EventEvaluatorModelHandler(Context context) {
022        super(context);
023    }
024
025    static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
026        return new EventEvaluatorModelHandler(context);
027    }
028
029    @Override
030    protected Class<EventEvaluatorModel> getSupportedModelClass() {
031        return EventEvaluatorModel.class;
032    }
033
034    @Override
035    public void handle(ModelInterpretationContext intercon, Model model) throws ModelHandlerException {
036        EventEvaluatorModel eem = (EventEvaluatorModel) model;
037
038        String className = eem.getClassName();
039        if (OptionHelper.isNullOrEmpty(className)) {
040            String defaultClassName = defaultClassName(intercon, eem);
041            if (OptionHelper.isNullOrEmpty(defaultClassName)) {
042                inError = true;
043                addError("Mandatory \"" + CLASS_ATTRIBUTE + "\" attribute missing for <evaluator>");
044                addError("No default classname could be found.");
045                return;
046            } else {
047                addInfo("Assuming default evaluator class [" + defaultClassName + "]");
048                className = defaultClassName;
049            }
050        } else {
051            className = intercon.getImport(className);
052        }
053
054        String evaluatorName = intercon.subst(eem.getName());
055        try {
056            evaluator = (EventEvaluator<?>) OptionHelper.instantiateByClassName(className,
057                    ch.qos.logback.core.boolex.EventEvaluator.class, context);
058            evaluator.setContext(this.context);
059            evaluator.setName(evaluatorName);
060            intercon.pushObject(evaluator);
061
062        } catch (Exception oops) {
063            inError = true;
064            addError("Could not create evaluator of type " + className + "].", oops);
065        }
066
067    }
068
069    private String defaultClassName(ModelInterpretationContext mic, EventEvaluatorModel model) {
070        DefaultNestedComponentRegistry registry = mic.getDefaultNestedComponentRegistry();
071        return registry.findDefaultComponentTypeByTag(model.getTag());
072    }
073
074    @Override
075    public void postHandle(ModelInterpretationContext intercon, Model model) throws ModelHandlerException {
076        if (inError) {
077            return;
078        }
079
080        if (evaluator instanceof LifeCycle) {
081            ((LifeCycle) evaluator).start();
082            addInfo("Starting evaluator named [" + evaluator.getName() + "]");
083        }
084
085        Object o = intercon.peekObject();
086
087        if (o != evaluator) {
088            addWarn("The object on the top the of the stack is not the evaluator pushed earlier.");
089        } else {
090            intercon.popObject();
091
092            try {
093                @SuppressWarnings("unchecked")
094                Map<String, EventEvaluator<?>> evaluatorMap = (Map<String, EventEvaluator<?>>) context
095                        .getObject(CoreConstants.EVALUATOR_MAP);
096                if (evaluatorMap == null) {
097                    addError("Could not find EvaluatorMap");
098                } else {
099                    evaluatorMap.put(evaluator.getName(), evaluator);
100                }
101            } catch (Exception ex) {
102                addError("Could not set evaluator named [" + evaluator + "].", ex);
103            }
104        }
105    }
106
107}