View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * PluginVisitor implementation for {@link PluginElement}. Supports arrays as well as singular values.
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<Object>();
47              final Collection<Node> used = new ArrayList<Node>();
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              // note that we need to return an empty array instead of null if the types are correct
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              // we need to use reflection here because values.toArray() will cause type errors at runtime
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                 // FIXME: check child.getObject() for null?
103                 // doing so would be more consistent with the array version
104                 return child;
105             }
106         }
107         return null;
108     }
109 }