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