View Javadoc
1   /*
2    * Copyright 2001-2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.logging.log4j.audit.plugin;
17  
18  import java.io.File;
19  import java.lang.reflect.Constructor;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import org.apache.logging.log4j.audit.generator.InterfacesGenerator;
24  import org.apache.logging.log4j.catalog.api.CatalogReader;
25  import org.apache.logging.log4j.catalog.api.dao.JsonCatalogReader;
26  import org.apache.logging.log4j.util.LoaderUtil;
27  import org.apache.maven.plugin.AbstractMojo;
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.plugins.annotations.LifecyclePhase;
30  import org.apache.maven.plugins.annotations.Mojo;
31  import org.apache.maven.plugins.annotations.Parameter;
32  import org.apache.maven.project.MavenProject;
33  
34  /**
35   * Goal which generates the audit interfaces.
36   */
37  @Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
38  public class AuditMojo extends AbstractMojo {
39  
40      private static final String BASEDIR = "baseDir";
41      private static final String BUILDDIR = "buildDir";
42      private static final int MAX_KEY_LENGTH = 32;
43      private static final int DEFAULT_ENTERPRISE_ID = 18060;
44  
45      @Parameter(defaultValue = "${project}")
46      private MavenProject project;
47  
48      @Parameter(property = "catalogReaderClassName", defaultValue = "org.apache.logging.log4j.catalog.api.dao.FileCatalogReader")
49      private String catalogReaderClassName;
50  
51      @Parameter(property = "catalogReaderAttributes", required = false)
52      private Map<String, String> catalogReaderAttributes;
53  
54      @Parameter(property = "packageName", required = true)
55      private String packageName;
56      /**
57       * Location of the file.
58       */
59      @Parameter(defaultValue = "${project.build.directory}/generated-sources/log4j-audit", property = "outputDir")
60      private File outputDirectory;
61  
62      @Parameter(required = false)
63      private int maxKeyLength;
64  
65      @Parameter(required = false)
66      private int enterpriseId;
67  
68      /**
69       * Set to <code>true</code> to show messages about what the code generator is doing.
70       */
71      @Parameter(defaultValue = "false")
72      private boolean verbose;
73  
74      @SuppressWarnings("unchecked")
75      public void execute() throws MojoExecutionException {
76          if (maxKeyLength <= 0) {
77              maxKeyLength = MAX_KEY_LENGTH;
78          }
79          if (enterpriseId <= 0) {
80              enterpriseId = DEFAULT_ENTERPRISE_ID;
81          }
82          CatalogReader catalogReader = null;
83          try {
84              File basedir = project.getBasedir();
85              Class<?> clazz = LoaderUtil.loadClass(catalogReaderClassName);
86              Constructor<CatalogReader>[] constructors = (Constructor<CatalogReader>[]) clazz.getConstructors();
87  
88              for (Constructor<CatalogReader> constructor : constructors) {
89                  if (constructor.getParameterCount() == 1 && constructor.getParameterTypes()[0].isAssignableFrom(Map.class)) {
90                      if (catalogReaderAttributes == null) {
91                          catalogReaderAttributes = new HashMap<>();
92                      }
93                      if (!catalogReaderAttributes.containsKey(BASEDIR)) {
94                          catalogReaderAttributes.put(BASEDIR, project.getBasedir().getAbsolutePath());
95                      }
96                      if (!catalogReaderAttributes.containsKey(BUILDDIR)) {
97                          catalogReaderAttributes.put(BUILDDIR, project.getBuild().getDirectory());
98                      }
99                      catalogReader = constructor.newInstance(catalogReaderAttributes);
100                     break;
101                 }
102             }
103             if (catalogReader == null) {
104                 catalogReader = LoaderUtil.newInstanceOf(catalogReaderClassName);
105             }
106 
107         } catch (Exception ex) {
108             getLog().error("Unable to load catalog reader " + catalogReaderClassName, ex);
109             return;
110         }
111         InterfacesGenerator generator = new InterfacesGenerator();
112         JsonCatalogReader jsonCatalogReader = new JsonCatalogReader();
113         jsonCatalogReader.setCatalogReader(catalogReader);
114         jsonCatalogReader.init();
115         generator.setCatalogReader(jsonCatalogReader);
116         generator.setOutputDirectory(outputDirectory.getAbsolutePath());
117         generator.setPackageName(packageName);
118         generator.setMaxKeyLength(maxKeyLength);
119         generator.setEnterpriseId(enterpriseId);
120         generator.setVerbose(verbose);
121         try {
122             generator.generateSource();
123             project.addCompileSourceRoot(outputDirectory.getAbsolutePath());
124         } catch (Exception ex) {
125             throw new MojoExecutionException("Error generating Audit interfaces", ex);
126         }
127     }
128 }