KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > maven > MavenModule


1 package hudson.maven;
2
3 import hudson.CopyOnWrite;
4 import hudson.FilePath;
5 import hudson.Util;
6 import hudson.model.AbstractProject;
7 import hudson.model.Action;
8 import hudson.model.DependencyGraph;
9 import hudson.model.Descriptor;
10 import hudson.model.Descriptor.FormException;
11 import hudson.model.Hudson;
12 import hudson.model.Item;
13 import hudson.model.ItemGroup;
14 import hudson.model.Job;
15 import hudson.model.Node;
16 import hudson.triggers.Trigger;
17 import hudson.util.DescribableList;
18 import org.apache.maven.project.MavenProject;
19 import org.kohsuke.stapler.StaplerRequest;
20 import org.kohsuke.stapler.StaplerResponse;
21
22 import javax.servlet.ServletException JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.Vector JavaDoc;
31
32 /**
33  * {@link Job} that builds projects based on Maven2.
34  *
35  * @author Kohsuke Kawaguchi
36  */

37 public final class MavenModule extends AbstractProject<MavenModule,MavenBuild> implements DescribableList.Owner {
38     private DescribableList<MavenReporter,Descriptor<MavenReporter>> reporters =
39         new DescribableList<MavenReporter,Descriptor<MavenReporter>>(this);
40
41     /**
42      * Name taken from {@link MavenProject#getName()}.
43      */

44     private String JavaDoc displayName;
45
46     private transient ModuleName moduleName;
47
48     private String JavaDoc relativePath;
49
50     /**
51      * If this module has goals specified by itself.
52      * Otherwise leave it null to use the default goals specified in the parent.
53      */

54     private String JavaDoc goals;
55
56     /**
57      * List of modules that this module declares direct dependencies on.
58      */

59     @CopyOnWrite
60     private Set JavaDoc<ModuleName> dependencies;
61
62     /**
63      * {@link Action}s contributed from {@link #triggers} and {@link MavenBuild#projectActionReporters}
64      * from the last build.
65      *
66      * We don't want to persist them separately, and these actions
67      * come and go as configuration change, so it's kept separate.
68      */

69     private transient /*final*/ List<Action> transientActions = new Vector JavaDoc<Action>();
70
71     /*package*/ MavenModule(MavenModuleSet parent, PomInfo pom, int firstBuildNumber) throws IOException JavaDoc {
72         super(parent, pom.name.toFileSystemName());
73         reconfigure(pom);
74         updateNextBuildNumber(firstBuildNumber);
75     }
76
77     /**
78      * Called to update the module with the new POM.
79      * <p>
80      * This method is invoked on {@link MavenModule} that has the matching
81      * {@link ModuleName}.
82      */

83     /*package*/ final void reconfigure(PomInfo pom) {
84         this.displayName = pom.displayName;
85         this.relativePath = pom.relativePath;
86         this.dependencies = pom.dependencies;
87     }
88
89     protected void doSetName(String JavaDoc name) {
90         moduleName = ModuleName.fromFileSystemName(name);
91         super.doSetName(moduleName.toString());
92     }
93
94     @Override JavaDoc
95     public void onLoad(ItemGroup<? extends Item> parent, String JavaDoc name) throws IOException JavaDoc {
96         super.onLoad(parent,name);
97         if(reporters==null)
98             reporters = new DescribableList<MavenReporter, Descriptor<MavenReporter>>(this);
99         reporters.setOwner(this);
100         if(dependencies==null)
101             dependencies = Collections.emptySet();
102         updateTransientActions();
103     }
104
105     /**
106      * Relative path to this module's root directory
107      * from {@link MavenModuleSet#getWorkspace()}.
108      *
109      * The path separator is normalized to '/'.
110      */

111     public String JavaDoc getRelativePath() {
112         return relativePath;
113     }
114
115     /**
116      * Gets the list of goals to execute for this module.
117      */

118     public String JavaDoc getGoals() {
119         if(goals!=null) return goals;
120         return getParent().getGoals();
121     }
122
123     /**
124      * Gets the list of goals specified by the user,
125      * without taking inheritance and POM default goals
126      * into account.
127      *
128      * <p>
129      * This is only used to present the UI screen, and in
130      * all the other cases {@link #getGoals()} should be used.
131      */

132     public String JavaDoc getUserConfiguredGoals() {
133         return goals;
134     }
135
136     @Override JavaDoc
137     public FilePath getWorkspace() {
138         return getParent().getModuleRoot().child(relativePath);
139     }
140
141     public ModuleName getModuleName() {
142         return moduleName;
143     }
144
145     @Override JavaDoc
146     public String JavaDoc getDisplayName() {
147         return displayName;
148     }
149
150     public MavenModuleSet getParent() {
151         return (MavenModuleSet)super.getParent();
152     }
153
154     /*package*/ void updateNextBuildNumber(int next) throws IOException JavaDoc {
155         if(next>nextBuildNumber) {
156             this.nextBuildNumber = next;
157             saveNextBuildNumber();
158         }
159     }
160
161     /**
162      * {@link MavenModule} uses the workspace of the {@link MavenModuleSet},
163      * so it always needs to be built on the same slave as the parent.
164      */

165     public Node getAssignedNode() {
166         return getParent().getLastBuiltOn();
167     }
168
169     @Override JavaDoc
170     public MavenBuild newBuild() throws IOException JavaDoc {
171         MavenBuild lastBuild = new MavenBuild(this);
172         builds.put(lastBuild);
173         return lastBuild;
174     }
175
176     @Override JavaDoc
177     protected MavenBuild loadBuild(File JavaDoc dir) throws IOException JavaDoc {
178         return new MavenBuild(this,dir);
179     }
180
181     @Override JavaDoc
182     protected boolean isBuildBlocked() {
183         if(super.isBuildBlocked())
184             return true;
185
186         // if the module set's new build is planned or in progress,
187
// don't start a new build. Otherwise on a busy maven project
188
// MavenModuleSet will never get a chance to run.
189
MavenModuleSet p = getParent();
190         return p.isBuilding() || p.isInQueue();
191     }
192
193     @Override JavaDoc
194     public boolean isFingerprintConfigured() {
195         return true;
196     }
197
198     protected void buildDependencyGraph(DependencyGraph graph) {
199         Map JavaDoc<ModuleName,MavenModule> modules = new HashMap JavaDoc<ModuleName,MavenModule>();
200
201         for (MavenModule m : Hudson.getInstance().getAllItems(MavenModule.class))
202             modules.put(m.getModuleName(),m);
203
204         for (ModuleName d : dependencies) {
205             MavenModule src = modules.get(d);
206             if(src!=null)
207                 graph.addDependency(src,this);
208         }
209     }
210
211     public synchronized List<Action> getActions() {
212         // add all the transient actions, too
213
List<Action> actions = new Vector JavaDoc<Action>(super.getActions());
214         actions.addAll(transientActions);
215         return actions;
216     }
217
218     /*package*/ void updateTransientActions() {
219         if(transientActions==null)
220             transientActions = new Vector JavaDoc<Action>(); // happens when loaded from disk
221
synchronized(transientActions) {
222             transientActions.clear();
223
224             MavenBuild lb = getLastBuild();
225             if(lb==null) return;
226             
227             List<MavenReporter> list = lb.projectActionReporters;
228             if(list!=null)
229                 for (MavenReporter step : list) {
230                     Action a = step.getProjectAction(this);
231                     if(a!=null)
232                         transientActions.add(a);
233                 }
234             for (Trigger trigger : triggers) {
235                 Action a = trigger.getProjectAction();
236                 if(a!=null)
237                     transientActions.add(a);
238             }
239         }
240     }
241
242     /**
243      * List of active {@link MavenReporter}s configured for this project.
244      */

245     public DescribableList<MavenReporter, Descriptor<MavenReporter>> getReporters() {
246         return reporters;
247     }
248
249     public void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws IOException JavaDoc, ServletException JavaDoc {
250         super.doConfigSubmit(req, rsp);
251
252         try {
253             reporters.rebuild(req,MavenReporters.getConfigurableList(),"reporter");
254         } catch (FormException e) {
255             sendError(e,req,rsp);
256         }
257
258         goals = Util.fixEmpty(req.getParameter("goals").trim());
259
260         save();
261
262         // dependency setting might have been changed by the user, so rebuild.
263
Hudson.getInstance().rebuildDependencyGraph();
264     }
265
266     /**
267      * Marks this build as disabled.
268      */

269     public void disable() throws IOException JavaDoc {
270         if(!disabled) {
271             disabled = true;
272             save();
273         }
274     }
275 }
276
Popular Tags