1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.logging.log4j.core.config.plugins.visitors;
19
20 import java.lang.reflect.Array;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.List;
25
26 import org.apache.logging.log4j.core.LogEvent;
27 import org.apache.logging.log4j.core.config.Configuration;
28 import org.apache.logging.log4j.core.config.Node;
29 import org.apache.logging.log4j.core.config.plugins.PluginElement;
30 import org.apache.logging.log4j.core.config.plugins.util.PluginType;
31
32
33
34
35 public class PluginElementVisitor extends AbstractPluginVisitor<PluginElement> {
36 public PluginElementVisitor() {
37 super(PluginElement.class);
38 }
39
40 @Override
41 public Object visit(final Configuration configuration, final Node node, final LogEvent event,
42 final StringBuilder log) {
43 final String name = this.annotation.value();
44 if (this.conversionType.isArray()) {
45 setConversionType(this.conversionType.getComponentType());
46 final List<Object> values = new ArrayList<>();
47 final Collection<Node> used = new ArrayList<>();
48 log.append("={");
49 boolean first = true;
50 for (final Node child : node.getChildren()) {
51 final PluginType<?> childType = child.getType();
52 if (name.equalsIgnoreCase(childType.getElementName()) ||
53 this.conversionType.isAssignableFrom(childType.getPluginClass())) {
54 if (!first) {
55 log.append(", ");
56 }
57 first = false;
58 used.add(child);
59 final Object childObject = child.getObject();
60 if (childObject == null) {
61 LOGGER.error("Null object returned for {} in {}.", child.getName(), node.getName());
62 continue;
63 }
64 if (childObject.getClass().isArray()) {
65 log.append(Arrays.toString((Object[]) childObject)).append('}');
66 return childObject;
67 }
68 log.append(child.toString());
69 values.add(childObject);
70 }
71 }
72 log.append('}');
73
74 if (!values.isEmpty() && !this.conversionType.isAssignableFrom(values.get(0).getClass())) {
75 LOGGER.error("Attempted to assign attribute {} to list of type {} which is incompatible with {}.",
76 name, values.get(0).getClass(), this.conversionType);
77 return null;
78 }
79 node.getChildren().removeAll(used);
80
81 final Object[] array = (Object[]) Array.newInstance(this.conversionType, values.size());
82 for (int i = 0; i < array.length; i++) {
83 array[i] = values.get(i);
84 }
85 return array;
86 }
87 final Node namedNode = findNamedNode(name, node.getChildren());
88 if (namedNode == null) {
89 log.append("null");
90 return null;
91 }
92 log.append(namedNode.getName()).append('(').append(namedNode.toString()).append(')');
93 node.getChildren().remove(namedNode);
94 return namedNode.getObject();
95 }
96
97 private Node findNamedNode(final String name, final Iterable<Node> children) {
98 for (final Node child : children) {
99 final PluginType<?> childType = child.getType();
100 if (name.equalsIgnoreCase(childType.getElementName()) ||
101 this.conversionType.isAssignableFrom(childType.getPluginClass())) {
102
103
104 return child;
105 }
106 }
107 return null;
108 }
109 }