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<>();
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                          node.getChildren().removeAll(used);
67                          return childObject;
68                      }
69                      log.append(child.toString());
70                      values.add(childObject);
71                  }
72              }
73              log.append('}');
74              // note that we need to return an empty array instead of null if the types are correct
75              if (!values.isEmpty() && !this.conversionType.isAssignableFrom(values.get(0).getClass())) {
76                  LOGGER.error("Attempted to assign attribute {} to list of type {} which is incompatible with {}.",
77                      name, values.get(0).getClass(), this.conversionType);
78                  return null;
79              }
80              node.getChildren().removeAll(used);
81              // we need to use reflection here because values.toArray() will cause type errors at runtime
82              final Object[] array = (Object[]) Array.newInstance(this.conversionType, values.size());
83              for (int i = 0; i < array.length; i++) {
84                  array[i] = values.get(i);
85              }
86              return array;
87          }
88          final Node namedNode = findNamedNode(name, node.getChildren());
89          if (namedNode == null) {
90              log.append(name).append("=null");
91              return null;
92          }
93          log.append(namedNode.getName()).append('(').append(namedNode.toString()).append(')');
94          node.getChildren().remove(namedNode);
95          return namedNode.getObject();
96      }
97  
98      private Node findNamedNode(final String name, final Iterable<Node> children) {
99          for (final Node child : children) {
100             final PluginType<?> childType = child.getType();
101             if (childType == null) {
102                 //System.out.println();
103             }
104             if (name.equalsIgnoreCase(childType.getElementName()) ||
105                 this.conversionType.isAssignableFrom(childType.getPluginClass())) {
106                 // FIXME: check child.getObject() for null?
107                 // doing so would be more consistent with the array version
108                 return child;
109             }
110         }
111         return null;
112     }
113 }