Search in sources :

Example 21 with Node

use of org.apache.logging.log4j.plugins.Node in project logging-log4j2 by apache.

the class AbstractConfiguration method createAdvertiser.

protected void createAdvertiser(final String advertiserString, final ConfigurationSource configSource, final byte[] buffer, final String contentType) {
    if (advertiserString != null) {
        final Node node = new Node(null, advertiserString, null);
        final Map<String, String> attributes = node.getAttributes();
        attributes.put("content", new String(buffer));
        attributes.put("contentType", contentType);
        attributes.put("name", "configuration");
        if (configSource.getLocation() != null) {
            attributes.put("location", configSource.getLocation());
        }
        advertiserNode = node;
    }
}
Also used : Node(org.apache.logging.log4j.plugins.Node)

Example 22 with Node

use of org.apache.logging.log4j.plugins.Node in project logging-log4j2 by apache.

the class AbstractConfiguration method processConditionals.

/**
 * Process conditions by evaluating them and including the children of conditions that are true
 * and discarding those that are not.
 * @param node The node to evaluate.
 */
protected void processConditionals(final Node node) {
    try {
        final List<Node> addList = new ArrayList<>();
        final List<Node> removeList = new ArrayList<>();
        for (final Node child : node.getChildren()) {
            final PluginType<?> type = child.getType();
            if (type != null && Arbiter.ELEMENT_TYPE.equals(type.getElementName())) {
                final Class<?> clazz = type.getPluginClass();
                if (SelectArbiter.class.isAssignableFrom(clazz)) {
                    removeList.add(child);
                    addList.addAll(processSelect(child, type));
                } else if (Arbiter.class.isAssignableFrom(clazz)) {
                    removeList.add(child);
                    try {
                        final Arbiter condition = (Arbiter) createPluginObject(type, child, null);
                        if (condition.isCondition()) {
                            addList.addAll(child.getChildren());
                            processConditionals(child);
                        }
                    } catch (final Exception inner) {
                        LOGGER.error("Exception processing {}: Ignoring and including children", type.getPluginClass());
                        processConditionals(child);
                    }
                } else {
                    LOGGER.error("Encountered Condition Plugin that does not implement Condition: {}", child.getName());
                    processConditionals(child);
                }
            } else {
                processConditionals(child);
            }
        }
        if (!removeList.isEmpty()) {
            final List<Node> children = node.getChildren();
            children.removeAll(removeList);
            children.addAll(addList);
            for (final Node grandChild : addList) {
                grandChild.setParent(node);
            }
        }
    } catch (final Exception ex) {
        LOGGER.error("Error capturing node data for node " + node.getName(), ex);
    }
}
Also used : Node(org.apache.logging.log4j.plugins.Node) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Arbiter(org.apache.logging.log4j.core.config.arbiters.Arbiter) SelectArbiter(org.apache.logging.log4j.core.config.arbiters.SelectArbiter) IOException(java.io.IOException)

Example 23 with Node

use of org.apache.logging.log4j.plugins.Node in project logging-log4j2 by apache.

the class AbstractConfiguration method createPluginCollection.

private static Collection<?> createPluginCollection(final Node node) {
    final List<Node> children = node.getChildren();
    final Collection<Object> list = new ArrayList<>(children.size());
    for (final Node child : children) {
        final Object object = child.getObject();
        list.add(object);
    }
    return list;
}
Also used : Node(org.apache.logging.log4j.plugins.Node) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList)

Example 24 with Node

use of org.apache.logging.log4j.plugins.Node in project logging-log4j2 by apache.

the class DefaultMergeStrategy method updateFilterNode.

private void updateFilterNode(final Node target, final Node targetChildNode, final Node sourceChildNode, final PluginManager pluginManager) {
    if (CompositeFilter.class.isAssignableFrom(targetChildNode.getType().getPluginClass())) {
        final Node node = new Node(targetChildNode, sourceChildNode.getName(), sourceChildNode.getType());
        node.getChildren().addAll(sourceChildNode.getChildren());
        node.getAttributes().putAll(sourceChildNode.getAttributes());
        targetChildNode.getChildren().add(node);
    } else {
        final PluginType pluginType = pluginManager.getPluginType(FILTERS);
        final Node filtersNode = new Node(targetChildNode, FILTERS, pluginType);
        final Node node = new Node(filtersNode, sourceChildNode.getName(), sourceChildNode.getType());
        node.getAttributes().putAll(sourceChildNode.getAttributes());
        final List<Node> children = filtersNode.getChildren();
        children.add(targetChildNode);
        children.add(node);
        final List<Node> nodes = target.getChildren();
        nodes.remove(targetChildNode);
        nodes.add(filtersNode);
    }
}
Also used : Node(org.apache.logging.log4j.plugins.Node) PluginType(org.apache.logging.log4j.plugins.util.PluginType)

Example 25 with Node

use of org.apache.logging.log4j.plugins.Node in project logging-log4j2 by apache.

the class DefaultMergeStrategy method mergeConfigurations.

/**
 * Merge the source Configuration into the target Configuration.
 *
 * @param target        The target node to merge into.
 * @param source        The source node.
 * @param pluginManager The PluginManager.
 */
