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