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