KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > Evaluator


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.modules.apisupport.project;
21
22 import org.netbeans.modules.apisupport.project.spi.NbModuleProvider;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.beans.PropertyChangeSupport JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.net.URI JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Arrays JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.HashSet JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.Set JavaDoc;
40 import java.util.TreeMap JavaDoc;
41 import java.util.regex.Matcher JavaDoc;
42 import java.util.regex.Pattern JavaDoc;
43 import javax.swing.event.ChangeEvent JavaDoc;
44 import javax.swing.event.ChangeListener JavaDoc;
45 import org.netbeans.api.java.classpath.ClassPath;
46 import org.netbeans.api.java.platform.JavaPlatform;
47 import org.netbeans.api.java.platform.JavaPlatformManager;
48 import org.netbeans.api.project.ProjectManager;
49 import org.netbeans.modules.apisupport.project.universe.TestModuleDependency;
50 import org.netbeans.modules.apisupport.project.universe.ModuleEntry;
51 import org.netbeans.modules.apisupport.project.universe.ModuleList;
52 import org.netbeans.spi.project.support.ant.AntProjectEvent;
53 import org.netbeans.spi.project.support.ant.AntProjectHelper;
54 import org.netbeans.spi.project.support.ant.AntProjectListener;
55 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
56 import org.netbeans.spi.project.support.ant.PropertyProvider;
57 import org.netbeans.spi.project.support.ant.PropertyUtils;
58 import org.openide.ErrorManager;
59 import org.openide.filesystems.FileObject;
60 import org.openide.filesystems.FileUtil;
61 import org.openide.modules.InstalledFileLocator;
62 import org.openide.util.Mutex;
63 import org.openide.util.RequestProcessor;
64 import org.openide.util.WeakListeners;
65 import org.w3c.dom.Element JavaDoc;
66
67 /**
68  * Property evaluator for {@link NbModuleProject}.
69  * Has two special behaviors of note:
70  * 1. Does not call ModuleList until it really needs to.
71  * 2. Is reset upon project.xml changes.
72  * @author Jesse Glick, Martin Krauskopf
73  */