@Override
public void mergeConfigurations(final Node target, final Node source, final PluginManager pluginManager) {
    for (final Node sourceChildNode : source.getChildren()) {
        final boolean isFilter = isFilterNode(sourceChildNode);
        boolean isMerged = false;
        for (final Node targetChildNode : target.getChildren()) {
            if (isFilter) {
                if (isFilterNode(targetChildNode)) {
                    updateFilterNode(target, targetChildNode, sourceChildNode, pluginManager);
                    isMerged = true;
                    break;
                }
                continue;
            }
            if (!targetChildNode.getName().equalsIgnoreCase(sourceChildNode.getName())) {
                continue;
            }
            switch(targetChildNode.getName().toLowerCase()) {
                case PROPERTIES:
                case SCRIPTS:
                case APPENDERS:
                    {
                        for (final Node node : sourceChildNode.getChildren()) {
                            for (final Node targetNode : targetChildNode.getChildren()) {
                                if (Objects.equals(targetNode.getAttributes().get(NAME), node.getAttributes().get(NAME))) {
                                    targetChildNode.getChildren().remove(targetNode);
                                    break;
                                }
                            }
                            targetChildNode.getChildren().add(node);
                        }
                        isMerged = true;
                        break;
                    }
                case LOGGERS:
                    {
                        final Map<String, Node> targetLoggers = new HashMap<>();
                        for (final Node node : targetChildNode.getChildren()) {
                            targetLoggers.put(node.getName(), node);
                        }
                        for (final Node node : sourceChildNode.getChildren()) {
                            final Node targetNode = getLoggerNode(targetChildNode, node.getAttributes().get(NAME));
                            final Node loggerNode = new Node(targetChildNode, node.getName(), node.getType());
                            if (targetNode != null) {
                                targetNode.getAttributes().putAll(node.getAttributes());
                                for (final Node sourceLoggerChild : node.getChildren()) {
                                    if (isFilterNode(sourceLoggerChild)) {
                                        boolean foundFilter = false;
                                        for (final Node targetChild : targetNode.getChildren()) {
                                            if (isFilterNode(targetChild)) {
                                                updateFilterNode(loggerNode, targetChild, sourceLoggerChild, pluginManager);
                                                foundFilter = true;
                                                break;
                                            }
                                        }
                                        if (!foundFilter) {
                                            final Node childNode = new Node(loggerNode, sourceLoggerChild.getName(), sourceLoggerChild.getType());
                                            childNode.getAttributes().putAll(sourceLoggerChild.getAttributes());
                                            childNode.getChildren().addAll(sourceLoggerChild.getChildren());
                                            targetNode.getChildren().add(childNode);
                                        }
                                    } else {
                                        final Node childNode = new Node(loggerNode, sourceLoggerChild.getName(), sourceLoggerChild.getType());
                                        childNode.getAttributes().putAll(sourceLoggerChild.getAttributes());
                                        childNode.getChildren().addAll(sourceLoggerChild.getChildren());
                                        if (childNode.getName().equalsIgnoreCase("AppenderRef")) {
                                            for (final Node targetChild : targetNode.getChildren()) {
                                                if (isSameReference(targetChild, childNode)) {
                                                    targetNode.getChildren().remove(targetChild);
                                                    break;
                                                }
                                            }
                                        } else {
                                            for (final Node targetChild : targetNode.getChildren()) {
                                                if (isSameName(targetChild, childNode)) {
                                                    targetNode.getChildren().remove(targetChild);
                                                    break;
                                                }
                                            }
                                        }
                                        targetNode.getChildren().add(childNode);
                                    }
                                }
                            } else {
                                loggerNode.getAttributes().putAll(node.getAttributes());
                                loggerNode.getChildren().addAll(node.getChildren());
                                targetChildNode.getChildren().add(loggerNode);
                            }
                        }
                        isMerged = true;
                        break;
                    }
                default:
                    {
                        targetChildNode.getChildren().addAll(sourceChildNode.getChildren());
                        isMerged = true;
                        break;
                    }
            }
        }
        if (!isMerged) {
            if (sourceChildNode.getName().equalsIgnoreCase("Properties")) {
                target.getChildren().add(0, sourceChildNode);
            } else {
                target.getChildren().add(sourceChildNode);
            }
        }
    }
}
Also used : Node(org.apache.logging.log4j.plugins.Node) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Node (org.apache.logging.log4j.plugins.Node)27 PluginManager (org.apache.logging.log4j.plugins.util.PluginManager)7 BeforeEach (org.junit.jupiter.api.BeforeEach)7 ArrayList (java.util.ArrayList)6 Map (java.util.Map)5 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 Appender (org.apache.logging.log4j.core.Appender)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 IOException (java.io.IOException)2 LinkedHashMap (java.util.LinkedHashMap)2 Arbiter (org.apache.logging.log4j.core.config.arbiters.Arbiter)2 SelectArbiter (org.apache.logging.log4j.core.config.arbiters.SelectArbiter)2 HostAndPort (org.apache.logging.log4j.plugins.test.validation.HostAndPort)2 HashMap (java.util.HashMap)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Filter (org.apache.logging.log4j.core.Filter)1 AbstractAppender (org.apache.logging.log4j.core.appender.AbstractAppender)1 AsyncAppender (org.apache.logging.log4j.core.appender.AsyncAppender)1