View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.reporting.exec;
20  
21  import org.apache.maven.model.Plugin;
22  import org.apache.maven.reporting.MavenReport;
23  
24  /**
25   * <p>
26   *   Since Maven 3, reporting plugins (ie {@link MavenReport}s) are not anymore prepared by Maven core.
27   *   This class will store all necessary information for later {@link MavenReport} generation/execution:
28   * </p>
29   * <ul>
30   *   <li>a {@link MavenReport},</li>
31   *   <li>the goal name associated to the report,</li>
32   *   <li>the associated {@link ClassLoader} for the report generation,</li>
33   *   <li>the {@link Plugin} associated to the {@link MavenReport}.</li>
34   * </ul>
35   * <p>
36   *   With this bean, a plugin wanting to generate a report (= <i>"execute"</i> the report) has to call the
37   *   {@link MavenReport#generate(org.apache.maven.doxia.sink.Sink, java.util.Locale)}
38   *   method, setting the current {@link Thread} classLoader first with {@link #classLoader}.
39   * </p>
40   * <p>
41   *   This bean is instantiated by {@link MavenReportExecutor}.
42   * </p>
43   *
44   * @author Olivier Lamy
45   */
46  public class MavenReportExecution {
47      private MavenReport mavenReport;
48  
49      private ClassLoader classLoader;
50  
51      private Plugin plugin;
52  
53      private final String goal;
54  
55      public MavenReportExecution(String goal, Plugin plugin, MavenReport mavenReport, ClassLoader classLoader) {
56          this.goal = goal;
57          this.setPlugin(plugin);
58          this.mavenReport = mavenReport;
59          this.classLoader = classLoader;
60      }
61  
62      public MavenReportExecution(Plugin plugin, MavenReport mavenReport, ClassLoader classLoader) {
63          this(null, plugin, mavenReport, classLoader);
64      }
65  
66      public MavenReportExecution(MavenReport mavenReport) {
67          this(null, null, mavenReport, null);
68      }
69  
70      /**
71       * execute Maven Report's <code>canGenerate()</code> with adequate classloader.
72       * @return Maven Report's <code>canGenerate()</code> result
73       */
74      public boolean canGenerateReport() {
75          ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
76          try {
77              if (classLoader != null) {
78                  Thread.currentThread().setContextClassLoader(classLoader);
79              }
80  
81              return mavenReport.canGenerateReport();
82          } finally {
83              if (classLoader != null) {
84                  Thread.currentThread().setContextClassLoader(originalClassLoader);
85              }
86          }
87      }
88  
89      public MavenReport getMavenReport() {
90          return mavenReport;
91      }
92  
93      public void setMavenReport(MavenReport mavenReport) {
94          this.mavenReport = mavenReport;
95      }
96  
97      public ClassLoader getClassLoader() {
98          return classLoader;
99      }
100 
101     public void setClassLoader(ClassLoader classLoader) {
102         this.classLoader = classLoader;
103     }
104 
105     public void setPlugin(Plugin plugin) {
106         this.plugin = plugin;
107     }
108 
109     public Plugin getPlugin() {
110         return plugin;
111     }
112 
113     public String getGoal() {
114         return goal;
115     }
116 }