KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > nbbuild > SortSuiteModules


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.nbbuild;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.TreeMap JavaDoc;
31 import org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.Task;
33 import org.apache.tools.ant.types.Path;
34 import org.w3c.dom.Document JavaDoc;
35 import org.w3c.dom.Element JavaDoc;
36 import org.xml.sax.InputSource JavaDoc;
37 import org.xml.sax.SAXException JavaDoc;
38
39 /**
40  * Task to sort the list of modules in a suite by their declared build dependencies.
41  * @author Jesse Glick
42  */

43 public class SortSuiteModules extends Task {
44     private boolean sortTests;
45     private Path unsortedModules;
46     /**
47      * Set a list of modules in the suite.
48      * Each entry should be a project base directory.
49      */

50     public void setUnsortedModules(Path unsortedModules) {
51         this.unsortedModules = unsortedModules;
52     }
53     
54     private String JavaDoc sortedModulesProperty;
55     /**
56      * Set a property name in which to store a sorted path of module base directories.
57      */

58     public void setSortedModulesProperty(String JavaDoc sortedModulesProperty) {
59         this.sortedModulesProperty = sortedModulesProperty;
60     }
61     
62     /** Is enabled sorting test dependencies?
63      */

64     public boolean isSortTests() {
65         return sortTests;
66     }
67
68     /** Enable or disable sorting test dependenciens. Default value is false.
69      */

70     public void setSortTests(boolean sortTests) {
71         this.sortTests = sortTests;
72     }
73     
74     public SortSuiteModules() {}
75     
76     public void execute() throws BuildException {
77         if (unsortedModules == null) {
78             throw new BuildException("Must set unsortedModules");
79         }
80         if (sortedModulesProperty == null) {
81             throw new BuildException("Must set sortedModulesProperty");
82         }
83         Map JavaDoc<String JavaDoc,File JavaDoc> basedirsByCNB = new TreeMap JavaDoc<String JavaDoc,File JavaDoc>();
84         Map JavaDoc<String JavaDoc,List JavaDoc<String JavaDoc>> buildDeps = new HashMap JavaDoc<String JavaDoc,List JavaDoc<String JavaDoc>>();
85         String JavaDoc[] pieces = unsortedModules.list();
86         for (int i = 0; i < pieces.length; i++) {
87             File JavaDoc d = new File JavaDoc(pieces[i]);
88             File JavaDoc projectXml = new File JavaDoc(d, "nbproject" + File.separatorChar + "project.xml");
89             if (!projectXml.isFile()) {
90                 throw new BuildException("Cannot open " + projectXml, getLocation());
91             }
92             Document JavaDoc doc;
93             try {
94                 doc = XMLUtil.parse(new InputSource JavaDoc(projectXml.toURI().toString()), false, true, null, null);
95             } catch (IOException JavaDoc e) {
96                 throw new BuildException("Error parsing " + projectXml + ": " + e, e, getLocation());
97             } catch (SAXException JavaDoc e) {
98                 throw new BuildException("Error parsing " + projectXml + ": " + e, e, getLocation());
99             }
100             Element JavaDoc config = XMLUtil.findElement(doc.getDocumentElement(), "configuration", ParseProjectXml.PROJECT_NS);
101             if (config == null) {
102                 throw new BuildException("Malformed project file " + projectXml, getLocation());
103             }
104             Element JavaDoc data = ParseProjectXml.findNBMElement(config, "data");
105             if (data == null) {
106                 throw new BuildException("Malformed project file " + projectXml, getLocation());
107             }
108             Element JavaDoc cnbEl = ParseProjectXml.findNBMElement(data, "code-name-base");
109             if (cnbEl == null) {
110                 throw new BuildException("Malformed project file " + projectXml, getLocation());
111             }
112             String JavaDoc cnb = XMLUtil.findText(cnbEl);
113             basedirsByCNB.put(cnb, d);
114             List JavaDoc<String JavaDoc> deps = new LinkedList JavaDoc<String JavaDoc>();
115             Element JavaDoc depsEl = ParseProjectXml.findNBMElement(data, "module-dependencies");
116             if (depsEl == null) {
117                 throw new BuildException("Malformed project file " + projectXml, getLocation());
118             }
119             Iterator JavaDoc it = XMLUtil.findSubElements(depsEl).iterator();
120             while (it.hasNext()) {
121                 Element JavaDoc dep = (Element JavaDoc) it.next();
122                 if (ParseProjectXml.findNBMElement(dep, "build-prerequisite") == null) {
123                     continue;
124                 }
125                 Element JavaDoc cnbEl2 = ParseProjectXml.findNBMElement(dep, "code-name-base");
126                 if (cnbEl2 == null) {
127                     throw new BuildException("Malformed project file " + projectXml, getLocation());
128                 }
129                 String JavaDoc cnb2 = XMLUtil.findText(cnbEl2);
130                 deps.add(cnb2);
131             }
132             buildDeps.put(cnb, deps);
133             
134             // create test dependencies
135
if (isSortTests()) {
136                 Element JavaDoc testDepsEl = ParseProjectXml.findNBMElement(data,"test-dependencies");
137                 if (testDepsEl != null) {
138                     // <test-type>
139
Iterator JavaDoc itTType = XMLUtil.findSubElements(testDepsEl).iterator();
140                     while (itTType.hasNext()) {
141                         Iterator JavaDoc itt = XMLUtil.findSubElements((Element JavaDoc)itTType.next()).iterator();
142                         while (itt.hasNext()) {
143                             Element JavaDoc dep = (Element JavaDoc) itt.next();
144                             if (ParseProjectXml.findNBMElement(dep, "test") == null) {
145                                 continue;
146                             }
147                             Element JavaDoc cnbEl2 = ParseProjectXml.findNBMElement(dep, "code-name-base");
148                             if (cnbEl2 == null) {
149                                 throw new BuildException("No cobase found for test-dependency");
150                             }
151                             String JavaDoc cnb2 = XMLUtil.findText(cnbEl2);
152                             deps.add(cnb2);
153                         }
154                     }
155                 }
156             }
157         }
158         for (List JavaDoc<String JavaDoc> deps: buildDeps.values()) {
159             deps.retainAll(basedirsByCNB.keySet());
160         }
161         // Stolen from org.openide.util.Utilities.topologicalSort, with various simplifications:
162
List JavaDoc<String JavaDoc> cnbs = new ArrayList JavaDoc<String JavaDoc>();
163         List JavaDoc<String JavaDoc> cRev = new ArrayList JavaDoc<String JavaDoc>(basedirsByCNB.keySet());
164         Map JavaDoc<String JavaDoc,Boolean JavaDoc> finished = new HashMap JavaDoc<String JavaDoc,Boolean JavaDoc>();
165         for (String JavaDoc s: cRev) {
166             if (!visit(s, buildDeps, finished, cnbs)) {
167                 throw new BuildException("Cycles detected in dependency graph, cannot sort", getLocation());
168             }
169         }
170         StringBuffer JavaDoc path = new StringBuffer JavaDoc();
171         for (String JavaDoc cnb: cnbs) {
172             assert basedirsByCNB.containsKey(cnb);
173             if (path.length() > 0) {
174                 path.append(File.pathSeparatorChar);
175             }
176             path.append(basedirsByCNB.get(cnb).getAbsolutePath());
177         }
178         getProject().setNewProperty(sortedModulesProperty, path.toString());
179     }
180     
181     private static <String> boolean visit(String JavaDoc node, Map JavaDoc<String JavaDoc,List JavaDoc<String JavaDoc>> edges, Map JavaDoc<String JavaDoc,Boolean JavaDoc> finished, List JavaDoc<String JavaDoc> r) {
182         Boolean JavaDoc b = finished.get(node);
183         if (b != null) {
184             return b.booleanValue();
185         }
186         List JavaDoc<String JavaDoc> e = edges.get(node);
187         if (e != null) {
188             finished.put(node, Boolean.FALSE);
189             for (String JavaDoc s: e) {
190                 if (!visit(s, edges, finished, r)) {
191                     return false;
192                 }
193             }
194         }
195         finished.put(node, Boolean.TRUE);
196         r.add(node);
197         return true;
198     }
199     
200 }
201
Popular Tags