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  import org.apache.maven.reporting.MavenReportException;
24  
25  /**
26   * <p>
27   *   Since Maven 3, reporting plugins (ie {@link MavenReport}s) are not anymore prepared by Maven core.
28   *   This class will store all necessary information for later {@link MavenReport} generation/execution:
29   * </p>
30   * <ul>
31   *   <li>a {@link MavenReport},</li>
32   *   <li>the goal name associated to the report,</li>
33   *   <li>the associated {@link ClassLoader} for the report generation,</li>
34   *   <li>the {@link Plugin} associated to the {@link MavenReport}.</li>
35   * </ul>
36   * <p>
37   *   With this bean, a plugin wanting to generate a report (= <i>"execute"</i> the report) has to call the
38   *   {@link MavenReport#generate(org.apache.maven.doxia.sink.Sink, java.util.Locale)}
39   *   method, setting the current {@link Thread} classLoader first with {@link #classLoader}.
40   * </p>
41   * <p>
42   *   This bean is instantiated by {@link MavenReportExecutor}.
43   * </p>
44   *
45   * @author Olivier Lamy
46   */
47  public class MavenReportExecution {
48      private MavenReport mavenReport;
49  
50      private ClassLoader classLoader;
51  
52      private Plugin plugin;
53  
54      private final String goal;
55  
56      public MavenReportExecution(String goal, Plugin plugin, MavenReport mavenReport, ClassLoader classLoader) {
57          this.goal = goal;
58          this.setPlugin(plugin);
59          this.mavenReport = mavenReport;
60          this.classLoader = classLoader;
61      }
62  
63      public MavenReportExecution(Plugin plugin, MavenReport mavenReport, ClassLoader classLoader) {
64          this(null, plugin, mavenReport, classLoader);
65      }
66  
67      public MavenReportExecution(MavenReport mavenReport) {
68          this(null, null, mavenReport, null);
69      }
70  
71      /**
72       * execute Maven Report's <code>canGenerate()</code> with adequate classloader.
73       * @return Maven Report's <code>canGenerate()</code> result
74       * @throws MavenReportException if any
75       */
76      public boolean canGenerateReport() throws MavenReportException {
77          ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
78          try {
79              if (classLoader != null) {
80                  Thread.currentThread().setContextClassLoader(classLoader);
81              }
82  
83              return mavenReport.canGenerateReport();
84          } finally {
85              if (classLoader != null) {
86                  Thread.currentThread().setContextClassLoader(originalClassLoader);
87              }
88          }
89      }
90  
91      public MavenReport getMavenReport() {
92          return mavenReport;
93      }
94  
95      public void setMavenReport(MavenReport mavenReport) {
96          this.mavenReport = mavenReport;
97      }
98  
99      public ClassLoader getClassLoader() {
100         return classLoader;
101     }
102 
103     public void setClassLoader(ClassLoader classLoader) {
104         this.classLoader = classLoader;
105     }
106 
107     public void setPlugin(Plugin plugin) {
108         this.plugin = plugin;
109     }
110 
111     public Plugin getPlugin() {
112         return plugin;
113     }
114 
115     public String getGoal() {
116         return goal;
117     }
118 }