1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.logging.log4j.core.config.plugins.processor;
19
20 import java.io.IOException;
21 import java.io.OutputStream;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.Map;
26 import java.util.Objects;
27 import java.util.Set;
28 import javax.annotation.processing.AbstractProcessor;
29 import javax.annotation.processing.RoundEnvironment;
30 import javax.annotation.processing.SupportedAnnotationTypes;
31 import javax.lang.model.SourceVersion;
32 import javax.lang.model.element.Element;
33 import javax.lang.model.element.ElementVisitor;
34 import javax.lang.model.element.TypeElement;
35 import javax.lang.model.util.Elements;
36 import javax.lang.model.util.SimpleElementVisitor7;
37 import javax.tools.Diagnostic.Kind;
38 import javax.tools.FileObject;
39 import javax.tools.StandardLocation;
40
41 import org.apache.logging.log4j.core.config.plugins.Plugin;
42 import org.apache.logging.log4j.core.config.plugins.PluginAliases;
43 import org.apache.logging.log4j.util.Strings;
44
45
46
47
48 @SupportedAnnotationTypes("org.apache.logging.log4j.core.config.plugins.*")
49 public class PluginProcessor extends AbstractProcessor {
50
51
52
53
54
55
56
57 public static final String PLUGIN_CACHE_FILE =
58 "META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat";
59
60 private final PluginCache pluginCache = new PluginCache();
61
62 @Override
63 public SourceVersion getSupportedSourceVersion() {
64 return SourceVersion.latest();
65 }
66
67 @Override
68 public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {
69 try {
70 final Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(Plugin.class);
71 if (elements.isEmpty()) {
72 return false;
73 }
74 collectPlugins(elements);
75 writeCacheFile(elements.toArray(new Element[elements.size()]));
76 return true;
77 } catch (final IOException e) {
78 error(e.getMessage());
79 return false;
80 }
81 }
82
83 private void error(final CharSequence message) {
84 processingEnv.getMessager().printMessage(Kind.ERROR, message);
85 }
86
87 private void collectPlugins(final Iterable<? extends Element> elements) {
88 final Elements elementUtils = processingEnv.getElementUtils();
89 final ElementVisitor<PluginEntry, Plugin> pluginVisitor = new PluginElementVisitor(elementUtils);
90 final ElementVisitor<Collection<PluginEntry>, Plugin> pluginAliasesVisitor = new PluginAliasesElementVisitor(
91 elementUtils);
92 for (final Element element : elements) {
93 final Plugin plugin = element.getAnnotation(Plugin.class);
94 if (plugin == null) {
95 continue;
96 }
97 final PluginEntry entry = element.accept(pluginVisitor, plugin);
98 final Map<String, PluginEntry> category = pluginCache.getCategory(entry.getCategory());
99 category.put(entry.getKey(), entry);
100 final Collection<PluginEntry> entries = element.accept(pluginAliasesVisitor, plugin);
101 for (final PluginEntry pluginEntry : entries) {
102 category.put(pluginEntry.getKey(), pluginEntry);
103 }
104 }
105 }
106
107 private void writeCacheFile(final Element... elements) throws IOException {
108 final FileObject fo = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, Strings.EMPTY,
109 PLUGIN_CACHE_FILE, elements);
110 try (final OutputStream out = fo.openOutputStream()) {
111 pluginCache.writeCache(out);
112 }
113 }
114
115
116
117
118 private static class PluginElementVisitor extends SimpleElementVisitor7<PluginEntry, Plugin> {
119
120 private final Elements elements;
121
122 private PluginElementVisitor(final Elements elements) {
123 this.elements = elements;
124 }
125
126 @Override
127 public PluginEntry visitType(final TypeElement e, final Plugin plugin) {
128 Objects.requireNonNull(plugin, "Plugin annotation is null.");
129 final PluginEntry entry = new PluginEntry();
130 entry.setKey(plugin.name().toLowerCase());
131 entry.setClassName(elements.getBinaryName(e).toString());
132 entry.setName(Plugin.EMPTY.equals(plugin.elementType()) ? plugin.name() : plugin.elementType());
133 entry.setPrintable(plugin.printObject());
134 entry.setDefer(plugin.deferChildren());
135 entry.setCategory(plugin.category());
136 return entry;
137 }
138 }
139
140
141
142
143 private static class PluginAliasesElementVisitor extends SimpleElementVisitor7<Collection<PluginEntry>, Plugin> {
144
145 private final Elements elements;
146
147 private PluginAliasesElementVisitor(final Elements elements) {
148 super(Collections.<PluginEntry> emptyList());
149 this.elements = elements;
150 }
151
152 @Override
153 public Collection<PluginEntry> visitType(final TypeElement e, final Plugin plugin) {
154 final PluginAliases aliases = e.getAnnotation(PluginAliases.class);
155 if (aliases == null) {
156 return DEFAULT_VALUE;
157 }
158 final Collection<PluginEntry> entries = new ArrayList<>(aliases.value().length);
159 for (final String alias : aliases.value()) {
160 final PluginEntry entry = new PluginEntry();
161 entry.setKey(alias.toLowerCase());
162 entry.setClassName(elements.getBinaryName(e).toString());
163 entry.setName(Plugin.EMPTY.equals(plugin.elementType()) ? alias : plugin.elementType());
164 entry.setPrintable(plugin.printObject());
165 entry.setDefer(plugin.deferChildren());
166 entry.setCategory(plugin.category());
167 entries.add(entry);
168 }
169 return entries;
170 }
171 }
172 }