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.annotation.Annotation;
21  import java.lang.reflect.Member;
22  import java.util.Map;
23  
24  import org.apache.logging.log4j.Logger;
25  import org.apache.logging.log4j.core.config.plugins.convert.TypeConverters;
26  import org.apache.logging.log4j.core.lookup.StrSubstitutor;
27  import org.apache.logging.log4j.core.util.Assert;
28  import org.apache.logging.log4j.status.StatusLogger;
29  import org.apache.logging.log4j.util.Strings;
30  
31  /**
32   * Base class for PluginVisitor implementations. Provides convenience methods as well as all method implementations
33   * other than the {@code visit} method.
34   *
35   * @param <A> the Plugin annotation type.
36   */
37  public abstract class AbstractPluginVisitor<A extends Annotation> implements PluginVisitor<A> {
38  
39      protected static final Logger LOGGER = StatusLogger.getLogger();
40  
41      protected final Class<A> clazz;
42      protected A annotation;
43      protected String[] aliases;
44      protected Class<?> conversionType;
45      protected StrSubstitutor substitutor;
46      protected Member member;
47  
48      /**
49       * This constructor must be overridden by implementation classes as a no-arg constructor.
50       *
51       * @param clazz the annotation class this PluginVisitor is for.
52       */
53      protected AbstractPluginVisitor(final Class<A> clazz) {
54          this.clazz = clazz;
55      }
56  
57      @SuppressWarnings("unchecked")
58      @Override
59      public PluginVisitor<A> setAnnotation(final Annotation annotation) {
60          final Annotation a = Assert.requireNonNull(annotation, "No annotation was provided");
61          if (this.clazz.isInstance(a)) {
62              this.annotation = (A) a;
63          }
64          return this;
65      }
66  
67      @Override
68      public PluginVisitor<A> setAliases(final String... aliases) {
69          this.aliases = aliases;
70          return this;
71      }
72  
73      @Override
74      public PluginVisitor<A> setConversionType(final Class<?> conversionType) {
75          this.conversionType = Assert.requireNonNull(conversionType, "No conversion type class was provided");
76          return this;
77      }
78  
79      @Override
80      public PluginVisitor<A> setStrSubstitutor(final StrSubstitutor substitutor) {
81          this.substitutor = Assert.requireNonNull(substitutor, "No StrSubstitutor was provided");
82          return this;
83      }
84  
85      @Override
86      public PluginVisitor<A> setMember(final Member member) {
87          this.member = member;
88          return this;
89      }
90  
91      /**
92       * Removes an Entry from a given Map using a key name and aliases for that key. Keys are case-insensitive.
93       *
94       * @param attributes the Map to remove an Entry from.
95       * @param name       the key name to look up.
96       * @param aliases    optional aliases of the key name to look up.
97       * @return the value corresponding to the given key or {@code null} if nonexistent.
98       */
99      protected static String removeAttributeValue(final Map<String, String> attributes,
100                                                  final String name,
101                                                  final String... aliases) {
102         for (final Map.Entry<String, String> entry : attributes.entrySet()) {
103             final String key = entry.getKey();
104             final String value = entry.getValue();
105             if (key.equalsIgnoreCase(name)) {
106                 attributes.remove(key);
107                 return value;
108             }
109             if (aliases != null) {
110                 for (final String alias : aliases) {
111                     if (key.equalsIgnoreCase(alias)) {
112                         attributes.remove(key);
113                         return value;
114                     }
115                 }
116             }
117         }
118         return null;
119     }
120 
121     /**
122      * Converts the given value into the configured type falling back to the provided default value.
123      *
124      * @param value        the value to convert.
125      * @param defaultValue the fallback value to use in case of no value or an error.
126      * @return the converted value whether that be based on the given value or the default value.
127      */
128     protected Object convert(final String value, final Object defaultValue) {
129         if (defaultValue instanceof String) {
130             return TypeConverters.convert(value, this.conversionType, Strings.trimToNull((String) defaultValue));
131         }
132         return TypeConverters.convert(value, this.conversionType, defaultValue);
133     }
134 }