KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > queries > ClassPathProviderImpl


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.queries;
21
22 import java.io.File JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import org.netbeans.api.java.classpath.ClassPath;
30 import org.netbeans.spi.java.classpath.ClassPathFactory;
31 import org.netbeans.spi.java.classpath.ClassPathImplementation;
32 import org.netbeans.spi.java.classpath.ClassPathProvider;
33 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
34 import org.netbeans.modules.apisupport.project.NbModuleProject;
35 import org.netbeans.modules.apisupport.project.NbModuleProjectType;
36 import org.netbeans.modules.apisupport.project.Util;
37 import org.netbeans.spi.java.classpath.PathResourceImplementation;
38 import org.netbeans.spi.java.project.classpath.support.ProjectClassPathSupport;
39 import org.netbeans.spi.project.support.ant.PropertyUtils;
40 import org.openide.ErrorManager;
41 import org.openide.filesystems.FileObject;
42 import org.openide.filesystems.FileStateInvalidException;
43 import org.openide.filesystems.FileUtil;
44 import org.w3c.dom.Element JavaDoc;
45
46 public final class ClassPathProviderImpl implements ClassPathProvider {
47     
48     private final NbModuleProject project;
49     
50     public ClassPathProviderImpl(NbModuleProject project) {
51         this.project = project;
52     }
53     
54     private ClassPath boot;
55     private ClassPath source;
56     private ClassPath compile;
57     private ClassPath execute;
58     private ClassPath testSource;
59     private ClassPath testCompile;
60     private ClassPath testExecute;
61     private ClassPath funcTestSource;
62     private ClassPath funcTestCompile;
63     private ClassPath funcTestExecute;
64     private Map JavaDoc<FileObject,ClassPath> extraCompilationUnitsCompile = null;
65     private Map JavaDoc<FileObject,ClassPath> extraCompilationUnitsExecute = null;
66     
67     public ClassPath findClassPath(FileObject file, String JavaDoc type) {
68         if (type.equals(ClassPath.BOOT)) {
69             if (boot == null) {
70                 boot = ClassPathFactory.createClassPath(createPathFromProperty("nbjdk.bootclasspath")); // NOI18N
71
}
72             return boot;
73         }
74         FileObject srcDir = project.getSourceDirectory();
75         FileObject testSrcDir = project.getTestSourceDirectory();
76         FileObject funcTestSrcDir = project.getFunctionalTestSourceDirectory();
77         File JavaDoc dir = project.getClassesDirectory();
78         FileObject classesDir = dir == null ? null : FileUtil.toFileObject(FileUtil.normalizeFile(dir));
79         dir = project.getTestClassesDirectory();
80         FileObject testClassesDir = dir == null ? null : FileUtil.toFileObject(FileUtil.normalizeFile(dir));
81         File JavaDoc moduleJar = project.getModuleJarLocation();
82         if (srcDir != null && (FileUtil.isParentOf(srcDir, file) || file == srcDir)) {
83             // Regular sources.
84
if (type.equals(ClassPath.COMPILE)) {
85                 if (compile == null) {
86                     compile = ClassPathFactory.createClassPath(createCompileClasspath());
87                     Util.err.log("compile/execute-time classpath for " + project + ": " + compile);
88                 }
89                 return compile;
90             } else if (type.equals(ClassPath.EXECUTE)) {
91                 if (execute == null) {
92                     execute = ClassPathFactory.createClassPath(createExecuteClasspath());
93                 }
94                 return execute;
95             } else if (type.equals(ClassPath.SOURCE)) {
96                 if (source == null) {
97                     source = ClassPathSupport.createClassPath(new FileObject[] {srcDir});
98                 }
99                 return source;
100             }
101         } else if (testSrcDir != null && (FileUtil.isParentOf(testSrcDir, file) || file == testSrcDir)) {
102             // Unit tests.
103
if (type.equals(ClassPath.COMPILE)) {
104                 if (testCompile == null) {
105                     testCompile = ClassPathFactory.createClassPath(createTestCompileClasspath());
106                     Util.err.log("compile-time classpath for tests in " + project + ": " + testCompile);
107                 }
108                 return testCompile;
109             } else if (type.equals(ClassPath.EXECUTE)) {
110                 if (testExecute == null) {
111                     testExecute = ClassPathFactory.createClassPath(createTestExecuteClasspath());
112                     Util.err.log("runtime classpath for tests in " + project + ": " + testExecute);
113                 }
114                 return testExecute;
115             } else if (type.equals(ClassPath.SOURCE)) {
116                 if (testSource == null) {
117                     testSource = ClassPathSupport.createClassPath(new FileObject[] {testSrcDir});
118                 }
119                 return testSource;
120             }
121         } else if (funcTestSrcDir != null && (FileUtil.isParentOf(funcTestSrcDir, file) || file == funcTestSrcDir)) {
122             // Functional tests.
123
if (type.equals(ClassPath.SOURCE)) {
124                 if (funcTestSource == null) {
125                     funcTestSource = ClassPathSupport.createClassPath(new FileObject[] {funcTestSrcDir});
126                 }
127                 return funcTestSource;
128             } else if (type.equals(ClassPath.COMPILE)) {
129                 // See #42331.
130
if (funcTestCompile == null) {
131                     funcTestCompile = ClassPathFactory.createClassPath(createFuncTestCompileClasspath());
132                     Util.err.log("compile-time classpath for func tests in " + project + ": " + funcTestCompile);
133                 }
134                 return funcTestCompile;
135             } else if (type.equals(ClassPath.EXECUTE)) {
136                 if (funcTestExecute == null) {
137                     funcTestExecute = ClassPathFactory.createClassPath(createFuncTestExecuteClasspath());
138                 }
139                 return funcTestExecute;
140             }
141         } else if (classesDir != null && (classesDir.equals(file) || FileUtil.isParentOf(classesDir,file))) {
142             if (ClassPath.EXECUTE.equals(type)) {
143                 try {
144                     List JavaDoc roots = new ArrayList JavaDoc ();
145                     roots.add ( ClassPathSupport.createResource(classesDir.getURL()));
146                     roots.addAll(createCompileClasspath().getResources());
147                     return ClassPathSupport.createClassPath (roots);
148                 } catch (FileStateInvalidException e) {
149                     ErrorManager.getDefault().notify (e);
150                     return null;
151                 }
152             }
153         } else if (testClassesDir != null && (testClassesDir.equals(file) || FileUtil.isParentOf(testClassesDir,file))) {
154             if (ClassPath.EXECUTE.equals(type)) {
155                 if (testExecute == null) {
156                     testExecute = ClassPathFactory.createClassPath(createTestExecuteClasspath());
157                     Util.err.log("runtime classpath for tests in " + project + ": " + testExecute);
158                 }
159                 return testExecute;
160             }
161         } else if (FileUtil.getArchiveFile(file) != null &&
162                 FileUtil.toFile(FileUtil.getArchiveFile(file)).equals(moduleJar) &&
163                 file.equals(FileUtil.getArchiveRoot(FileUtil.getArchiveFile(file)))) {
164             if (ClassPath.EXECUTE.equals(type)) {
165                 List JavaDoc roots = new ArrayList JavaDoc ();
166                 roots.add(ClassPathSupport.createResource(Util.urlForJar(moduleJar)));
167                 roots.addAll(createCompileClasspath().getResources());
168                 return ClassPathSupport.createClassPath (roots);
169             }
170         }
171         else {
172             calculateExtraCompilationUnits();
173             Iterator JavaDoc it = extraCompilationUnitsCompile.entrySet().iterator();
174             while (it.hasNext()) {
175                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
176                 FileObject pkgroot = (FileObject) entry.getKey();
177                 if (FileUtil.isParentOf(pkgroot, file) || file == pkgroot) {
178                     if (type.equals(ClassPath.COMPILE)) {
179                         return (ClassPath) entry.getValue();
180                     } else if (type.equals(ClassPath.EXECUTE)) {
181                         return (ClassPath) extraCompilationUnitsExecute.get(pkgroot);
182                     } else if (type.equals(ClassPath.SOURCE)) {
183                         // XXX should these be cached?
184
return ClassPathSupport.createClassPath(new FileObject[] {pkgroot});
185                     } else {
186                         break;
187                     }
188                 }
189             }
190         }
191         // Something not supported.
192
return null;
193     }
194     
195     private ClassPathImplementation createPathFromProperty(String JavaDoc prop) {
196         return ProjectClassPathSupport.createPropertyBasedClassPathImplementation(
197             project.getProjectDirectoryFile(), project.evaluator(), new String JavaDoc[] {prop});
198     }
199     
200     /** &lt;compile-dependency&gt; is what we care about. */
201     private ClassPathImplementation createCompileClasspath() {
202         return createPathFromProperty("cp"); // NOI18N
203
}
204     
205     private void addPathFromProjectEvaluated(List JavaDoc<PathResourceImplementation> entries, String JavaDoc path) {
206         if (path != null) {
207             String JavaDoc[] pieces = PropertyUtils.tokenizePath(path);
208             for (int i = 0; i < pieces.length; i++) {
209                 entries.add(ClassPathSupport.createResource(Util.urlForDirOrJar(project.getHelper().resolveFile(pieces[i]))));
210             }
211         }
212     }
213     
214     private ClassPathImplementation createTestCompileClasspath() {
215         return createPathFromProperty("test.unit.cp"); // NOI18N
216
}
217     
218     private ClassPathImplementation createTestExecuteClasspath() {
219         return createPathFromProperty("test.unit.run.cp"); // NOI18N
220
}
221     
222     private ClassPathImplementation createFuncTestCompileClasspath() {
223         return createPathFromProperty("test.qa-functional.cp"); // NOI18N
224
}
225     
226     private ClassPathImplementation createFuncTestExecuteClasspath() {
227         return createPathFromProperty("test.qa-functional.run.cp"); // NOI18N
228
}
229     
230     private ClassPathImplementation createExecuteClasspath() {
231         return createPathFromProperty("run.cp"); // NOI18N
232
}
233     
234     private void calculateExtraCompilationUnits() {
235         if (extraCompilationUnitsCompile != null) {
236             return;
237         }
238         extraCompilationUnitsCompile = new HashMap JavaDoc();
239         extraCompilationUnitsExecute = new HashMap JavaDoc();
240         Iterator JavaDoc<Map.Entry JavaDoc<FileObject,Element JavaDoc>> ecus = project.getExtraCompilationUnits().entrySet().iterator();
241         while (ecus.hasNext()) {
242             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) ecus.next();
243             FileObject pkgroot = (FileObject) entry.getKey();
244             Element JavaDoc pkgrootEl = (Element JavaDoc) entry.getValue();
245             Element JavaDoc classpathEl = Util.findElement(pkgrootEl, "classpath", NbModuleProjectType.NAMESPACE_SHARED); // NOI18N
246
assert classpathEl != null : "no <classpath> in " + pkgrootEl;
247             String JavaDoc classpathS = Util.findText(classpathEl);
248             if (classpathS == null) {
249                 extraCompilationUnitsCompile.put(pkgroot, ClassPathSupport.createClassPath(new URL JavaDoc[0]));
250                 extraCompilationUnitsExecute.put(pkgroot, ClassPathSupport.createClassPath(new URL JavaDoc[0]));
251             } else {
252                 String JavaDoc classpathEval = project.evaluator().evaluate(classpathS);
253                 List JavaDoc<PathResourceImplementation> entries = new ArrayList JavaDoc();
254                 addPathFromProjectEvaluated(entries, classpathEval);
255                 extraCompilationUnitsCompile.put(pkgroot, ClassPathSupport.createClassPath(entries));
256                 // Add <built-to> dirs and JARs for ClassPath.EXECUTE.
257
entries = new ArrayList JavaDoc(entries);
258                 Iterator JavaDoc<Element JavaDoc> pkgrootKids = Util.findSubElements(pkgrootEl).iterator();
259                 while (pkgrootKids.hasNext()) {
260                     Element JavaDoc kid = (Element JavaDoc) pkgrootKids.next();
261                     if (!kid.getLocalName().equals("built-to")) { // NOI18N
262
continue;
263                     }
264                     String JavaDoc rawtext = Util.findText(kid);
265                     assert rawtext != null : "Null content for <built-to> in " + project;
266                     String JavaDoc text = project.evaluator().evaluate(rawtext);
267                     if (text == null) {
268                         continue;
269                     }
270                     addPathFromProjectEvaluated(entries, text);
271                 }
272                 extraCompilationUnitsExecute.put(pkgroot, ClassPathSupport.createClassPath(entries));
273             }
274         }
275     }
276     
277     /**
278      * Returns array of all classpaths of the given type in the project.
279      * The result is used for example for GlobalPathRegistry registrations.
280      */

