View Javadoc
1   package org.apache.maven.reporting.exec;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.model.Plugin;
23  import org.apache.maven.reporting.MavenReport;
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   *   <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   * <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.codehaus.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  {
49      private MavenReport mavenReport;
50  
51      private ClassLoader classLoader;
52  
53      private Plugin plugin;
54  
55      private final String goal;
56  
57      public MavenReportExecution( String goal, Plugin plugin, MavenReport mavenReport, ClassLoader classLoader )
58      {
59          this.goal = goal;
60          this.setPlugin( plugin );
61          this.mavenReport = mavenReport;
62          this.classLoader = classLoader;
63      }
64  
65      public MavenReportExecution( Plugin plugin, MavenReport mavenReport, ClassLoader classLoader )
66      {
67          this( null, plugin, mavenReport, classLoader );
68      }
69  
70      public MavenReportExecution( MavenReport mavenReport )
71      {
72          this( null, null, mavenReport, null );
73      }
74  
75      /**
76       * execute Maven Report's <code>canGenerate()</code> with adequate classloader.
77       * @return Maven Report's <code>canGenerate()</code> result
78       */
79      public boolean canGenerateReport()
80      {
81          ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
82          try
83          {
84              if ( classLoader != null )
85              {
86                  Thread.currentThread().setContextClassLoader( classLoader );
87              }
88  
89              return mavenReport.canGenerateReport();
90          }
91          finally
92          {
93              if ( classLoader != null )
94              {
95                  Thread.currentThread().setContextClassLoader( originalClassLoader );
96              }
97          } 
98      }
99  
100     public MavenReport getMavenReport()
101     {
102         return mavenReport;
103     }
104 
105     public void setMavenReport( MavenReport mavenReport )
106     {
107         this.mavenReport = mavenReport;
108     }
109 
110     public ClassLoader getClassLoader()
111     {
112         return classLoader;
113     }
114 
115     public void setClassLoader( ClassLoader classLoader )
116     {
117         this.classLoader = classLoader;
118     }
119 
120     public void setPlugin( Plugin plugin )
121     {
122         this.plugin = plugin;
123     }
124 
125     public Plugin getPlugin()
126     {
127         return plugin;
128     }
129 
130     public String getGoal()
131     {
132         return goal;
133     }
134 }