KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > maven > PluginManagerInterceptor


1 package hudson.maven;
2
3 import org.apache.maven.plugin.MojoExecution;
4 import org.apache.maven.project.MavenProject;
5 import org.apache.maven.execution.MavenSession;
6 import org.apache.maven.execution.ReactorManager;
7 import org.apache.maven.monitor.event.EventDispatcher;
8 import org.apache.maven.BuildFailureException;
9 import org.apache.maven.lifecycle.LifecycleExecutionException;
10 import org.apache.maven.lifecycle.LifecycleExecutorListener;
11 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
12 import org.codehaus.plexus.configuration.PlexusConfiguration;
13
14 import java.io.IOException JavaDoc;
15
16 import hudson.model.BuildListener;
17 import hudson.maven.agent.PluginManagerListener;
18 import hudson.maven.agent.AbortException;
19
20 /**
21  * Description in META-INF/plexus/components.xml makes it possible to use this instead of the default
22  * plugin manager.
23  *
24  * @author Kohsuke Kawaguchi
25  */

26 public class PluginManagerInterceptor implements PluginManagerListener, LifecycleExecutorListener {
27
28     private final MavenBuildProxy buildProxy;
29     private final MavenReporter[] reporters;
30     private final BuildListener listener;
31
32     /**
33      * Used to detect when to fire {@link MavenReporter#enterModule}
34      */

35     private MavenProject lastModule;
36
37     /**
38      * Called by {@link MavenBuild} to connect this object to the rest of Hudson objects,
39      * namely {@link MavenBuild}.
40      *
41      * <p>
42      * We can't do this in the constructor because this object is created by
43      * Plexus along with the rest of Maven objects.
44      */

45     public PluginManagerInterceptor(MavenBuildProxy buildProxy, MavenReporter[] reporters, BuildListener listener) {
46         this.buildProxy = buildProxy;
47         this.reporters = reporters;
48         this.listener = listener;
49     }
50
51     /**
52      * Called before the whole build.
53      */

54     public void preBuild(MavenSession session, ReactorManager rm, EventDispatcher dispatcher) throws BuildFailureException, LifecycleExecutionException {
55         try {
56             for (MavenReporter r : reporters)
57                 r.preBuild(buildProxy,rm.getTopLevelProject(),listener);
58         } catch (InterruptedException JavaDoc e) {
59             throw new BuildFailureException("aborted",e);
60         } catch (IOException JavaDoc e) {
61             throw new BuildFailureException(e.getMessage(),e);
62         }
63     }
64
65
66     public void postBuild(MavenSession session, ReactorManager rm, EventDispatcher dispatcher) throws BuildFailureException, LifecycleExecutionException {
67         try {
68             fireLeaveModule();
69             for (MavenReporter r : reporters)
70                 r.postBuild(buildProxy,rm.getTopLevelProject(),listener);
71         } catch (InterruptedException JavaDoc e) {
72             throw new BuildFailureException("aborted",e);
73         } catch (IOException JavaDoc e) {
74             throw new BuildFailureException(e.getMessage(),e);
75         } catch (AbortException e) {
76             throw new BuildFailureException("aborted",e);
77         }
78     }
79
80     public void preExecute(MavenProject project, MojoExecution exec, PlexusConfiguration mergedConfig, ExpressionEvaluator eval) throws IOException JavaDoc, InterruptedException JavaDoc, AbortException {
81         if(lastModule!=project) {
82             // module change
83
fireLeaveModule();
84             fireEnterModule(project);
85         }
86
87         MojoInfo info = new MojoInfo(exec, mergedConfig, eval);
88         for (MavenReporter r : reporters)
89             if(!r.preExecute(buildProxy,project,info,listener))
90                 throw new AbortException(r+" failed");
91     }
92
93     public void postExecute(MavenProject project, MojoExecution exec, PlexusConfiguration mergedConfig, ExpressionEvaluator eval) throws IOException JavaDoc, InterruptedException JavaDoc, AbortException {
94         MojoInfo info = new MojoInfo(exec, mergedConfig, eval);
95
96         for (MavenReporter r : reporters)
97             if(!r.postExecute(buildProxy,project, info,listener))
98                 throw new AbortException(r+" failed");
99     }
100     
101     private void fireEnterModule(MavenProject project) throws InterruptedException JavaDoc, IOException JavaDoc, AbortException {
102         lastModule = project;
103         for (MavenReporter r : reporters)
104             if(!r.enterModule(buildProxy,project,listener))
105                 throw new AbortException(r+" failed");
106     }
107
108     private void fireLeaveModule() throws InterruptedException JavaDoc, IOException JavaDoc, AbortException {
109         if(lastModule!=null) {
110             for (MavenReporter r : reporters)
111                 if(!r.leaveModule(buildProxy,lastModule,listener))
112                     throw new AbortException(r+" failed");
113         }
114     }
115 }
116
Popular Tags