281     public ClassPath[] getProjectClassPaths(String JavaDoc type) {
282         if (ClassPath.BOOT.equals(type)) {
283             FileObject srcDir = project.getSourceDirectory();
284             if (srcDir != null) {
285                 return new ClassPath[] {findClassPath(srcDir, ClassPath.BOOT)};
286             }
287         }
288         List JavaDoc<ClassPath> paths = new ArrayList JavaDoc(3);
289         if (ClassPath.COMPILE.equals(type)) {
290             FileObject srcDir = project.getSourceDirectory();
291             if (srcDir != null) {
292                 paths.add(findClassPath(srcDir, ClassPath.COMPILE));
293             }
294             FileObject testSrcDir = project.getTestSourceDirectory();
295             if (testSrcDir != null) {
296                 paths.add(findClassPath(testSrcDir, ClassPath.COMPILE));
297             }
298             FileObject funcTestSrcDir = project.getFunctionalTestSourceDirectory();
299             if (funcTestSrcDir != null) {
300                 paths.add(findClassPath(funcTestSrcDir, ClassPath.COMPILE));
301             }
302             calculateExtraCompilationUnits();
303             paths.addAll(extraCompilationUnitsCompile.values());
304         } else if (ClassPath.EXECUTE.equals(type)) {
305             FileObject srcDir = project.getSourceDirectory();
306             if (srcDir != null) {
307                 paths.add(findClassPath(srcDir, ClassPath.EXECUTE));
308             }
309             FileObject testSrcDir = project.getTestSourceDirectory();
310             if (testSrcDir != null) {
311                 paths.add(findClassPath(testSrcDir, ClassPath.EXECUTE));
312             }
313             FileObject funcTestSrcDir = project.getFunctionalTestSourceDirectory();
314             if (funcTestSrcDir != null) {
315                 paths.add(findClassPath(funcTestSrcDir, ClassPath.EXECUTE));
316             }
317             calculateExtraCompilationUnits();
318             paths.addAll(extraCompilationUnitsExecute.values());
319         } else if (ClassPath.SOURCE.equals(type)) {
320             FileObject srcDir = project.getSourceDirectory();
321             if (srcDir != null) {
322                 paths.add(findClassPath(srcDir, ClassPath.SOURCE));
323             }
324             FileObject testSrcDir = project.getTestSourceDirectory();
325             if (testSrcDir != null) {
326                 paths.add(findClassPath(testSrcDir, ClassPath.SOURCE));
327             }
328             FileObject funcTestSrcDir = project.getFunctionalTestSourceDirectory();
329             if (funcTestSrcDir != null) {
330                 paths.add(findClassPath(funcTestSrcDir, ClassPath.SOURCE));
331             }
332             calculateExtraCompilationUnits();
333             Iterator JavaDoc it = extraCompilationUnitsCompile.keySet().iterator();
334             while (it.hasNext()) {
335                 paths.add(ClassPathSupport.createClassPath(new FileObject[] {(FileObject) it.next()}));
336             }
337         }
338         return (ClassPath[])paths.toArray(new ClassPath[paths.size()]);
339     }
340     
341 }
342
Popular Tags