74 final class Evaluator implements PropertyEvaluator, PropertyChangeListener JavaDoc, AntProjectListener {
75     
76     private final NbModuleProject project;
77     private final NbModuleProvider typeProvider;
78     private final PropertyChangeSupport JavaDoc pcs = new PropertyChangeSupport JavaDoc(this);
79     
80     private PropertyEvaluator delegate;
81     private boolean loadedModuleList = false;
82     
83     /** See issue #69440 for more details. */
84     private boolean runInAtomicAction;
85     
86     private static class TestClasspath {
87         
88         private final String JavaDoc compile;
89         private final String JavaDoc runtime;
90         private final String JavaDoc testCompile;
91         private final String JavaDoc testRuntime;
92         
93         public TestClasspath(String JavaDoc compile,String JavaDoc runtime,String JavaDoc testCompile,String JavaDoc testRuntime) {
94             this.compile = compile;
95             this.runtime = runtime;
96             this.testCompile = testCompile;
97             this.testRuntime = testRuntime;
98         }
99         
100         public String JavaDoc getCompileClasspath() {
101             return compile + ':' + testCompile;
102         }
103         
104         public String JavaDoc getRuntimeClasspath() {
105             return runtime + ':' + testRuntime;
106         }
107         
108         private static TestClasspath getOrEmpty(Map JavaDoc testsCPs, String JavaDoc testtype) {
109             TestClasspath tcp = (TestClasspath) testsCPs.get(testtype);
110             if (tcp == null ) {
111                 // create with empty classpaths
112
tcp = new TestClasspath("", "", "", ""); // NOI18N
113
}
114             return tcp;
115         }
116     }
117     
118     public Evaluator(NbModuleProject project, NbModuleProvider typeProvider) {
119         this.project = project;
120         this.typeProvider = typeProvider;
121         delegate = createEvaluator(null);
122         delegate.addPropertyChangeListener(this);
123         project.getHelper().addAntProjectListener(this);
124     }
125     
126     public String JavaDoc getProperty(String JavaDoc prop) {
127         PropertyEvaluator eval = delegatingEvaluator(false);
128         assert eval != this;
129         String JavaDoc v = eval.getProperty(prop);
130         if ((v == null && isModuleListDependentProperty(prop)) || isModuleListDependentValue(v)) {
131             return delegatingEvaluator(true).getProperty(prop);
132         } else {
133             return v;
134         }
135     }
136     
137     public String JavaDoc evaluate(String JavaDoc text) {
138         String JavaDoc v = delegatingEvaluator(false).evaluate(text);
139         if (isModuleListDependentValue(v)) {
140             return delegatingEvaluator(true).evaluate(text);
141         } else {
142             return v;
143         }
144     }
145     
146     public Map JavaDoc getProperties() {
147         return delegatingEvaluator(true).getProperties();
148     }
149     
150     private boolean isModuleListDependentProperty(String JavaDoc p) {
151         return p.equals("module.classpath") || // NOI18N
152
p.equals("cp") || p.endsWith(".cp") || p.endsWith(".cp.extra") || // NOI18N
153
p.equals("cluster") || // NOI18N
154
// MODULENAME.dir, but not module.jar.dir or the like:
155
(p.endsWith(".dir") && p.lastIndexOf('.', p.length() - 5) == -1); // NOI18N
156
}
157     
158     private static final Pattern JavaDoc ANT_PROP_REGEX = Pattern.compile("\\$\\{([a-zA-Z0-9._-]+)\\}"); // NOI18N
159
private boolean isModuleListDependentValue(String JavaDoc v) {
160         if (v == null) {
161             return false;
162         }
163         Matcher JavaDoc m = ANT_PROP_REGEX.matcher(v);
164         while (m.find()) {
165             if (isModuleListDependentProperty(m.group(1))) {
166                 return true;
167             }
168         }
169         return false;
170     }
171     
172     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
173         pcs.addPropertyChangeListener(listener);
174     }
175     
176     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
177         pcs.removePropertyChangeListener(listener);
178     }
179     
180     private PropertyEvaluator delegatingEvaluator(boolean reset) {
181         if (reset && !loadedModuleList) {
182             reset();
183             if (Util.err.isLoggable(ErrorManager.INFORMATIONAL)) {
184                 Util.err.log("Needed to reset evaluator in " + project + "due to use of module-list-dependent property; now cp=" + delegate.getProperty("cp"));
185             }
186         }
187         return delegate;
188     }
189     
190     private void reset() {
191         loadedModuleList = true;
192         delegate.removePropertyChangeListener(this);
193         try {
194             delegate = createEvaluator(project.getModuleList());
195         } catch (IOException JavaDoc e) {
196             Util.err.notify(ErrorManager.INFORMATIONAL, e);
197             // but leave old evaluator in place for now
198
}
199         delegate.addPropertyChangeListener(this);
200         pcs.firePropertyChange(null, null, null);
201     }
202     
203     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
204         if ("netbeans.dest.dir".equals(evt.getPropertyName()) || evt.getPropertyName() == null) {
205             // Module list may have changed.
206
reset();
207         } else {
208             Util.err.log("Refiring property change from delegate in " + evt.getPropertyName() + " for " + project);
209             pcs.firePropertyChange(evt.getPropertyName(), evt.getOldValue(), evt.getNewValue());
210         }
211     }
212     
213     public void configurationXmlChanged(AntProjectEvent ev) {
214         if (!runInAtomicAction && ev.getPath().equals(AntProjectHelper.PROJECT_XML_PATH)) {
215             reset();
216         }
217     }
218     
219     public void propertiesChanged(AntProjectEvent ev) {
220         /* TODO: Not needed now? Put here at least some comment. */
221     }
222     
223     /** See issue #69440 for more details. */
224     public void setRunInAtomicAction(boolean runInAtomicAction) {
225         assert ProjectManager.mutex().isWriteAccess();
226         this.runInAtomicAction = runInAtomicAction;
227     }
228     
229     public void removeListeners() {
230         project.getHelper().removeAntProjectListener(this);
231         delegate.removePropertyChangeListener(this);
232     }
233     
234     /**
235      * Create a property evaluator: private project props, shared project props, various defaults.
236      * Synch with nbbuild/templates/projectized.xml.
237      * @param ml this module list, or may be left null to skip all properties which require knowledge of other modules
238      */

