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