KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > maven > MavenModuleSet


1 package hudson.maven;
2
3 import hudson.FilePath;
4 import hudson.Util;
5 import hudson.tasks.Maven;
6 import hudson.tasks.Maven.MavenInstallation;
7 import hudson.model.AbstractProject;
8 import hudson.model.DependencyGraph;
9 import hudson.model.Executor;
10 import hudson.model.Hudson;
11 import hudson.model.Item;
12 import hudson.model.ItemGroup;
13 import hudson.model.Items;
14 import hudson.model.Job;
15 import hudson.model.Node;
16 import hudson.model.SCMedItem;
17 import hudson.model.TopLevelItem;
18 import hudson.model.TopLevelItemDescriptor;
19 import hudson.util.CopyOnWriteMap;
20 import org.kohsuke.stapler.StaplerRequest;
21 import org.kohsuke.stapler.StaplerResponse;
22
23 import javax.servlet.ServletException JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileFilter JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.logging.Level JavaDoc;
32 import java.util.logging.Logger JavaDoc;
33
34 /**
35  * Group of {@link MavenModule}s.
36  *
37  * <p>
38  * This corresponds to the group of Maven POMs that constitute a single
39  * tree of projects. This group serves as the grouping of those related
40  * modules.
41  *
42  * @author Kohsuke Kawaguchi
43  */

