All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.juli.logging.ch.qos.logback.core.sift.AppenderFactoryUsingSiftModel Maven / Gradle / Ivy

There is a newer version: 8.5.100.SP1
Show newest version
/**
 * Logback: the reliable, generic, fast and flexible logging framework.
 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
 *
 * This program and the accompanying materials are dual-licensed under
 * either the terms of the Eclipse Public License v1.0 as published by
 * the Eclipse Foundation
 *
 *   or (per the licensee's choosing)
 *
 * under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation.
 */
package org.apache.juli.logging.ch.qos.logback.core.sift;

import java.util.Collection;
import java.util.Map;

import org.apache.juli.logging.ch.qos.logback.core.Appender;
import org.apache.juli.logging.ch.qos.logback.core.Context;
import org.apache.juli.logging.ch.qos.logback.core.joran.JoranConstants;
import org.apache.juli.logging.ch.qos.logback.core.joran.ParamModelHandler;
import org.apache.juli.logging.ch.qos.logback.core.joran.spi.DefaultNestedComponentRegistry;
import org.apache.juli.logging.ch.qos.logback.core.joran.spi.JoranException;
import org.apache.juli.logging.ch.qos.logback.core.model.AppenderModel;
import org.apache.juli.logging.ch.qos.logback.core.model.ImplicitModel;
import org.apache.juli.logging.ch.qos.logback.core.model.Model;
import org.apache.juli.logging.ch.qos.logback.core.model.ParamModel;
import org.apache.juli.logging.ch.qos.logback.core.model.PropertyModel;
import org.apache.juli.logging.ch.qos.logback.core.model.SiftModel;
import org.apache.juli.logging.ch.qos.logback.core.model.processor.AppenderModelHandler;
import org.apache.juli.logging.ch.qos.logback.core.model.processor.ImplicitModelHandler;
import org.apache.juli.logging.ch.qos.logback.core.model.processor.ModelInterpretationContext;
import org.apache.juli.logging.ch.qos.logback.core.model.processor.PropertyModelHandler;

/**
 * Builds new appenders dynamically by running SiftingJoranConfigurator instance,
 * a custom configurator tailored for the contents of the sift element.
 * @param 
 */
public class AppenderFactoryUsingSiftModel implements AppenderFactory {
    
    Context context;
    final Model siftModel;
    protected String discriminatingKey;
    protected ModelInterpretationContext parentMic;
    protected DefaultNestedComponentRegistry registry;
    
    public AppenderFactoryUsingSiftModel(ModelInterpretationContext parentMic, Model aSiftModel, String discriminatingKey)  {
        this.siftModel = Model.duplicate(aSiftModel);
        this.discriminatingKey = discriminatingKey;
        this.parentMic = parentMic;
        this.context = parentMic.getContext();
      
    }


    public SiftProcessor getSiftingModelProcessor(String value) {
        ModelInterpretationContext smic = new ModelInterpretationContext(parentMic) {
            @Override
            public boolean hasDependers(String dependeeName) {
                return true;
            } 
        };
        SiftProcessor siftProcessor = new SiftProcessor<>(context, smic); 
        siftProcessor.addHandler(ParamModel.class, ParamModelHandler::makeInstance);
        siftProcessor.addHandler(PropertyModel.class, PropertyModelHandler::makeInstance);
        siftProcessor.addHandler(ImplicitModel.class, ImplicitModelHandler::makeInstance);
        siftProcessor.addHandler(AppenderModel.class, AppenderModelHandler::makeInstance);
        siftProcessor.addHandler(SiftModel.class, NOPSiftModelHandler::makeInstance);
        
        return siftProcessor;

    }

    public Appender buildAppender(Context context, String discriminatingValue) throws JoranException {
        
        SiftProcessor sp = getSiftingModelProcessor(discriminatingValue);
        ModelInterpretationContext mic = sp.getModelInterpretationContext();
        sp.setContext(context);
        Model duplicate = Model.duplicate(siftModel);
        mic.addSubstitutionProperty(discriminatingKey, discriminatingValue);
        sp.process(duplicate);
        @SuppressWarnings("unchecked")
        Map> appenderBag = (Map>) mic.getObjectMap()
                .get(JoranConstants.APPENDER_BAG);
        Collection> values = appenderBag.values();
        if (values.size() == 0) {
            return null;
        }
        return values.iterator().next();
    }

    public Model getSiftModel() {
        return siftModel;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy