View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
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.Locale;
26  import java.util.Map;
27  import java.util.Objects;
28  import java.util.Set;
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.SimpleElementVisitor7;
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   * Annotation processor for pre-scanning Log4j 2 plugins.
48   */
49  @SupportedAnnotationTypes("org.apache.logging.log4j.core.config.plugins.*")
50  public class PluginProcessor extends AbstractProcessor {
51  
52      // TODO: this could be made more abstract to allow for compile-time and run-time plugin processing
53  
54      /**
55       * The location of the plugin cache data file. This file is written to by this processor, and read from by
56       * {@link org.apache.logging.log4j.core.config.plugins.util.PluginManager}.
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          System.out.println("Processing annotations");
71          try {
72              final Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(Plugin.class);
73              if (elements.isEmpty()) {
74                  System.out.println("No elements to process");
75                  return false;
76              }
77              collectPlugins(elements);
78              writeCacheFile(elements.toArray(new Element[elements.size()]));
79              System.out.println("Annotations processed");
80              return true;
81          } catch (final IOException e) {
82              e.printStackTrace();
83              error(e.getMessage());
84              return false;
85          } catch (final Exception ex) {
86              ex.printStackTrace();
87              error(ex.getMessage());
88              return false;
89          }
90      }
91  
92      private void error(final CharSequence message) {
93          processingEnv.getMessager().printMessage(Kind.ERROR, message);
94      }
95  
96      private void collectPlugins(final Iterable<? extends Element> elements) {
97          final Elements elementUtils = processingEnv.getElementUtils();
98          final ElementVisitor<PluginEntry, Plugin> pluginVisitor = new PluginElementVisitor(elementUtils);
99          final ElementVisitor<Collection<PluginEntry>, Plugin> pluginAliasesVisitor = new PluginAliasesElementVisitor(
100                 elementUtils);
101         for (final Element element : elements) {
102             final Plugin plugin = element.getAnnotation(Plugin.class);
103             if (plugin == null) {
104                 continue;
105             }
106             final PluginEntry entry = element.accept(pluginVisitor, plugin);
107             final Map<String, PluginEntry> category = pluginCache.getCategory(entry.getCategory());
108             category.put(entry.getKey(), entry);
109             final Collection<PluginEntry> entries = element.accept(pluginAliasesVisitor, plugin);
110             for (final PluginEntry pluginEntry : entries) {
111                 category.put(pluginEntry.getKey(), pluginEntry);
112             }
113         }
114     }
115 
116     private void writeCacheFile(final Element... elements) throws IOException {
117         final FileObject fileObject = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, Strings.EMPTY,
118                 PLUGIN_CACHE_FILE, elements);
119         try (final OutputStream out = fileObject.openOutputStream()) {
120             pluginCache.writeCache(out);
121         }
122     }
123 
124     /**
125      * ElementVisitor to scan the Plugin annotation.
126      */
127     private static class PluginElementVisitor extends SimpleElementVisitor7<PluginEntry, Plugin> {
128 
129         private final Elements elements;
130 
131         private PluginElementVisitor(final Elements elements) {
132             this.elements = elements;
133         }
134 
135         @Override
136         public PluginEntry visitType(final TypeElement e, final Plugin plugin) {
137             Objects.requireNonNull(plugin, "Plugin annotation is null.");
138             final PluginEntry entry = new PluginEntry();
139             entry.setKey(plugin.name().toLowerCase(Locale.US));
140             entry.setClassName(elements.getBinaryName(e).toString());
141             entry.setName(Plugin.EMPTY.equals(plugin.elementType()) ? plugin.name() : plugin.elementType());
142             entry.setPrintable(plugin.printObject());
143             entry.setDefer(plugin.deferChildren());
144             entry.setCategory(plugin.category());
145             return entry;
146         }
147     }
148 
149     /**
150      * ElementVisitor to scan the PluginAliases annotation.
151      */
152     private static class PluginAliasesElementVisitor extends SimpleElementVisitor7<Collection<PluginEntry>, Plugin> {
153 
154         private final Elements elements;
155 
156         private PluginAliasesElementVisitor(final Elements elements) {
157             super(Collections.<PluginEntry> emptyList());
158             this.elements = elements;
159         }
160 
161         @Override
162         public Collection<PluginEntry> visitType(final TypeElement e, final Plugin plugin) {
163             final PluginAliases aliases = e.getAnnotation(PluginAliases.class);
164             if (aliases == null) {
165                 return DEFAULT_VALUE;
166             }
167             final Collection<PluginEntry> entries = new ArrayList<>(aliases.value().length);
168             for (final String alias : aliases.value()) {
169                 final PluginEntry entry = new PluginEntry();
170                 entry.setKey(alias.toLowerCase(Locale.US));
171                 entry.setClassName(elements.getBinaryName(e).toString());
172                 entry.setName(Plugin.EMPTY.equals(plugin.elementType()) ? alias : plugin.elementType());
173                 entry.setPrintable(plugin.printObject());
174                 entry.setDefer(plugin.deferChildren());
175                 entry.setCategory(plugin.category());
176                 entries.add(entry);
177             }
178             return entries;
179         }
180     }
181 }