Search in sources :

Example 11 with Node

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

the class XmlConfiguration method constructHierarchy.

private void constructHierarchy(final Node node, final Element element) {
    processAttributes(node, element);
    final StringBuilder buffer = new StringBuilder();
    final NodeList list = element.getChildNodes();
    final List<Node> children = node.getChildren();
    for (int i = 0; i < list.getLength(); i++) {
        final org.w3c.dom.Node w3cNode = list.item(i);
        if (w3cNode instanceof Element) {
            final Element child = (Element) w3cNode;
            final String name = getType(child);
            final PluginType<?> type = pluginManager.getPluginType(name);
            final Node childNode = new Node(node, name, type);
            constructHierarchy(childNode, child);
            if (type == null) {
                final String value = childNode.getValue();
                if (!childNode.hasChildren() && value != null) {
                    node.getAttributes().put(name, value);
                } else {
                    status.add(new Status(name, element, ErrorType.CLASS_NOT_FOUND));
                }
            } else {
                children.add(childNode);
            }
        } else if (w3cNode instanceof Text) {
            final Text data = (Text) w3cNode;
            buffer.append(data.getData());
        }
    }
    final String text = buffer.toString().trim();
    if (text.length() > 0 || (!node.hasChildren() && !node.isRoot())) {
        node.setValue(text);
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.apache.logging.log4j.plugins.Node) Element(org.w3c.dom.Element) Text(org.w3c.dom.Text)

Example 12 with Node

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

the class PluginElementVisitor method inject.

@Override
public void inject(final Object factory) {
    final Optional<Class<?>> componentType = getComponentType(conversionType);
    if (componentType.isPresent()) {
        final Class<?> compType = componentType.get();
        final List<Object> values = new ArrayList<>();
        final Collection<Node> used = new ArrayList<>();
        debugLog.append("={");
        boolean first = true;
        for (final Node child : node.getChildren()) {
            final PluginType<?> type = child.getType();
            if (name.equalsIgnoreCase(type.getElementName()) || compType.isAssignableFrom(type.getPluginClass())) {
                if (!first) {
                    debugLog.append(", ");
                }
                first = false;
                used.add(child);
                final Object childObject = child.getObject();
                if (childObject == null) {
                    LOGGER.warn("Skipping null object returned for element {} in node {}", child.getName(), node.getName());
                } else if (childObject.getClass().isArray()) {
                    final Object[] children = (Object[]) childObject;
                    debugLog.append(Arrays.toString(children)).append('}');
                    node.getChildren().removeAll(used);
                    configurationBinder.bindObject(factory, children);
                    return;
                } else {
                    debugLog.append(child.toString());
                    values.add(childObject);
                }
            }
        }
        debugLog.append('}');
        if (!values.isEmpty() && !TypeUtil.isAssignable(compType, values.get(0).getClass())) {
            LOGGER.error("Cannot assign element {} a list of {} as it is incompatible with {}", name, values.get(0).getClass(), compType);
            return;
        }
        node.getChildren().removeAll(used);
        // using List::toArray here would cause type mismatch later on
        final Object[] vals = (Object[]) Array.newInstance(compType, values.size());
        for (int i = 0; i < vals.length; i++) {
            vals[i] = values.get(i);
        }
        configurationBinder.bindObject(factory, vals);
    } else {
        final Optional<Node> matchingChild = node.getChildren().stream().filter(this::isRequestedNode).findAny();
        if (matchingChild.isPresent()) {
            final Node child = matchingChild.get();
            debugLog.append(child.getName()).append('(').append(child.toString()).append(')');
            node.getChildren().remove(child);
            configurationBinder.bindObject(factory, child.getObject());
        } else {
            debugLog.append(name).append("=null");
            configurationBinder.bindObject(factory, null);
        }
    }
}
Also used : Node(org.apache.logging.log4j.plugins.Node) ArrayList(java.util.ArrayList)

Example 13 with Node

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

the class PluginBuilder method verifyNodeChildrenUsed.

private void verifyNodeChildrenUsed() {
    final List<Node> children = node.getChildren();
    if (!(pluginType.isDeferChildren() || children.isEmpty())) {
        for (final Node child : children) {
            final String nodeType = node.getType().getElementName();
            final String start = nodeType.equals(node.getName()) ? node.getName() : nodeType + ' ' + node.getName();
            LOGGER.error("{} has no parameter that matches element {}", start, child.getName());
        }
    }
}
Also used : Node(org.apache.logging.log4j.plugins.Node)

Example 14 with Node

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

the class JsonConfiguration method setup.

@Override
public void setup() {
    final Iterator<Map.Entry<String, JsonNode>> iter = root.fields();
    final List<Node> children = rootNode.getChildren();
    while (iter.hasNext()) {
        final Map.Entry<String, JsonNode> entry = iter.next();
        final JsonNode n = entry.getValue();
        if (n.isObject()) {
            LOGGER.debug("Processing node for object {}", entry.getKey());
            children.add(constructNode(entry.getKey(), rootNode, n));
        } else if (n.isArray()) {
            LOGGER.error("Arrays are not supported at the root configuration.");
        }
    }
    LOGGER.debug("Completed parsing configuration");
    if (status.size() > 0) {
        for (final Status s : status) {
            LOGGER.error("Error processing element {}: {}", s.name, s.errorType);
        }
    }
}
Also used : Node(org.apache.logging.log4j.plugins.Node) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) Map(java.util.Map)

Example 15 with Node

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

the class ValidHostValidatorTest method setUp.

@SuppressWarnings("unchecked")
@BeforeEach
public void setUp() throws Exception {
    final PluginManager manager = new PluginManager("Test");
    manager.collectPlugins();
    plugin = (PluginType<HostAndPort>) manager.getPluginType("HostAndPort");
    assertNotNull(plugin, "Rebuild this module to ensure annotation processing has been done.");
    node = new Node(null, "HostAndPort", plugin);
}
Also used : PluginManager(org.apache.logging.log4j.plugins.util.PluginManager) HostAndPort(org.apache.logging.log4j.plugins.test.validation.HostAndPort) Node(org.apache.logging.log4j.plugins.Node) BeforeEach(org.junit.jupiter.api.BeforeEach)

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