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.Set;
26 import java.util.concurrent.ConcurrentMap;
27
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.SimpleElementVisitor6;
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 = "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 ConcurrentMap<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,
106 Strings.EMPTY, PLUGIN_CACHE_FILE, elements);
107 final OutputStream out = fo.openOutputStream();
108 try {
109 pluginCache.writeCache(out);
110 } finally {
111 out.close();
112 }
113 }
114
115
116
117
118 private static class PluginElementVisitor extends SimpleElementVisitor6<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 if (plugin == null) {
129 throw new NullPointerException("Plugin annotation is null.");
130 }
131 final PluginEntry entry = new PluginEntry();
132 entry.setKey(plugin.name().toLowerCase());
133 entry.setClassName(elements.getBinaryName(e).toString());
134 entry.setName(Plugin.EMPTY.equals(plugin.elementType()) ? plugin.name() : plugin.elementType());
135 entry.setPrintable(plugin.printObject());
136 entry.setDefer(plugin.deferChildren());
137 entry.setCategory(plugin.category());
138 return entry;
139 }
140 }
141
142
143
144
145 private static class PluginAliasesElementVisitor extends SimpleElementVisitor6<Collection<PluginEntry>, Plugin> {
146
147 private final Elements elements;
148
149 private PluginAliasesElementVisitor(final Elements elements) {
150 super(Collections.<PluginEntry>emptyList());
151 this.elements = elements;
152 }
153
154 @Override
155 public Collection<PluginEntry> visitType(final TypeElement e, final Plugin plugin) {
156 final PluginAliases aliases = e.getAnnotation(PluginAliases.class);
157 if (aliases == null) {
158 return DEFAULT_VALUE;
159 }
160 final Collection<PluginEntry> entries = new ArrayList<PluginEntry>(aliases.value().length);
161 for (final String alias : aliases.value()) {
162 final PluginEntry entry = new PluginEntry();
163 entry.setKey(alias.toLowerCase());
164 entry.setClassName(elements.getBinaryName(e).toString());
165 entry.setName(Plugin.EMPTY.equals(plugin.elementType()) ? alias : plugin.elementType());
166 entry.setPrintable(plugin.printObject());
167 entry.setDefer(plugin.deferChildren());
168 entry.setCategory(plugin.category());
169 entries.add(entry);
170 }
171 return entries;
172 }
173 }
174 }