44 public final class MavenModuleSet extends AbstractProject<MavenModuleSet,MavenModuleSetBuild> implements TopLevelItem, ItemGroup<MavenModule>, SCMedItem {
45     /**
46      * All {@link MavenModule}s, keyed by their {@link MavenModule#getModuleName()} module name}s.
47      */

48     transient /*final*/ Map<ModuleName,MavenModule> modules = new CopyOnWriteMap.Tree<ModuleName,MavenModule>();
49
50     /**
51      * Name of the top-level module.
52      */

53     private ModuleName rootModule;
54
55     private String JavaDoc rootPOM;
56
57     private String JavaDoc goals;
58
59     /**
60      * Default goals specified in POM. Can be null.
61      */

62     private String JavaDoc defaultGoals;
63
64     /**
65      * Identifies {@link MavenInstallation} to be used.
66      * Null to indicate 'default' maven.
67      */

68     private String JavaDoc mavenName;
69
70     public MavenModuleSet(String JavaDoc name) {
71         super(Hudson.getInstance(),name);
72     }
73
74     public String JavaDoc getUrlChildPrefix() {
75         // seemingly redundant "./" is used to make sure that ':' is not interpreted as the scheme identifier
76
return ".";
77     }
78
79     public Hudson getParent() {
80         return Hudson.getInstance();
81     }
82
83     public Collection JavaDoc<MavenModule> getItems() {
84         return modules.values();
85     }
86
87     public Collection JavaDoc<MavenModule> getModules() {
88         return getItems();
89     }
90
91     public MavenModule getItem(String JavaDoc name) {
92         return modules.get(ModuleName.fromString(name));
93     }
94
95     public MavenModule getModule(String JavaDoc name) {
96         return getItem(name);
97     }
98
99     public Object JavaDoc getDynamic(String JavaDoc token, StaplerRequest req, StaplerResponse rsp) {
100         if(ModuleName.isValid(token))
101             return getModule(token);
102         return super.getDynamic(token,req,rsp);
103     }
104
105     public File JavaDoc getRootDirFor(MavenModule child) {
106         return new File JavaDoc(new File JavaDoc(getRootDir(),"modules"),child.getModuleName().toFileSystemName());
107     }
108
109     public Collection JavaDoc<Job> getAllJobs() {
110         Set JavaDoc<Job> jobs = new HashSet JavaDoc<Job>(getItems());
111         jobs.add(this);
112         return jobs;
113     }
114
115     @Override JavaDoc
116     protected boolean isBuildBlocked() {
117         if(super.isBuildBlocked())
118             return true;
119         // updating the workspace (=our build) cannot be done
120
// if someone else is still touching the workspace.
121
for (MavenModule m : modules.values()) {
122             if(m.isBuilding())
123                 return true;
124         }
125         return false;
126     }
127
128     /**
129      * Gets the workspace of this job.
130      */

131     public FilePath getWorkspace() {
132         Node node = getLastBuiltOn();
133         if(node==null) node = Hudson.getInstance();
134         return node.getWorkspaceFor(this);
135     }
136
137     @Override JavaDoc
138     public MavenModuleSetBuild newBuild() throws IOException JavaDoc {
139         MavenModuleSetBuild lastBuild = new MavenModuleSetBuild(this);
140         builds.put(lastBuild);
141         return lastBuild;
142     }
143
144     @Override JavaDoc
145     protected MavenModuleSetBuild loadBuild(File JavaDoc dir) throws IOException JavaDoc {
146         return new MavenModuleSetBuild(this,dir);
147     }
148
149     @Override JavaDoc
150     public boolean isFingerprintConfigured() {
151         return true;
152     }
153
154     public void onLoad(ItemGroup<? extends Item> parent, String JavaDoc name) throws IOException JavaDoc {
155         super.onLoad(parent, name);
156
157         File JavaDoc modulesDir = new File JavaDoc(getRootDir(),"modules");
158         modulesDir.mkdirs(); // make sure it exists
159

160         File JavaDoc[] subdirs = modulesDir.listFiles(new FileFilter JavaDoc() {
161             public boolean accept(File JavaDoc child) {
162                 return child.isDirectory();
163             }
164         });
165         modules = new CopyOnWriteMap.Tree<ModuleName,MavenModule>();
166         for (File JavaDoc subdir : subdirs) {
167             try {
168                 MavenModule item = (MavenModule) Items.load(this,subdir);
169                 modules.put(item.getModuleName(), item);
170             } catch (IOException JavaDoc e) {
171                 e.printStackTrace(); // TODO: logging
172
}
173         }
174     }
175
176     /**
177      * To make it easy to grasp relationship among modules
178      * and the module set, we'll align the build numbers of
179      * all the modules.
180      *
181      * <p>
182      * This method is invoked from {@link Executor#run()},
183      * and because of the mutual exclusion among {@link MavenModuleSetBuild}
184      * and {@link MavenBuild}, we can safely touch all the modules.
185      */

186     public synchronized int assignBuildNumber() throws IOException JavaDoc {
187         // determine the next value
188
updateNextBuildNumber();
189
190         return super.assignBuildNumber();
191     }
192
193     public synchronized int getNextBuildNumber() {
194         try {
195             updateNextBuildNumber();
196         } catch (IOException JavaDoc e) {
197             LOGGER.log(Level.SEVERE,"Failed to save the next build number",e);
198         }
199         return nextBuildNumber;
200     }
201
202     private void updateNextBuildNumber() throws IOException JavaDoc {
203         int next = this.nextBuildNumber;
204         for (MavenModule m : modules.values())
205             next = Math.max(next,m.getNextBuildNumber());
206
207         if(this.nextBuildNumber!=next) {
208             this.nextBuildNumber=next;
209             this.saveNextBuildNumber();
210         }
211
212         for (MavenModule m : modules.values())
213             m.updateNextBuildNumber(next);
214     }
215
216     protected void buildDependencyGraph(DependencyGraph graph) {
217         // no dependency for this.
218
}
219
220     public MavenModule getRootModule() {
221         return modules.get(rootModule);
222     }
223
224     /**
225      * Gets the location of top-level <tt>pom.xml</tt> relative to the workspace root.
226      */

227     public String JavaDoc getRootPOM() {
228         if(rootPOM==null) return "pom.xml";
229         return rootPOM;
230     }
231
232     public AbstractProject<?,?> asProject() {
233         return this;
234     }
235
236     /**
237      * Gets the list of goals to execute.
238      */

239     public String JavaDoc getGoals() {
240         if(goals==null) {
241             if(defaultGoals!=null) return defaultGoals;
242             return "install";
243         }
244         return goals;
245     }
246
247     /**
248      * Gets the Maven to invoke.
249      * If null, we pick any random Maven installation.
250      */

251     public MavenInstallation getMaven() {
252         for( MavenInstallation i : DESCRIPTOR.getMavenDescriptor().getInstallations() ) {
253             if(mavenName==null || i.getName().equals(mavenName))
254                 return i;
255         }
256         return null;
257     }
258
259     /**
260      * Gets the list of goals specified by the user,
261      * without taking inheritance and POM default goals
262      * into account.
263      *
264      * <p>
265      * This is only used to present the UI screen, and in
266      * all the other cases {@link #getGoals()} should be used.
267      */

268     public String JavaDoc getUserConfiguredGoals() {
269         return goals;
270     }
271
272     /*package*/ void reconfigure(PomInfo rootPom) throws IOException JavaDoc {
273         if(this.rootModule!=null && this.rootModule.equals(rootModule))
274             return; // no change
275
this.rootModule = rootPom.name;
276         this.defaultGoals = rootPom.defaultGoal;
277         save();
278     }
279
280 //
281
//
282
// Web methods
283
//
284
//
285

286     public synchronized void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws IOException JavaDoc, ServletException JavaDoc {
287         if(!Hudson.adminCheck(req,rsp))
288             return;
289
290         rootPOM = Util.fixEmpty(req.getParameter("rootPOM").trim());
291         if(rootPOM.equals("pom.xml")) rootPOM=null; // normalization
292

293         goals = Util.fixEmpty(req.getParameter("goals").trim());
294         mavenName = req.getParameter("maven_version");
295
296         super.doConfigSubmit(req,rsp);
297
298         save();
299     }
300
301     public TopLevelItemDescriptor getDescriptor() {
302         return DESCRIPTOR;
303     }
304
305     public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl();
306
307     public static final class DescriptorImpl extends TopLevelItemDescriptor {
308         private DescriptorImpl() {
309             super(MavenModuleSet.class);
310         }
311
312         public String JavaDoc getDisplayName() {
313             return "Build a maven2 project (alpha)";
314         }
315
316         public MavenModuleSet newInstance(String JavaDoc name) {
317             return new MavenModuleSet(name);
318         }
319         
320         public Maven.DescriptorImpl getMavenDescriptor() {
321             return Maven.DESCRIPTOR;
322         }
323     }
324
325     private static final Logger JavaDoc LOGGER = Logger.getLogger(MavenModuleSet.class.getName());
326 }
327
Popular Tags