KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileOutputStream JavaDoc;
24 import java.io.FileWriter JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.io.Writer JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Set JavaDoc;
33 import java.util.StringTokenizer JavaDoc;
34 import java.util.TreeMap JavaDoc;
35 import java.util.TreeSet JavaDoc;
36 import org.apache.tools.ant.BuildException;
37 import org.apache.tools.ant.Project;
38 import org.apache.tools.ant.Task;
39 import org.w3c.dom.Document JavaDoc;
40 import org.w3c.dom.Element JavaDoc;
41 import org.w3c.dom.NodeList JavaDoc;
42 import org.xml.sax.InputSource JavaDoc;
43 import org.xml.sax.SAXException JavaDoc;
44
45 /**
46  * Analyzes build.properties and cluster.properties and tries to diagnose any problems.
47  * Also produces a summary of moduleconfig contents which is written to a golden file.
48  * @author Jesse Glick
49  */

50 public final class CheckModuleConfigs extends Task {
51     
52     private File JavaDoc nbroot;
53     
54     public CheckModuleConfigs() {}
55     
56     public void setNbroot(File JavaDoc f) {
57         nbroot = f;
58     }
59     
60     public void execute() throws BuildException {
61         if (nbroot == null) {
62             throw new BuildException("Must define 'nbroot' param", getLocation());
63         }
64         File JavaDoc buildPropertiesFile = new File JavaDoc(nbroot, "nbbuild" + File.separatorChar + "build.properties");
65         File JavaDoc clusterPropertiesFile = new File JavaDoc(nbroot, "nbbuild" + File.separatorChar + "cluster.properties");
66         File JavaDoc goldenFile = new File JavaDoc(nbroot, "ide" + File.separatorChar + "golden" + File.separatorChar + "moduleconfigs.txt");
67         File JavaDoc masterProjectXml = new File JavaDoc(nbroot, "nbbuild" + File.separatorChar + "nbproject" + File.separatorChar + "project.xml");
68         @SuppressWarnings JavaDoc("unchecked")
69         Map JavaDoc<String JavaDoc,String JavaDoc> properties = getProject().getProperties();
70         Map JavaDoc<String JavaDoc,Set JavaDoc<String JavaDoc>> configs = loadModuleConfigs(properties, buildPropertiesFile);
71         Map JavaDoc<String JavaDoc,Set JavaDoc<String JavaDoc>> clusters = loadModuleClusters(properties, clusterPropertiesFile);
72         Set JavaDoc<String JavaDoc> allClusterModules = new TreeSet JavaDoc<String JavaDoc>();
73         for (Set JavaDoc<String JavaDoc> s : clusters.values()) {
74             allClusterModules.addAll(s);
75         }
76         try {
77             writeModuleConfigs(goldenFile, configs, buildPropertiesFile);
78         } catch (IOException JavaDoc e) {
79             throw new BuildException("Could not write to " + goldenFile, e, getLocation());
80         }
81         try {
82             writeMasterProjectXml(masterProjectXml, allClusterModules);
83         } catch (SAXException JavaDoc e) {
84             throw new BuildException("Could not write to " + masterProjectXml, e, getLocation());
85         } catch (IOException JavaDoc e) {
86             throw new BuildException("Could not write to " + masterProjectXml, e, getLocation());
87         }
88         Set JavaDoc<String JavaDoc> s;
89         /* Apparently wanted now?
90         // Check that stable != daily-alpha-nbms:
91         s = new TreeSet((Set) configs.get("stable"));
92         s.retainAll((Set) configs.get("daily-alpha-nbms"));
93         if (!s.isEmpty()) {
94             log(buildPropertiesFile + ": warning: stable and daily-alpha-nbms configs overlap: " + s);
95         }
96         */

97         /* This is not actually desired; just includes everything:
98         // Check that sigtest <= javadoc:
99         s = new TreeSet((Set) configs.get("sigtest"));
100         s.removeAll((Set) configs.get("javadoc"));
101         if (!s.isEmpty()) {
102             log(buildPropertiesFile + ": warning: sigtest config contains entries not in javadoc config: " + s);
103         }
104         */

105         /* This config is no more?
106         // Check that platform-javadoc <= javadoc:
107         s = new TreeSet((Set) configs.get("platform-javadoc"));
108         s.removeAll((Set) configs.get("javadoc"));
109         if (!s.isEmpty()) {
110             log(buildPropertiesFile + ": warning: platform-javadoc config contains entries not in javadoc config: " + s);
111         }
112         */

113         // Check that javadoc <= stable + daily-alpha-nbms:
114
s = new TreeSet JavaDoc<String JavaDoc>(configs.get("javadoc"));
115         s.removeAll(configs.get("stable"));
116         s.removeAll(configs.get("daily-alpha-nbms"));
117         if (!s.isEmpty()) {
118             log(buildPropertiesFile + ": warning: javadoc config contains entries not in stable and daily-alpha-nbms configs: " + s);
119         }
120         /*
121         // Check that platform-javadoc = javadoc where module in platform cluster:
122         Set/ *<String>* / platformJavadoc = (Set) configs.get("platform-javadoc");
123         Set/ *<String>* / platformClusterJavadoc = (Set) configs.get("javadoc");
124         platformClusterJavadoc.retainAll((Set) clusters.get("nb.cluster.platform"));
125         s = new TreeSet(platformJavadoc);
126         s.removeAll(platformClusterJavadoc);
127         if (!s.isEmpty()) {
128             log(buildPropertiesFile + ": warning: platform-javadoc config not equal to javadoc config for platform cluster modules: " + s);
129         }
130         s = new TreeSet(platformClusterJavadoc);
131         s.removeAll(platformJavadoc);
132         if (!s.isEmpty()) {
133             log(buildPropertiesFile + ": warning: platform-javadoc config not equal to javadoc config restricted to platform cluster modules: " + s);
134         }
135         */

136         // Check that stable = modules in enumerated clusters:
137
Set JavaDoc<String JavaDoc> stable = configs.get("stable");
138         s = new TreeSet JavaDoc<String JavaDoc>(stable);
139         s.removeAll(allClusterModules);
140         if (!s.isEmpty()) {
141             log(buildPropertiesFile + ": warning: stable config not equal to listed cluster modules: " + s);
142         }
143         s = new TreeSet JavaDoc<String JavaDoc>(allClusterModules);
144         s.removeAll(stable);
145         if (!s.isEmpty()) {
146             log(buildPropertiesFile + ": warning: stable config not equal to listed cluster modules: " + s);
147         }
148         // Check that platform = modules in platform cluster:
149
Set JavaDoc<String JavaDoc> platform = configs.get("platform");
150         Set JavaDoc<String JavaDoc> platformCluster = clusters.get("nb.cluster.platform");
151         s = new TreeSet JavaDoc<String JavaDoc>(platform);
152         s.removeAll(platformCluster);
153         if (!s.isEmpty()) {
154             log(buildPropertiesFile + ": warning: platform config not equal to platform cluster modules: " + s);
155         }
156         s = new TreeSet JavaDoc<String JavaDoc>(platformCluster);
157         s.removeAll(platform);
158         if (!s.isEmpty()) {
159             log(buildPropertiesFile + ": warning: platform config not equal to platform cluster modules: " + s);
160         }
161     }
162     
163     @SuppressWarnings JavaDoc("unchecked")
164     private Set JavaDoc<String JavaDoc> split(String JavaDoc list) {
165         return new HashSet JavaDoc(Collections.list(new StringTokenizer JavaDoc(list, ", ")));
166     }
167     
168     private Map JavaDoc<String JavaDoc,Set JavaDoc<String JavaDoc>> loadModuleConfigs(Map JavaDoc<String JavaDoc,String JavaDoc> buildProperties, File JavaDoc buildPropertiesFile) {
169         Map JavaDoc<String JavaDoc,Set JavaDoc<String JavaDoc>> configs = new TreeMap JavaDoc<String JavaDoc,Set JavaDoc<String JavaDoc>>();
170         for (String JavaDoc k : buildProperties.keySet()) {
171             String JavaDoc prefix = "config.modules.";
172             if (!k.startsWith(prefix)) {
173                 continue;
174             }
175             String JavaDoc config = k.substring(prefix.length());
176             Set JavaDoc<String JavaDoc> modules = new TreeSet JavaDoc<String JavaDoc>(split(buildProperties.get(k)));
177             String JavaDoc fixedK = "config.fixedmodules." + config;
178             String JavaDoc fixed = buildProperties.get(fixedK);
179             if (fixed != null) {
180                 modules.addAll(split(fixed));
181             } else {
182                 log(buildPropertiesFile + ": warning: have " + k + " but no " + fixedK, Project.MSG_WARN);
183             }
184             configs.put(config, modules);
185         }
186         return configs;
187     }
188
189     private void writeModuleConfigs(File JavaDoc goldenFile, Map JavaDoc<String JavaDoc,Set JavaDoc<String JavaDoc>> configs, File JavaDoc buildPropertiesFile) throws IOException JavaDoc {
190         log("Writing moduleconfigs " + configs.keySet() + " from " + buildPropertiesFile + " to " + goldenFile);
191         Writer JavaDoc w = new FileWriter JavaDoc(goldenFile); // default encoding OK
192
try {
193             PrintWriter JavaDoc pw = new PrintWriter JavaDoc(w);
194             pw.println("# To update, run: ant -f nbbuild/build.xml check-module-configs");
195             for (Map.Entry JavaDoc<String JavaDoc,Set JavaDoc<String JavaDoc>> entry : configs.entrySet()) {
196                 String JavaDoc config = entry.getKey();
197                 for (String JavaDoc module : entry.getValue()) {
198                     pw.println(config + ':' + module);
199                 }
200             }
201             pw.flush();
202         } finally {
203             w.close();
204         }
205     }
206
207     private Map JavaDoc<String JavaDoc,Set JavaDoc<String JavaDoc>> loadModuleClusters(Map JavaDoc<String JavaDoc,String JavaDoc> clusterProperties, File JavaDoc clusterPropertiesFile) {
208         String JavaDoc l = clusterProperties.get("nb.clusters.list");
209         if (l == null) {
210             log(clusterPropertiesFile + ": warning: no definition for nb.clusters.list", Project.MSG_WARN);
211             return Collections.emptyMap();
212         }
213         Map JavaDoc<String JavaDoc,Set JavaDoc<String JavaDoc>> clusters = new TreeMap JavaDoc<String JavaDoc,Set JavaDoc<String JavaDoc>>();
214         for (String JavaDoc cluster : split(l)) {
215             l = clusterProperties.get(cluster);
216             if (l == null) {
217                 log(clusterPropertiesFile + ": warning: no definition for " + cluster, Project.MSG_WARN);
218                 continue;
219             }
220             clusters.put(cluster, new TreeSet JavaDoc<String JavaDoc>(split(l)));
221         }
222         return clusters;
223     }
224
225     private void writeMasterProjectXml(File JavaDoc masterProjectXml, Set JavaDoc<String JavaDoc> allClusterModules) throws IOException JavaDoc, SAXException JavaDoc {
226         log("Writing module list to " + masterProjectXml);
227         Document JavaDoc doc = XMLUtil.parse(new InputSource JavaDoc(masterProjectXml.toURI().toString()), false, true, null, null);
228         NodeList JavaDoc nl = doc.getElementsByTagName("subprojects");
229         if (nl.getLength() != 1) {
230             throw new IOException JavaDoc("No or multiple <subprojects>");
231         }
232         Element JavaDoc sp = (Element JavaDoc) nl.item(0);
233         nl = sp.getChildNodes();
234         while (nl.getLength() > 0) {
235             sp.removeChild(nl.item(0));
236         }
237         sp.appendChild(doc.createComment(" To update, run: ant -f nbbuild/build.xml check-module-configs "));
238         for (String JavaDoc module : allClusterModules) {
239             if (new File JavaDoc(nbroot, (module + "/nbproject/project.xml").replace('/', File.separatorChar)).isFile()) {
240                 Element JavaDoc e = doc.createElementNS("http://www.netbeans.org/ns/freeform-project/1", "project");
241                 e.appendChild(doc.createTextNode("../" + module));
242                 sp.appendChild(e);
243             } else {
244                 sp.appendChild(doc.createComment(" Unprojectized: " + module + " "));
245             }
246         }
247         OutputStream JavaDoc os = new FileOutputStream JavaDoc(masterProjectXml);
248         try {
249             XMLUtil.write(doc, os);
250         } finally {
251             os.close();
252         }
253     }
254     
255 }
256
Popular Tags