KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > maven > agent > PluginManagerInterceptor


1 package hudson.maven.agent;
2
3 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
4 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
5 import org.apache.maven.execution.MavenSession;
6 import org.apache.maven.plugin.DefaultPluginManager;
7 import org.apache.maven.plugin.MojoExecution;
8 import org.apache.maven.plugin.MojoExecutionException;
9 import org.apache.maven.plugin.MojoFailureException;
10 import org.apache.maven.plugin.PluginConfigurationException;
11 import org.apache.maven.plugin.PluginManagerException;
12 import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
13 import org.apache.maven.plugin.descriptor.MojoDescriptor;
14 import org.apache.maven.plugin.descriptor.PluginDescriptor;
15 import org.apache.maven.project.MavenProject;
16 import org.apache.maven.project.artifact.InvalidDependencyVersionException;
17 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
18 import org.codehaus.plexus.configuration.PlexusConfiguration;
19 import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
20 import org.codehaus.plexus.util.xml.Xpp3Dom;
21
22 import java.io.IOException JavaDoc;
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25
26 /**
27  * Description in META-INF/plexus/components.xml makes it possible to use this instead of the default
28  * plugin manager.
29  *
30  * @author Kohsuke Kawaguchi
31  */

32 public class PluginManagerInterceptor extends DefaultPluginManager {
33
34     private final Method JavaDoc mergeMojoConfiguration;
35
36     /**
37      * {@link PluginManagerListener} that receives events.
38      * There's no way external code can connect to a running instance of
39      * {@link PluginManagerInterceptor}, so this cannot be made instance fields.
40      */

41     private static PluginManagerListener listener;
42
43     public PluginManagerInterceptor() {
44         try {
45             this.mergeMojoConfiguration = DefaultPluginManager.class.getDeclaredMethod(
46                 "mergeMojoConfiguration", XmlPlexusConfiguration.class, MojoDescriptor.class);
47             mergeMojoConfiguration.setAccessible(true);
48         } catch (NoSuchMethodException JavaDoc e) {
49             NoSuchMethodError JavaDoc x = new NoSuchMethodError JavaDoc("Unable to find DefaultPluginManager.mergeMojoConfiguration()");
50             x.initCause(e);
51             throw x;
52         }
53     }
54
55     public static void setListener(PluginManagerListener _listener) {
56         listener = _listener;
57     }
58
59     public void executeMojo(MavenProject project, MojoExecution mojoExecution, MavenSession session) throws ArtifactResolutionException, MojoExecutionException, MojoFailureException, ArtifactNotFoundException, InvalidDependencyVersionException, PluginManagerException, PluginConfigurationException {
60         Xpp3Dom dom = getConfigDom(mojoExecution, project);
61
62         XmlPlexusConfiguration pomConfiguration;
63         if ( dom == null )
64         {
65             pomConfiguration = new XmlPlexusConfiguration( "configuration" );
66         }
67         else
68         {
69             pomConfiguration = new XmlPlexusConfiguration( dom );
70         }
71
72         MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
73         PlexusConfiguration mergedConfiguration;
74
75         try {
76             mergedConfiguration = (PlexusConfiguration) mergeMojoConfiguration.invoke(this, pomConfiguration, mojoDescriptor );
77         } catch (IllegalAccessException JavaDoc e) {
78             IllegalAccessError JavaDoc x = new IllegalAccessError JavaDoc();
79             x.initCause(e);
80             throw x;
81         } catch (InvocationTargetException JavaDoc e) {
82             throw new MojoExecutionException("Failed to check configuration",e);
83         }
84         // this just seems like an error check
85
//PlexusConfiguration extractedMojoConfiguration =
86
// extractMojoConfiguration( mergedConfiguration, mojoDescriptor ); // what does this do?
87

88         ExpressionEvaluator eval = new PluginParameterExpressionEvaluator( session, mojoExecution,
89                                                                                           pathTranslator, getLogger(),
90                                                                                           project,
91                                                                                           session.getExecutionProperties() );
92
93         try {
94             if(listener!=null)
95                 listener.preExecute(project,mojoExecution,mergedConfiguration,eval);
96             super.executeMojo(project, mojoExecution, session);
97             if(listener!=null)
98                 listener.postExecute(project,mojoExecution,mergedConfiguration,eval);
99         } catch (InterruptedException JavaDoc e) {
100             // orderly abort
101
throw new AbortException("Execution aborted",e);
102         } catch (IOException JavaDoc e) {
103             throw new PluginManagerException(e.getMessage(),e);
104         }
105     }
106
107     private Xpp3Dom getConfigDom(MojoExecution mojoExecution, MavenProject project) {
108         MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
109         PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor();
110         String JavaDoc goalId = mojoDescriptor.getGoal();
111         String JavaDoc groupId = pluginDescriptor.getGroupId();
112         String JavaDoc artifactId = pluginDescriptor.getArtifactId();
113         String JavaDoc executionId = mojoExecution.getExecutionId();
114         Xpp3Dom dom = project.getGoalConfiguration( groupId, artifactId, executionId, goalId );
115         Xpp3Dom reportDom = project.getReportConfiguration( groupId, artifactId, executionId );
116         dom = Xpp3Dom.mergeXpp3Dom( dom, reportDom );
117         if ( mojoExecution.getConfiguration() != null )
118         {
119             dom = Xpp3Dom.mergeXpp3Dom( dom, mojoExecution.getConfiguration() );
120         }
121         return dom;
122     }
123
124 }
125
Popular Tags