239     private PropertyEvaluator createEvaluator(ModuleList ml) {
240         // XXX a lot of this duplicates ModuleList.parseProperties... can they be shared?
241
PropertyProvider predefs = project.getHelper().getStockPropertyPreprovider();
242         Map JavaDoc<String JavaDoc,String JavaDoc> stock = new HashMap JavaDoc();
243         File JavaDoc dir = project.getProjectDirectoryFile();
244         NbModuleProvider.NbModuleType type = typeProvider.getModuleType();
245         File JavaDoc nbroot;
246         if (type == NbModuleProvider.NETBEANS_ORG) {
247             nbroot = ModuleList.findNetBeansOrg(dir);
248             assert nbroot != null : "netbeans.org-type module not in a complete netbeans.org source root " + dir;
249             stock.put("nb_all", nbroot.getAbsolutePath()); // NOI18N
250
// Only needed for netbeans.org modules, since for external modules suite.properties suffices.
251
stock.put("netbeans.dest.dir", new File JavaDoc(nbroot, ModuleList.DEST_DIR_IN_NETBEANS_ORG).getAbsolutePath()); // NOI18N
252
} else {
253             nbroot = null;
254         }
255         if (ml != null) {
256             // Register *.dir for nb.org modules. There is no equivalent for external modules.
257
Iterator JavaDoc it = ml.getAllEntriesSoft().iterator();
258             while (it.hasNext()) {
259                 ModuleEntry e = (ModuleEntry) it.next();
260                 String JavaDoc nborgPath = e.getNetBeansOrgPath();
261                 if (nborgPath != null) {
262                     // #48449: intern these; number is (size of modules.xml) * (# of loaded module projects)
263
stock.put((nborgPath + ".dir").intern(), e.getClusterDirectory().getAbsolutePath().intern()); // NOI18N
264
}
265             }
266             ModuleEntry thisEntry = ml.getEntry(project.getCodeNameBase());
267             if (thisEntry != null) { // can be null e.g. for a broken suite component module
268
assert nbroot == null ^ thisEntry.getNetBeansOrgPath() != null : thisEntry;
269                 File JavaDoc clusterDir = thisEntry.getClusterDirectory();
270                 stock.put("cluster", clusterDir.getAbsolutePath()); // NOI18N
271
}
272         }
273         List JavaDoc<PropertyProvider> providers = new ArrayList JavaDoc();
274         providers.add(PropertyUtils.fixedPropertyProvider(stock));
275         // XXX should listen to changes in values of properties which refer to property files:
276
if (type == NbModuleProvider.SUITE_COMPONENT) {
277             providers.add(project.getHelper().getPropertyProvider("nbproject/private/suite-private.properties")); // NOI18N
278
providers.add(project.getHelper().getPropertyProvider("nbproject/suite.properties")); // NOI18N
279
PropertyEvaluator baseEval = PropertyUtils.sequentialPropertyEvaluator(predefs, (PropertyProvider[]) providers.toArray(new PropertyProvider[providers.size()]));
280             String JavaDoc suiteDirS = baseEval.getProperty("suite.dir"); // NOI18N
281
if (suiteDirS != null) {
282                 File JavaDoc suiteDir = PropertyUtils.resolveFile(dir, suiteDirS);
283                 providers.add(PropertyUtils.propertiesFilePropertyProvider(new File JavaDoc(suiteDir, "nbproject" + File.separatorChar + "private" + File.separatorChar + "platform-private.properties"))); // NOI18N
284
providers.add(PropertyUtils.propertiesFilePropertyProvider(new File JavaDoc(suiteDir, "nbproject" + File.separatorChar + "platform.properties"))); // NOI18N
285
}
286         } else if (type == NbModuleProvider.STANDALONE) {
287             providers.add(project.getHelper().getPropertyProvider("nbproject/private/platform-private.properties")); // NOI18N
288
providers.add(project.getHelper().getPropertyProvider("nbproject/platform.properties")); // NOI18N
289
}
290         if (type == NbModuleProvider.SUITE_COMPONENT || type == NbModuleProvider.STANDALONE) {
291             PropertyEvaluator baseEval = PropertyUtils.sequentialPropertyEvaluator(predefs, (PropertyProvider[]) providers.toArray(new PropertyProvider[providers.size()]));
292             providers.add(new Util.UserPropertiesFileProvider(baseEval, dir));
293             baseEval = PropertyUtils.sequentialPropertyEvaluator(predefs, (PropertyProvider[]) providers.toArray(new PropertyProvider[providers.size()]));
294             class DestDirProvider extends Util.ComputedPropertyProvider {
295                 public DestDirProvider(PropertyEvaluator eval) {
296                     super(eval);
297                 }
298                 protected Map JavaDoc<String JavaDoc,String JavaDoc> getProperties(Map JavaDoc<String JavaDoc,String JavaDoc> inputPropertyValues) {
299                     String JavaDoc platformS = (String JavaDoc) inputPropertyValues.get("nbplatform.active"); // NOI18N
300
if (platformS != null) {
301                         return Collections.singletonMap("netbeans.dest.dir", "${nbplatform." + platformS + ".netbeans.dest.dir}"); // NOI18N
302
} else {
303                         return Collections.EMPTY_MAP;
304                     }
305                 }
306                 protected Set JavaDoc inputProperties() {
307                     return Collections.singleton("nbplatform.active"); // NOI18N
308
}
309             }
310             providers.add(new DestDirProvider(baseEval));
311         }
312         if (type == NbModuleProvider.NETBEANS_ORG) {
313             // For local definitions of nbjdk.* properties:
314
File JavaDoc nbbuild = new File JavaDoc(nbroot, "nbbuild"); // NOI18N
315
providers.add(PropertyUtils.propertiesFilePropertyProvider(new File JavaDoc(nbbuild, "user.build.properties"))); // NOI18N
316
providers.add(PropertyUtils.propertiesFilePropertyProvider(new File JavaDoc(nbbuild, "site.build.properties"))); // NOI18N
317
}
318         PropertyEvaluator baseEval = PropertyUtils.sequentialPropertyEvaluator(predefs, (PropertyProvider[]) providers.toArray(new PropertyProvider[providers.size()]));
319         providers.add(new NbJdkProvider(baseEval));
320         providers.add(project.getHelper().getPropertyProvider(AntProjectHelper.PRIVATE_PROPERTIES_PATH));
321         providers.add(project.getHelper().getPropertyProvider(AntProjectHelper.PROJECT_PROPERTIES_PATH));
322         Map JavaDoc<String JavaDoc,String JavaDoc> defaults = new HashMap JavaDoc();
323         defaults.put("code.name.base.dashes", project.getCodeNameBase().replace('.', '-')); // NOI18N
324
defaults.put("module.jar.dir", "modules"); // NOI18N
325
defaults.put("module.jar.basename", "${code.name.base.dashes}.jar"); // NOI18N
326
defaults.put("module.jar", "${module.jar.dir}/${module.jar.basename}"); // NOI18N
327
defaults.put("manifest.mf", "manifest.mf"); // NOI18N
328
defaults.put("src.dir", "src"); // NOI18N
329
defaults.put("build.classes.dir", "build/classes"); // NOI18N
330
defaults.put("test.unit.src.dir", "test/unit/src"); // NOI18N
331
defaults.put("test.qa-functional.src.dir", "test/qa-functional/src"); // NOI18N
332
defaults.put("test.qa-performance.src.dir", "test/qa-performance/src"); // NOI18N
333
defaults.put("build.test.unit.classes.dir", "build/test/unit/classes"); // NOI18N
334
defaults.put("javac.source", "1.4"); // NOI18N
335
defaults.put("test.user.dir", new File JavaDoc(dir, "build/testuserdir").getAbsolutePath()); // NOI18N
336
providers.add(PropertyUtils.fixedPropertyProvider(defaults));
337         if (ml != null) {
338             providers.add(PropertyUtils.fixedPropertyProvider(Collections.singletonMap("module.classpath", computeModuleClasspath(ml)))); // NOI18N
339
Map JavaDoc<String JavaDoc,String JavaDoc> buildDefaults = new HashMap JavaDoc();
340             buildDefaults.put("cp.extra", ""); // NOI18N
341
buildDefaults.put("cp", "${module.classpath}:${cp.extra}"); // NOI18N
342
buildDefaults.put("run.cp", computeRuntimeModuleClasspath(ml) + ":${cp.extra}:${build.classes.dir}"); // NOI18N
343

344             baseEval = PropertyUtils.sequentialPropertyEvaluator(predefs, (PropertyProvider[]) providers.toArray(new PropertyProvider[providers.size()]));
345             buildDefaults.put("test.unit.cp.extra", ""); // NOI18N
346
String JavaDoc testJars; // #68685 - follow Ant script
347
if (type == NbModuleProvider.NETBEANS_ORG) {
348                 // Cf. nbbuild/templates/projectized.xml#test-lib-init
349
buildDefaults.put("xtest.home", "${nb_all}/xtest"); // NOI18N
350
testJars =
351                         "${xtest.home}/lib/junit.jar:" + // NOI18N
352
"${xtest.home}/lib/nbjunit.jar:" + // NOI18N
353
"${xtest.home}/lib/nbjunit-ide.jar:" + // NOI18N
354
"${xtest.home}/lib/insanelib.jar"; // NOI18N
355
} else {
356                 // Cf. apisupport/harness/release/build.xml#test-lib-init
357
testJars =
358                         "${test.unit.lib.cp}:" + // NOI18N
359
// XXX this is ugly, try to look for the JAR using wildcards instead
360
"${netbeans.dest.dir}/ide6/modules/ext/junit-3.8.1.jar:" + // NOI18N
361
"${netbeans.dest.dir}/ide8/modules/ext/junit-3.8.2.jar:" + // NOI18N
362
"${netbeans.dest.dir}/testtools/modules/ext/nbjunit.jar:" + // NOI18N
363
"${netbeans.dest.dir}/testtools/modules/ext/insanelib.jar:" + // NOI18N
364
"${netbeans.dest.dir}/testtools/modules/org-netbeans-modules-nbjunit.jar:" + // NOI18N, new for 6.0
365
"${netbeans.dest.dir}/testtools/modules/org-netbeans-modules-nbjunit-ide.jar:" + // NOI18N, new for 6.0
366
"${netbeans.home}/../ide6/modules/ext/junit-3.8.1.jar:" + // NOI18N
367
"${netbeans.home}/../ide8/modules/ext/junit-3.8.2.jar:" + // NOI18N
368
"${netbeans.home}/../testtools/modules/ext/nbjunit.jar:" + // NOI18N
369
"${netbeans.home}/../testtools/modules/ext/insanelib.jar:" + // NOI18N
370
"${netbeans.home}/../testtools/modules/org-netbeans-modules-nbjunit.jar:" + // NOI18N, new for 6.0
371
"${netbeans.home}/../testtools/modules/org-netbeans-modules-nbjunit-ide.jar:" + // NOI18N, new for 6.0
372
"${netbeans.user}/modules/ext/nbjunit.jar:" + // NOI18N
373
"${netbeans.user}/modules/ext/insanelib.jar:" + // NOI18N
374
"${netbeans.dest.dir}/../../xtest/lib/junit.jar:" + // NOI18N
375
"${netbeans.dest.dir}/../../xtest/lib/nbjunit.jar:" + // NOI18N
376
"${netbeans.dest.dir}/../../xtest/lib/insanelib.jar:" + // NOI18N
377
"${netbeans.user}/modules/org-netbeans-modules-nbjunit.jar:" + // NOI18N, new for 6.0
378
"${netbeans.user}/modules/org-netbeans-modules-nbjunit-ide.jar"; // NOI18N, new for 6.0
379
}
380             Map JavaDoc<String JavaDoc,TestClasspath> testsCPs = computeTestingClassPaths(ml,baseEval);
381             TestClasspath tcp = TestClasspath.getOrEmpty(testsCPs, "unit"); // NOI18N
382

383             buildDefaults.put("test.unit.cp", "${cp}:${cluster}/${module.jar}:" + testJars + ":${test.unit.cp.extra}:" + tcp.getCompileClasspath()); // NOI18N
384
buildDefaults.put("test.unit.run.cp.extra", ""); // NOI18N
385
buildDefaults.put("test.unit.run.cp", "${test.unit.cp}:${build.test.unit.classes.dir}:${test.unit.run.cp.extra}:"+ tcp.getRuntimeClasspath()); // NOI18N
386
// #61085: need to treat qa-functional tests the same way...
387
buildDefaults.put("test.qa-functional.cp.extra", ""); // NOI18N
388
// No idea how XTest finds these, some weird magic, so no Ant script to match up to:
389
String JavaDoc jemmyJar = findJemmyJar(baseEval);
390             if (jemmyJar != null) {
391                 buildDefaults.put("jemmy.jar", jemmyJar); // NOI18N
392
}
393             String JavaDoc jelly2NbJar = findJelly2NbJar(baseEval);
394             if (jelly2NbJar != null) {
395                 buildDefaults.put("jelly2-nb.jar", jelly2NbJar); // NOI18N
396
}
397             tcp = TestClasspath.getOrEmpty(testsCPs, "qa-functional"); // NOI18N
398
buildDefaults.put("test.qa-functional.cp", testJars + // NOI18N
399
":${netbeans.home}/../testtools/modules/ext/nbjunit-ide.jar" + // NOI18N
400
":${netbeans.user}/testtools/modules/ext/nbjunit.jar" + // NOI18N
401
":${jemmy.jar}" + // NOI18N
402
":${jelly2-nb.jar}" + // NOI18N
403
":${test.qa-functional.cp.extra}:" + // NOI18N
404
tcp.compile + ':' + tcp.testCompile);
405             buildDefaults.put("build.test.qa-functional.classes.dir", "build/test/qa-functional/classes"); // NOI18N
406
buildDefaults.put("test.qa-functional.run.cp", "${test.qa-functional.cp}:${build.test.qa-functional.classes.dir}" + ':' + tcp.runtime + ':' + tcp.testRuntime); // NOI18N
407
providers.add(PropertyUtils.fixedPropertyProvider(buildDefaults));
408         }
409         // skip a bunch of properties irrelevant here - NBM stuff, etc.
410
return PropertyUtils.sequentialPropertyEvaluator(predefs, (PropertyProvider[]) providers.toArray(new PropertyProvider[providers.size()]));
411     }
412     
413     private final class NbJdkProvider implements PropertyProvider, PropertyChangeListener JavaDoc { // #63541: JDK selection
414

415         private final PropertyEvaluator eval;
416         private final List JavaDoc<ChangeListener JavaDoc> listeners = new ArrayList JavaDoc();
417         private final PropertyChangeListener JavaDoc weakListener = WeakListeners.propertyChange(this, null);
418         
419         public NbJdkProvider(PropertyEvaluator eval) {
420             this.eval = eval;
421             eval.addPropertyChangeListener(weakListener);
422             JavaPlatformManager.getDefault().addPropertyChangeListener(weakListener);
423         }
424         
425         public final Map JavaDoc<String JavaDoc,String JavaDoc> getProperties() {
426             Map JavaDoc<String JavaDoc,String JavaDoc> props = new HashMap JavaDoc();
427             String JavaDoc home = eval.getProperty("nbjdk.home"); // NOI18N
428
if (home == null) {
429                 String JavaDoc active = eval.getProperty("nbjdk.active"); // NOI18N
430
if (active != null && !active.equals("default")) { // NOI18N
431
home = eval.getProperty("platforms." + active + ".home"); // NOI18N
432
if (home != null) {
433                         props.put("nbjdk.home", home); // NOI18N
434
}
435                 }
436             }
437             if (home == null) {
438                 JavaPlatform platform = JavaPlatformManager.getDefault().getDefaultPlatform();
439                 if (platform != null) {
440                     Collection JavaDoc<FileObject> installs = platform.getInstallFolders();
441                     if (installs.size() == 1) {
442                         home = FileUtil.toFile((FileObject) installs.iterator().next()).getAbsolutePath();
443                     }
444                 }
445             }
446             String JavaDoc bootcp = null;
447             if (home != null) {
448                 FileObject homeFO = FileUtil.toFileObject(new File JavaDoc(home));
449                 if (homeFO != null) {
450                     JavaPlatform[] platforms = JavaPlatformManager.getDefault().getInstalledPlatforms();
451                     for (int i = 0; i < platforms.length; i++) {
452                         if (new HashSet JavaDoc(platforms[i].getInstallFolders()).equals(Collections.singleton(homeFO))) {
453                             // Matching JDK is registered, so look up its real bootcp.
454
StringBuffer JavaDoc bootcpSB = new StringBuffer JavaDoc();
455                             ClassPath boot = platforms[i].getBootstrapLibraries();
456                             boot.removePropertyChangeListener(weakListener);
457                             boot.addPropertyChangeListener(weakListener);
458                             Iterator JavaDoc<ClassPath.Entry> entries = boot.entries().iterator();
459                             while (entries.hasNext()) {
460                                 ClassPath.Entry entry = (ClassPath.Entry) entries.next();
461                                 URL JavaDoc u = entry.getURL();
462                                 if (u.toExternalForm().endsWith("!/")) { // NOI18N
463
URL JavaDoc nested = FileUtil.getArchiveFile(u);
464                                     if (nested != null) {
465                                         u = nested;
466                                     }
467                                 }
468                                 if ("file".equals(u.getProtocol())) { // NOI18N
469
File JavaDoc f = new File JavaDoc(URI.create(u.toExternalForm()));
470                                     if (bootcpSB.length() > 0) {
471                                         bootcpSB.append(File.pathSeparatorChar);
472                                     }
473                                     bootcpSB.append(f.getAbsolutePath());
474                                 }
475                             }
476                             bootcp = bootcpSB.toString();
477                             break;
478                         }
479                     }
480                 }
481                 if (bootcp == null) {
482                     bootcp = "${nbjdk.home}/jre/lib/rt.jar".replace('/', File.separatorChar); // NOI18N
483
}
484             }
485             if (bootcp == null) {
486                 // Real fallback...
487
bootcp = "${sun.boot.class.path}"; // NOI18N
488
}
489             props.put("nbjdk.bootclasspath", bootcp); // NOI18N
490
if (home != null) {
491                 props.put("tools.jar", home + "/lib/tools.jar".replace('/', File.separatorChar)); // NOI18N
492
}
493             if (Util.err.isLoggable(ErrorManager.INFORMATIONAL)) {
494                 Map JavaDoc<String JavaDoc,String JavaDoc> _props = new TreeMap JavaDoc(eval.getProperties());
495                 Iterator JavaDoc it = _props.entrySet().iterator();
496                 while (it.hasNext()) {
497                     String JavaDoc k = (String JavaDoc) ((Map.Entry JavaDoc) it.next()).getKey();
498                     if (!k.startsWith("nbjdk.") && !k.startsWith("platforms.")) { // NOI18N
499
it.remove();
500                     }
501                 }
502                 _props.putAll(props);
503                 Util.err.log("JDK-related properties of " + project + ": " + _props);
504             }
505             return props;
506         }
507         
508         public final void addChangeListener(ChangeListener JavaDoc l) {
509             synchronized (listeners) {
510                 listeners.add(l);
511             }
512         }
513         
514         public final void removeChangeListener(ChangeListener JavaDoc l) {
515             synchronized (listeners) {
516                 listeners.remove(l);
517             }
518         }
519         
520         public final void propertyChange(PropertyChangeEvent JavaDoc evt) {
521             String JavaDoc p = evt.getPropertyName();
522             if (p != null && !p.startsWith("nbjdk.") && !p.startsWith("platforms.") && // NOI18N
523
!p.equals(ClassPath.PROP_ENTRIES) && !p.equals(JavaPlatformManager.PROP_INSTALLED_PLATFORMS)) {
524                 return;
525             }
526             final ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
527             final Iterator JavaDoc it;
528             synchronized (listeners) {
529                 if (listeners.isEmpty()) {
530                     return;
531                 }
532                 it = new HashSet JavaDoc(listeners).iterator();
533             }
534             final Mutex.Action action = new Mutex.Action() {
535                 public Object JavaDoc run() {
536                     while (it.hasNext()) {
537                         ((ChangeListener JavaDoc) it.next()).stateChanged(ev);
538                     }
539                     return null;
540                 }
541             };
542             // See ProjectProperties.PP.fireChange for explanation of this threading stuff:
543
if (ProjectManager.mutex().isWriteAccess()) {
544                 ProjectManager.mutex().readAccess(action);
545             } else if (ProjectManager.mutex().isReadAccess()) {
546                 action.run();
547             } else {
548                 RequestProcessor.getDefault().post(new Runnable JavaDoc() {
549                     public void run() {
550                         ProjectManager.mutex().readAccess(action);
551                     }
552                 });
553             }
554         }
555         
556     }
557     
558     /**
559      * Get an Ant location for the root of jemmy.jar.
560      */

