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
22 import org.apache.logging.log4j.Logger;
23 import org.apache.logging.log4j.core.config.plugins.PluginVisitorStrategy;
24 import org.apache.logging.log4j.status.StatusLogger;
25
26 /**
27 * Utility class to locate an appropriate {@link PluginVisitor} implementation for an annotation.
28 */
29 public final class PluginVisitors {
30
31 private static final Logger LOGGER = StatusLogger.getLogger();
32
33 private PluginVisitors() {
34 }
35
36 /**
37 * Creates a PluginVisitor instance for the given annotation class using metadata provided by the annotation's
38 * {@link PluginVisitorStrategy} annotation. This instance must be further populated with
39 * data to be useful. Such data is passed through both the setters and the visit method.
40 *
41 * @param annotation the Plugin annotation class to find a PluginVisitor for.
42 * @return a PluginVisitor instance if one could be created, or {@code null} otherwise.
43 */
44 public static PluginVisitor<? extends Annotation> findVisitor(final Class<? extends Annotation> annotation) {
45 final PluginVisitorStrategy strategy = annotation.getAnnotation(PluginVisitorStrategy.class);
46 if (strategy == null) {
47 return null;
48 }
49 try {
50 return strategy.value().newInstance();
51 } catch (final Exception e) {
52 LOGGER.error("Error loading PluginVisitor [{}] for annotation [{}].", strategy.value(), annotation, e);
53 return null;
54 }
55 }
56 }