Search in sources :

Example 6 with Node

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

the class ValidPortValidatorTest 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);
    node.getAttributes().put("host", "localhost");
}
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)

Example 7 with Node

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

the class ValidatingPluginWithGenericSubclassFoo1BuilderTest method setUp.

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

Example 8 with Node

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

the class ValidatingPluginWithFailoverTest method setUp.

@SuppressWarnings("unchecked")
@BeforeEach
public void setUp() throws Exception {
    final PluginManager manager = new PluginManager(Core.CATEGORY_NAME);
    manager.collectPlugins();
    plugin = (PluginType<FailoverAppender>) manager.getPluginType("failover");
    assertNotNull(plugin, "Rebuild this module to make sure annotation processing kicks in.");
    AppenderRef appenderRef = AppenderRef.createAppenderRef("List", Level.ALL, null);
    node = new Node(null, "failover", plugin);
    Node failoversNode = new Node(node, "Failovers", manager.getPluginType("Failovers"));
    Node appenderRefNode = new Node(failoversNode, "appenderRef", manager.getPluginType("appenderRef"));
    appenderRefNode.getAttributes().put("ref", "file");
    appenderRefNode.setObject(appenderRef);
    failoversNode.getChildren().add(appenderRefNode);
    failoversNode.setObject(FailoversPlugin.createFailovers(appenderRef));
    node.getAttributes().put("primary", "CONSOLE");
    node.getAttributes().put("name", "Failover");
    node.getChildren().add(failoversNode);
}
Also used : PluginManager(org.apache.logging.log4j.plugins.util.PluginManager) Node(org.apache.logging.log4j.plugins.Node) FailoverAppender(org.apache.logging.log4j.core.appender.FailoverAppender) AppenderRef(org.apache.logging.log4j.core.config.AppenderRef) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 9 with Node

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

the class JsonConfiguration method constructNode.

private Node constructNode(final String name, final Node parent, final JsonNode jsonNode) {
    final PluginType<?> type = pluginManager.getPluginType(name);
    final Node node = new Node(parent, name, type);
    processAttributes(node, jsonNode);
    final Iterator<Map.Entry<String, JsonNode>> iter = jsonNode.fields();
    final List<Node> children = node.getChildren();
    while (iter.hasNext()) {
        final Map.Entry<String, JsonNode> entry = iter.next();
        final JsonNode n = entry.getValue();
        if (n.isArray() || n.isObject()) {
            if (type == null) {
                status.add(new Status(name, n, ErrorType.CLASS_NOT_FOUND));
            }
            if (n.isArray()) {
                LOGGER.debug("Processing node for array {}", entry.getKey());
                for (int i = 0; i < n.size(); ++i) {
                    final String pluginType = getType(n.get(i), entry.getKey());
                    final PluginType<?> entryType = pluginManager.getPluginType(pluginType);
                    final Node item = new Node(node, entry.getKey(), entryType);
                    processAttributes(item, n.get(i));
                    if (pluginType.equals(entry.getKey())) {
                        LOGGER.debug("Processing {}[{}]", entry.getKey(), i);
                    } else {
                        LOGGER.debug("Processing {} {}[{}]", pluginType, entry.getKey(), i);
                    }
                    final Iterator<Map.Entry<String, JsonNode>> itemIter = n.get(i).fields();
                    final List<Node> itemChildren = item.getChildren();
                    while (itemIter.hasNext()) {
                        final Map.Entry<String, JsonNode> itemEntry = itemIter.next();
                        if (itemEntry.getValue().isObject()) {
                            LOGGER.debug("Processing node for object {}", itemEntry.getKey());
                            itemChildren.add(constructNode(itemEntry.getKey(), item, itemEntry.getValue()));
                        } else if (itemEntry.getValue().isArray()) {
                            final JsonNode array = itemEntry.getValue();
                            final String entryName = itemEntry.getKey();
                            LOGGER.debug("Processing array for object {}", entryName);
                            for (int j = 0; j < array.size(); ++j) {
                                itemChildren.add(constructNode(entryName, item, array.get(j)));
                            }
                        }
                    }
                    children.add(item);
                }
            } else {
                LOGGER.debug("Processing node for object {}", entry.getKey());
                children.add(constructNode(entry.getKey(), node, n));
            }
        } else {
            LOGGER.debug("Node {} is of type {}", entry.getKey(), n.getNodeType());
        }
    }
    final String t;
    if (type == null) {
        t = "null";
    } else {
        t = type.getElementName() + ':' + type.getPluginClass();
    }
    final String p = node.getParent() == null ? "null" : node.getParent().getName() == null ? LoggerConfig.ROOT : node.getParent().getName();
    LOGGER.debug("Returning {} with parent {} of type {}", node.getName(), p, t);
    return node;
}
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 10 with Node

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

the class PluginElementInjector 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()) {
                    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)

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