561     private String JavaDoc findJemmyJar(PropertyEvaluator eval) {
562         File JavaDoc f = project.getNbrootFile("jemmy/builds/jemmy.jar", eval); // NOI18N
563
if(f == null) {
564             // try to find jemmy.jar installed by Jemmy module
565
f = InstalledFileLocator.getDefault().locate(
566                     "modules/ext/jemmy.jar", "org.netbeans.modules.jemmy", false); // NOI18N
567
}
568         if (f != null) {
569             return f.getAbsolutePath();
570         } else {
571             return null;
572         }
573     }
574     
575     /**
576      * Get an Ant location for the root of jemmy.jar.
577      */

578     private String JavaDoc findJelly2NbJar(PropertyEvaluator eval) {
579         File JavaDoc f = project.getNbrootFile("jellytools/builds/jelly2-nb.jar", eval); // NOI18N
580
if(f == null) {
581             // try to find jelly2-nb.jar installed by Jellytools module
582
f = InstalledFileLocator.getDefault().locate(
583                     "modules/ext/jelly2-nb.jar", "org.netbeans.modules.jellytools", false); // NOI18N
584
}
585         if (f != null) {
586             return f.getAbsolutePath();
587         } else {
588             return null;
589         }
590     }
591     
592     /**
593      * Should be similar to impl in ParseProjectXml.
594      */

