001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, 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.classic.sift;
015
016import java.util.Collection;
017import java.util.HashMap;
018import java.util.Map;
019
020import ch.qos.logback.classic.spi.ILoggingEvent;
021import ch.qos.logback.classic.util.DefaultNestedComponentRules;
022import ch.qos.logback.core.Appender;
023import ch.qos.logback.core.joran.action.ActionConst;
024import ch.qos.logback.core.joran.action.AppenderAction;
025import ch.qos.logback.core.joran.spi.DefaultNestedComponentRegistry;
026import ch.qos.logback.core.joran.spi.ElementPath;
027import ch.qos.logback.core.joran.spi.ElementSelector;
028import ch.qos.logback.core.joran.spi.RuleStore;
029import ch.qos.logback.core.sift.SiftingJoranConfiguratorBase;
030
031public class SiftingJoranConfigurator extends SiftingJoranConfiguratorBase<ILoggingEvent> {
032
033    SiftingJoranConfigurator(String key, String value, Map<String, String> parentPropertyMap) {
034        super(key, value, parentPropertyMap);
035    }
036
037    @Override
038    protected ElementPath initialElementPath() {
039        return new ElementPath("configuration");
040    }
041
042    @Override
043    protected void addInstanceRules(RuleStore rs) {
044        super.addInstanceRules(rs);
045        rs.addRule(new ElementSelector("configuration/appender"), new AppenderAction<ILoggingEvent>());
046    }
047
048    @Override
049    protected void addDefaultNestedComponentRegistryRules(DefaultNestedComponentRegistry registry) {
050        DefaultNestedComponentRules.addDefaultNestedComponentRegistryRules(registry);
051    }
052
053    @Override
054    protected void buildInterpreter() {
055        super.buildInterpreter();
056        Map<String, Object> omap = interpreter.getInterpretationContext().getObjectMap();
057        omap.put(ActionConst.APPENDER_BAG, new HashMap<String, Appender<?>>());
058        //omap.put(ActionConst.FILTER_CHAIN_BAG, new HashMap());
059        Map<String, String> propertiesMap = new HashMap<String, String>();
060        propertiesMap.putAll(parentPropertyMap);
061        propertiesMap.put(key, value);
062        interpreter.setInterpretationContextPropertiesMap(propertiesMap);
063    }
064
065    @SuppressWarnings("unchecked")
066    public Appender<ILoggingEvent> getAppender() {
067        Map<String, Object> omap = interpreter.getInterpretationContext().getObjectMap();
068        HashMap<String, Appender<?>> appenderMap = (HashMap<String, Appender<?>>) omap.get(ActionConst.APPENDER_BAG);
069        oneAndOnlyOneCheck(appenderMap);
070        Collection<Appender<?>> values = appenderMap.values();
071        if (values.size() == 0) {
072            return null;
073        }
074        return (Appender<ILoggingEvent>) values.iterator().next();
075    }
076}