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 final String goal;
49  
50      private Plugin plugin;
51  
52      private MavenReport mavenReport;
53  
54      private ClassLoader classLoader;
55  
56      private boolean userDefined;
57  
58      public MavenReportExecution(
59              String goal, Plugin plugin, MavenReport mavenReport, ClassLoader classLoader, boolean userDefined) {
60          this.goal = goal;
61          this.setPlugin(plugin);
62          this.mavenReport = mavenReport;
63          this.classLoader = classLoader;
64          this.userDefined = userDefined;
65      }
66  
67      public MavenReportExecution(Plugin plugin, MavenReport mavenReport, ClassLoader classLoader, boolean userDefined) {
68          this(null, plugin, mavenReport, classLoader, userDefined);
69      }
70  
71      public MavenReportExecution(MavenReport mavenReport) {
72          this(null, null, mavenReport, null, false);
73      }
74  
75      /**
76       * execute Maven Report's <code>canGenerate()</code> with adequate classloader.
77       * @return Maven Report's <code>canGenerate()</code> result
78       * @throws MavenReportException if any
79       */
80      public boolean canGenerateReport() throws MavenReportException {
81          ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
82          try {
83              if (classLoader != null) {
84                  Thread.currentThread().setContextClassLoader(classLoader);
85              }
86  
87              return mavenReport.canGenerateReport();
88          } finally {
89              if (classLoader != null) {
90                  Thread.currentThread().setContextClassLoader(originalClassLoader);
91              }
92          }
93      }
94  
95      public MavenReport getMavenReport() {
96          return mavenReport;
97      }
98  
99      public void setMavenReport(MavenReport mavenReport) {
100         this.mavenReport = mavenReport;
101     }
102 
103     public ClassLoader getClassLoader() {
104         return classLoader;
105     }
106 
107     public void setClassLoader(ClassLoader classLoader) {
108         this.classLoader = classLoader;
109     }
110 
111     public void setPlugin(Plugin plugin) {
112         this.plugin = plugin;
113     }
114 
115     public Plugin getPlugin() {
116         return plugin;
117     }
118 
119     public String getGoal() {
120         return goal;
121     }
122 
123     public boolean isUserDefined() {
124         return userDefined;
125     }
126 
127     public void setUserDefined(boolean userDefined) {
128         this.userDefined = userDefined;
129     }
130 }