595     private String JavaDoc computeModuleClasspath(ModuleList ml) {
596         Element JavaDoc data = project.getPrimaryConfigurationData();
597         Element JavaDoc moduleDependencies = Util.findElement(data,
598             "module-dependencies", NbModuleProjectType.NAMESPACE_SHARED); // NOI18N
599
List JavaDoc<Element JavaDoc> deps = Util.findSubElements(moduleDependencies);
600         Iterator JavaDoc it = deps.iterator();
601         StringBuffer JavaDoc cp = new StringBuffer JavaDoc();
602         while (it.hasNext()) {
603             Element JavaDoc dep = (Element JavaDoc)it.next();
604             if (Util.findElement(dep, "compile-dependency", // NOI18N
605
NbModuleProjectType.NAMESPACE_SHARED) == null) {
606                 continue;
607             }
608             Element JavaDoc cnbEl = Util.findElement(dep, "code-name-base", // NOI18N
609
NbModuleProjectType.NAMESPACE_SHARED);
610             String JavaDoc cnb = Util.findText(cnbEl);
611             ModuleEntry module = ml.getEntry(cnb);
612             if (module == null) {
613                 Util.err.log(ErrorManager.WARNING, "Warning - could not find dependent module " + cnb + " for " + FileUtil.getFileDisplayName(project.getProjectDirectory()));
614                 continue;
615             }
616             File JavaDoc moduleJar = module.getJarLocation();
617             if (cp.length() > 0) {
618                 cp.append(File.pathSeparatorChar);
619             }
620             cp.append(moduleJar.getAbsolutePath());
621             cp.append(module.getClassPathExtensions());
622         }
623         ModuleEntry myself = ml.getEntry(project.getCodeNameBase());
624         if (myself == null) {
625             // ???
626
return "";
627         }
628         cp.append(myself.getClassPathExtensions());
629         return cp.toString();
630     }
631     
632     /**
633      * Follows transitive runtime dependencies.
634      * @see "issue #70206"
635      */

