KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
24
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.Project;
27 import org.apache.tools.ant.Target;
28 import org.apache.tools.ant.Task;
29
30 /** Pseudo-task to unpack a set of modules.
31  * Causes the containing target to both depend on the building of the modules in
32  * the first place; and then to unpack them all to a certain location.
33  *
34  * @author various people
35  *
36  * 2002-07-31: Rudolf Balada Added build success granularity (Issue 9701),
37  * fixed modules can't fail, "modules" can fail
38  */

39 public class NbMerge extends Task {
40     
41     private File JavaDoc dest;
42     private Vector<String JavaDoc> modules = new Vector<String JavaDoc> (); // list of modules defined by build.xml
43
private Vector<String JavaDoc> buildmodules = new Vector<String JavaDoc> (); // list of modules which will be built
44
private Vector<String JavaDoc> fixedmodules = new Vector<String JavaDoc> (); // list of fixed modules defined in build.xml
45
private Vector<String JavaDoc> buildfixedmodules = new Vector<String JavaDoc> (); // List of fixed modules which will be built
46
private Vector<String JavaDoc> failedmodules = new Vector<String JavaDoc> (); // List of failed modules
47
private Vector<String JavaDoc> builtmodules = new Vector<String JavaDoc> (); // list of successfully built modules
48
private Vector<String JavaDoc> mergemodules = new Vector<String JavaDoc> (); // list of successfully built modules
49
private Vector<String JavaDoc> builttargets = new Vector<String JavaDoc> (); // list of successfully built targets
50
private String JavaDoc targetprefix = "all-";
51     private List<File JavaDoc> topdirs = new ArrayList<File JavaDoc> ();
52     private List<Suppress> suppress = new LinkedList<Suppress> ();
53     private boolean failonerror = true; // false = enable build success granularity
54
private boolean mergedependentmodules = false; // merge also dependent modules
55
private String JavaDoc dummyName;
56     private Target dummy;
57     private Hashtable targets;
58     private String JavaDoc builtmodulesproperty = ""; // if set, update property of the name
59
// to list of successfuly built modules
60

61     /** Target directory to unpack to (top of IDE installation). */
62     public void setDest (File JavaDoc f) {
63         dest = f;
64     }
65
66     /** Enable/disable build failing */
67     public void setFailOnError (boolean b) {
68         failonerror = b;
69     }
70
71     /** At the end of task, set system property to the list of successfuly
72      * built modules
73      */

74     public void setBuiltModulesProperty (String JavaDoc s) {
75         builtmodulesproperty = s;
76     }
77     
78     /** Enable/Disable merging also dependencies */
79     public void setMergeDependentModules (boolean b) {
80         mergedependentmodules = b;
81     }
82     
83     /** Comma-separated list of fixed modules to include. */
84     public void setFixedModules (String JavaDoc s) {
85         StringTokenizer tok = new StringTokenizer (s, ", ");
86         fixedmodules = new Vector<String JavaDoc> ();
87         while (tok.hasMoreTokens ())
88             fixedmodules.addElement (tok.nextToken ());
89     }
90     
91     /** Comma-separated list of modules to include. */
92     public void setModules (String JavaDoc s) {
93         StringTokenizer tok = new StringTokenizer (s, ", ");
94         modules = new Vector<String JavaDoc> ();
95         while (tok.hasMoreTokens ())
96             modules.addElement (tok.nextToken ());
97     }
98     
99     /** String which will have a module name appended to it.
100      * This will form a target in the same project which should
101      * create the <samp>netbeans/</samp> subdirectory.
102      */

103     public void setTargetprefix (String JavaDoc s) {
104         targetprefix = s;
105     }
106     
107     /** Set the top directory.
108      * There should be subdirectories under this for each named module.
109      */

110     public void setTopdir (File JavaDoc t) {
111         topdirs.add (t);
112     }
113
114     /** Nested topdir addition. */
115     public class Topdir {
116         /** Path to an extra topdir. */
117         public void setPath (File JavaDoc t) {
118             topdirs.add (t);
119         }
120     }
121     /** Add a nested topdir.
122      * If there is more than one topdir total, build products
123      * may be taken from any of them, including from multiple places
124      * for the same module. (Later topdirs may override build
125      * products in earlier topdirs.)
126      */

127     public Topdir createTopdir () {
128         return new Topdir ();
129     }
130
131     /** Locale to suppress. */
132     public class Suppress {
133         // [PENDING] also support branding here
134
String JavaDoc locale;
135         String JavaDoc iftest;
136         String JavaDoc unlesstest;
137         /** Name of the locale, e.g. <samp>ja</samp>. */
138         public void setLocale (String JavaDoc l) {
139             locale = l;
140         }
141         /** Property which if set will enable the suppression. */
142         public void setIf (String JavaDoc p) {
143             iftest = p;
144         }
145         /** Property which if set will disable the suppression. */
146         public void setUnless (String JavaDoc p) {
147             unlesstest = p;
148         }
149     }
150     /** Add a locale to suppress.
151      * Files matching this locale suffix will not be merged in.
152      * E.g. for the locale <samp>ja</samp>, this will exclude
153      * all files and directories ending in <samp>_ja</samp> as well
154      * as files ending in <samp>_ja.</samp> plus some extension.
155      */

156     public Suppress createSuppress () {
157         Suppress s = new Suppress ();
158         suppress.add (s);
159         return s;
160     }
161
162     
163     /** Execute targets which cannot fail and though throw BuildException */
164     private void fixedModulesBuild() throws BuildException {
165         // Somewhat convoluted code because Project.executeTargets does not
166
// eliminate duplicates when analyzing dependencies! Ecch.
167
// build fixed modules first
168
dummy = new Target ();
169         dummyName = "nbmerge-" + getOwningTarget().getName();
170         targets = getProject().getTargets();
171         while (targets.contains (dummyName))
172             dummyName += "-x";
173         dummy.setName (dummyName);
174         for (String JavaDoc fixedmodule : buildfixedmodules) {
175             dummy.addDependency (targetprefix + fixedmodule);
176         }
177         getProject().addTarget(dummy);
178
179         getProject().setProperty("fixedmodules-built", "1" );
180         @SuppressWarnings JavaDoc("unchecked")
181         Vector<Target> fullList = getProject().topoSort(dummyName, targets);
182         // Now remove earlier ones: already done.
183
Vector doneList = getProject().topoSort(getOwningTarget().getName(), targets);
184         List<Target> todo = new ArrayList<Target>(fullList.subList(0, fullList.indexOf(dummy)));
185         todo.removeAll(doneList.subList(0, doneList.indexOf(getOwningTarget())));
186         log("Going to execute targets " + todo);
187         for (Target nexttargit: todo) {
188             String JavaDoc targetname = nexttargit.getName();
189             if ( builttargets.indexOf(targetname) < 0 ) {
190                 // XXX poor replacement for Project.fireTargetStarted etc.
191
System.out.println(""); System.out.println(targetname + ":");
192                 try {
193                     nexttargit.execute();
194                 } catch (BuildException ex) {
195                     log("Failed to build target: " + targetname, Project.MSG_ERR);
196                     throw ex;
197                 }
198                 builttargets.addElement(targetname);
199             }
200         }
201
202         builtmodules.addAll(buildfixedmodules); // add already built fixed modules to the list
203
log("fixedmodules=" + buildfixedmodules, Project.MSG_DEBUG);
204         log("builtmodules=" + builtmodules, Project.MSG_VERBOSE);
205     }
206     
207     /** Execute targets which can fail _without_ throwing BuildException */
208     private void modulesBuild() throws BuildException {
209         if ( ! failonerror ) {
210             // build the rest of modules
211
for (String JavaDoc module : buildmodules) {
212                 dummy = new Target ();
213                 dummyName = "nbmerge-" + getOwningTarget().getName() + "-" + module;
214                 while (targets.contains (dummyName))
215                     dummyName += "-x";
216                 dummy.setName (dummyName);
217                 dummy.addDependency (targetprefix + module);
218                 getProject().addTarget(dummy);
219                 @SuppressWarnings JavaDoc("unchecked")
220                 Vector<Target> fullList = getProject().topoSort(dummyName, targets);
221                 // Now remove earlier ones: already done.
222
@SuppressWarnings JavaDoc("unchecked")
223                 Vector<Target> doneList = getProject().topoSort(getOwningTarget().getName(), targets);
224                 List<Target> todo = new ArrayList<Target>(fullList.subList(0, fullList.indexOf(dummy)));
225                 todo.removeAll(doneList.subList(0, doneList.indexOf(getOwningTarget())));
226                 
227                 Iterator<Target> targit = todo.iterator();
228                 try {
229                     while (targit.hasNext()) {
230                         Target nexttargit = targit.next();
231                         String JavaDoc targetname = nexttargit.getName();
232                         if ( builttargets.indexOf(targetname) < 0 ) {
233                             System.out.println(); System.out.println(targetname + ":");
234                             nexttargit.execute();
235                             builttargets.addElement(targetname);
236                         }
237                         
238                     }
239                     builtmodules.addElement(module);
240                 } catch (BuildException BE) {
241                         log(BE.toString(), Project.MSG_WARN);
242                         BE.printStackTrace();
243                         failedmodules.addElement(module);
244                 }
245             }
246             log("builtmodules=" + builtmodules, Project.MSG_VERBOSE);
247             log("failedmodules=" + failedmodules, Project.MSG_VERBOSE);
248         }
249     }
250     
251     public void execute () throws BuildException {
252         if (topdirs.isEmpty ()) {
253             throw new BuildException("You must set at least one topdir attribute", getLocation());
254         }
255
256         buildfixedmodules.addAll(fixedmodules);
257         buildmodules.addAll(modules);
258         
259         if (( modules.size() > 0 ) && ( fixedmodules.size() == 0 ) && (! failonerror)) {
260             log("Unable to build without fixedmodules set", Project.MSG_WARN);
261             log("Swapping modules list with fixedmodules list", Project.MSG_WARN);
262             buildfixedmodules.addAll(modules);
263             buildmodules.removeAllElements();
264         }
265
266         if (( failonerror ) && ( modules.size() > 0 )) {
267             // failonerror is enabled => build success granularity is disabled
268
// though move all modules to fixedmodules
269
buildfixedmodules.addAll(modules);
270             buildmodules.removeAllElements();
271         }
272
273         // build of fixed modules
274
fixedModulesBuild();
275         
276         // build of the rest of modules
277
modulesBuild();
278         
279         // final data merging
280
dataMerge();
281
282         // display build success status
283
if (builtmodules.size() > 0 ) {
284             log("builtmodules=" + builtmodules);
285             log("builttargets=" + builttargets);
286             if (failedmodules.size() > 0 ) {
287                 log("SOME MODULES FAILED TO BUILD, BUT THEIR BuildException WAS CAUGHT.", Project.MSG_WARN);
288                 log("failedmodules=" + failedmodules, Project.MSG_WARN);
289             }
290             
291             if ( mergemodules.size() > 0 ) {
292                 if ( builtmodulesproperty.length() > 0 ) {
293                     Vector<String JavaDoc> setmodules = new Vector<String JavaDoc>();
294                     // add all successfuly built modules
295
setmodules.addAll(mergemodules);
296                     // remove all fixed modules (don't put fixed modules to modules list)
297
setmodules.removeAll(fixedmodules);
298                     // check if the modules list is equal to mergemodules without fixedmodules
299
if (( ! modules.containsAll(setmodules)) || ( ! setmodules.containsAll(modules))) {
300                         String JavaDoc bm = setmodules.toString();
301                         bm = bm.substring( 1, bm.length() - 1);
302                         if (bm.length() > 0 ) {
303                             log("Setting property \"" + builtmodulesproperty + "\" to new value " + bm); //, Project.MSG_VERBOSE);
304
getProject().setUserProperty(builtmodulesproperty, bm);
305                         }
306                     }
307                 }
308             }
309             
310         } else {
311             throw new BuildException("No modules were built", getLocation());
312         }
313         
314     }
315
316     /** Do final data merge */
317     private void dataMerge() throws BuildException {
318         List<String JavaDoc> suppressedlocales = new LinkedList<String JavaDoc> ();
319         Iterator it = suppress.iterator ();
320         while (it.hasNext ()) {
321             Suppress s = (Suppress) it.next ();
322             if (s.iftest != null && getProject().getProperty(s.iftest) == null) {
323                 continue;
324             } else if (s.unlesstest != null && getProject().getProperty(s.unlesstest) != null) {
325                 continue;
326             }
327             log ("Suppressing locale: " + s.locale);
328             suppressedlocales.add (s.locale);
329         }
330
331         UpdateTracking tr = new UpdateTracking( dest.getAbsolutePath() );
332         log ( dest.getAbsolutePath() );
333         while (it.hasNext ()) {
334           String JavaDoc locale = (String JavaDoc) it.next ();
335           tr.removeLocalized(locale);
336         }
337     }
338 }
339
Popular Tags