636     private String JavaDoc computeRuntimeModuleClasspath(ModuleList ml) {
637         Set JavaDoc<String JavaDoc> unprocessed = new HashSet JavaDoc();
638         unprocessed.add(project.getCodeNameBase());
639         Set JavaDoc<String JavaDoc> processed = new HashSet JavaDoc();
640         StringBuffer JavaDoc cp = new StringBuffer JavaDoc();
641         while (!unprocessed.isEmpty()) { // crude breadth-first search
642
Iterator JavaDoc it = unprocessed.iterator();
643             String JavaDoc cnb = (String JavaDoc) it.next();
644             it.remove();
645             if (processed.add(cnb)) {
646                 ModuleEntry module = ml.getEntry(cnb);
647                 if (module == null) {
648                     Util.err.log(ErrorManager.WARNING, "Warning - could not find dependent module " + cnb + " for " + FileUtil.getFileDisplayName(project.getProjectDirectory()));
649                     continue;
650                 }
651                 if (!cnb.equals(project.getCodeNameBase())) { // build/classes for this is special
652
if (cp.length() > 0) {
653                         cp.append(File.pathSeparatorChar);
654                     }
655                     cp.append(module.getJarLocation().getAbsolutePath());
656                     cp.append(module.getClassPathExtensions());
657                 }
658                 String JavaDoc[] newDeps = module.getRunDependencies();
659                 unprocessed.addAll(Arrays.asList(newDeps));
660             }
661         }
662         ModuleEntry myself = ml.getEntry(project.getCodeNameBase());
663         if (myself != null) {
664             // #76341: must include <class-path-extension>s in ${run.cp} too.
665
cp.append(myself.getClassPathExtensions());
666         }
667         return cp.toString();
668     }
669
670     /**
671      * Gives a map from test type (e.g. <em>unit</em> or <em>qa-functional</em>)
672      * to the {@link TestClasspath test classpath} according to the content in
673      * the project's metadata (<em>project.xml<em>).
674      */

675     private Map JavaDoc computeTestingClassPaths(ModuleList ml, PropertyEvaluator evaluator) {
676         Map JavaDoc<String JavaDoc, TestClasspath> classpaths = new HashMap JavaDoc();
677         ProjectXMLManager pxm = new ProjectXMLManager(project);
678         Map JavaDoc<String JavaDoc, Set JavaDoc<TestModuleDependency>> testTypes = pxm.getTestDependencies(ml);
679         
680         String JavaDoc testDistDir = evaluator.getProperty("test.dist.dir"); // NOI18N
681
if (testDistDir == null) {
682             NbModuleProvider.NbModuleType type = typeProvider.getModuleType();
683             if (type == NbModuleProvider.NETBEANS_ORG) {
684                 // test.dist.dir = ${nb_all}/nbbuild/build/testdist
685
String JavaDoc nball = evaluator.getProperty("nb_all"); // NOI18N
686
testDistDir = nball + File.separatorChar + "nbbuild" + File.separatorChar + "build" + File.separatorChar + "testdist"; // NOI18N
687
} else if ( type == NbModuleProvider.SUITE_COMPONENT) {
688                 // test.dist.dir = ${suite.dir}/build/testdist
689
String JavaDoc suiteDir = evaluator.getProperty("suite.dir"); // NOI18N
690
testDistDir = suiteDir + File.separatorChar + "build" + File.separatorChar + "testdist"; // NOI18N
691
} else {
692                 // standalone module
693
// test.dist.dir = ${module.dir}/build/testdist
694
String JavaDoc moduleDir = evaluator.getProperty("module.dir"); // NOI18N
695
testDistDir = moduleDir + File.separatorChar + "build" + File.separatorChar + "testdist"; // NOI18N
696
}
697         }
698         for (Iterator JavaDoc ttIt = testTypes.entrySet().iterator(); ttIt.hasNext(); ) {
699             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) ttIt.next();
700             String JavaDoc ttName = (String JavaDoc) entry.getKey();
701             Set JavaDoc ttModules = (Set JavaDoc) entry.getValue();
702             computeTestType(ttName,new File JavaDoc(testDistDir),ttModules,classpaths,ml);
703         }
704         return classpaths;
705     }
706     
707     private void computeTestType(String JavaDoc ttName,File JavaDoc testDistDir, Set JavaDoc ttModules, Map JavaDoc<String JavaDoc,TestClasspath> classpaths,ModuleList ml) {
708   
709         Set JavaDoc compileCnds = new HashSet JavaDoc();
710         Set JavaDoc runtimeCnds = new HashSet JavaDoc();
711         Set JavaDoc testCompileCnds = new HashSet JavaDoc();
712         Set JavaDoc testRuntimeCnds = new HashSet JavaDoc();
713         
714         Set JavaDoc processedRecursive = new HashSet JavaDoc();
715         for (Iterator JavaDoc it = ttModules.iterator() ; it.hasNext();) {
716             TestModuleDependency td = (TestModuleDependency) it.next();
717             String JavaDoc cnd = td.getModule().getCodeNameBase();
718             if (td.isTest()) {
719                 if (td.isCompile()) {
720                     testCompileCnds.add(cnd);
721                 }
722                 testRuntimeCnds.add(cnd);
723             }
724             if (td.isRecursive()) {
725                 // scan cp recursively
726
processTestEntryRecursive(td,compileCnds,runtimeCnds,processedRecursive,ml);
727             } else {
728                 runtimeCnds.add(cnd);
729                 if (td.isCompile()) {
730                     compileCnds.add(cnd);
731                 }
732             }
733         }
734         TestClasspath testClasspath = new TestClasspath(mergePaths(compileCnds,false,ttName,testDistDir, ml),
735                 mergePaths(runtimeCnds,false,ttName,testDistDir,ml),
736                 mergePaths(testCompileCnds,true,ttName,testDistDir,ml),
737                 mergePaths(testRuntimeCnds,true,ttName,testDistDir,ml));
738
739         classpaths.put(ttName,testClasspath);
740     }
741   
742   private void processTestEntryRecursive(TestModuleDependency td,
743                                         Set JavaDoc compileCnds,
744                                         Set JavaDoc runtimeCnds,
745                                         Set JavaDoc processedRecursive,
746                                         ModuleList ml) {
747         Set JavaDoc<String JavaDoc> unprocessed = new HashSet JavaDoc();
748         
749         unprocessed.add(td.getModule().getCodeNameBase());
750         while (!unprocessed.isEmpty()) { // crude breadth-first search
751
Iterator JavaDoc it = unprocessed.iterator();
752             String JavaDoc cnb = (String JavaDoc) it.next();
753             it.remove();
754             if (processedRecursive.add(cnb)) {
755                 ModuleEntry module = ml.getEntry(cnb);
756                 if (module == null) {
757                     Util.err.log(ErrorManager.WARNING, "Warning - could not find dependent module " + cnb + " for " + FileUtil.getFileDisplayName(project.getProjectDirectory()));
758                     continue;
759                 }
760                 if (!cnb.equals(project.getCodeNameBase())) { // build/classes for this is special
761
runtimeCnds.add(cnb);
762                     if (td.isCompile()) {
763                         compileCnds.add(cnb);
764                     }
765                 }
766                 String JavaDoc[] newDeps = module.getRunDependencies();
767                 unprocessed.addAll(Arrays.asList(newDeps));
768             }
769         }
770     }
771
772     private String JavaDoc mergePaths(Set JavaDoc cnbs, boolean test,String JavaDoc testtype,File JavaDoc testDistDir,ModuleList ml) {
773         StringBuffer JavaDoc cps = new StringBuffer JavaDoc();
774         for (Iterator JavaDoc it = cnbs.iterator() ; it.hasNext();) {
775                 String JavaDoc cnb = (String JavaDoc)it.next();
776                 ModuleEntry module = (ModuleEntry)ml.getEntry(cnb);
777                 if (cps.length() > 0) {
778                     cps.append(':');
779                 }
780                 if (test) {
781                     // we need to get cluster name
782
File JavaDoc clusterDir = module.getClusterDirectory();
783                     if (clusterDir != null) {
784                         String JavaDoc clusterName = clusterDir.getName();
785                         char s = File.separatorChar;
786                         File JavaDoc jarFile = new File JavaDoc(
787                                           testDistDir, testtype + s + clusterName + s + cnb.replace('.','-') + s + "tests.jar"); // NOI18N
788
cps.append(jarFile.getPath());
789                     }
790                      
791                 } else {
792                     cps.append(module.getJarLocation().getPath());
793                 }
794         }
795         return cps.toString();
796     }
797    
798 }
799
Popular Tags