1 19 20 package org.netbeans.modules.apisupport.project.queries; 21 22 import java.io.File ; 23 import java.net.URL ; 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 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 ; 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 <FileObject,ClassPath> extraCompilationUnitsCompile = null; 65 private Map <FileObject,ClassPath> extraCompilationUnitsExecute = null; 66 67 public ClassPath findClassPath(FileObject file, String type) { 68 if (type.equals(ClassPath.BOOT)) { 69 if (boot == null) { 70 boot = ClassPathFactory.createClassPath(createPathFromProperty("nbjdk.bootclasspath")); } 72 return boot; 73 } 74 FileObject srcDir = project.getSourceDirectory(); 75 FileObject testSrcDir = project.getTestSourceDirectory(); 76 FileObject funcTestSrcDir = project.getFunctionalTestSourceDirectory(); 77 File 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 moduleJar = project.getModuleJarLocation(); 82 if (srcDir != null && (FileUtil.isParentOf(srcDir, file) || file == srcDir)) { 83 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 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 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 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 roots = new ArrayList (); 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 roots = new ArrayList (); 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 it = extraCompilationUnitsCompile.entrySet().iterator(); 174 while (it.hasNext()) { 175 Map.Entry entry = (Map.Entry ) 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 return ClassPathSupport.createClassPath(new FileObject[] {pkgroot}); 185 } else { 186 break; 187 } 188 } 189 } 190 } 191 return null; 193 } 194 195 private ClassPathImplementation createPathFromProperty(String prop) { 196 return ProjectClassPathSupport.createPropertyBasedClassPathImplementation( 197 project.getProjectDirectoryFile(), project.evaluator(), new String [] {prop}); 198 } 199 200 201 private ClassPathImplementation createCompileClasspath() { 202 return createPathFromProperty("cp"); } 204 205 private void addPathFromProjectEvaluated(List <PathResourceImplementation> entries, String path) { 206 if (path != null) { 207 String [] 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"); } 217 218 private ClassPathImplementation createTestExecuteClasspath() { 219 return createPathFromProperty("test.unit.run.cp"); } 221 222 private ClassPathImplementation createFuncTestCompileClasspath() { 223 return createPathFromProperty("test.qa-functional.cp"); } 225 226 private ClassPathImplementation createFuncTestExecuteClasspath() { 227 return createPathFromProperty("test.qa-functional.run.cp"); } 229 230 private ClassPathImplementation createExecuteClasspath() { 231 return createPathFromProperty("run.cp"); } 233 234 private void calculateExtraCompilationUnits() { 235 if (extraCompilationUnitsCompile != null) { 236 return; 237 } 238 extraCompilationUnitsCompile = new HashMap (); 239 extraCompilationUnitsExecute = new HashMap (); 240 Iterator <Map.Entry <FileObject,Element >> ecus = project.getExtraCompilationUnits().entrySet().iterator(); 241 while (ecus.hasNext()) { 242 Map.Entry entry = (Map.Entry ) ecus.next(); 243 FileObject pkgroot = (FileObject) entry.getKey(); 244 Element pkgrootEl = (Element ) entry.getValue(); 245 Element classpathEl = Util.findElement(pkgrootEl, "classpath", NbModuleProjectType.NAMESPACE_SHARED); assert classpathEl != null : "no <classpath> in " + pkgrootEl; 247 String classpathS = Util.findText(classpathEl); 248 if (classpathS == null) { 249 extraCompilationUnitsCompile.put(pkgroot, ClassPathSupport.createClassPath(new URL [0])); 250 extraCompilationUnitsExecute.put(pkgroot, ClassPathSupport.createClassPath(new URL [0])); 251 } else { 252 String classpathEval = project.evaluator().evaluate(classpathS); 253 List <PathResourceImplementation> entries = new ArrayList (); 254 addPathFromProjectEvaluated(entries, classpathEval); 255 extraCompilationUnitsCompile.put(pkgroot, ClassPathSupport.createClassPath(entries)); 256 entries = new ArrayList (entries); 258 Iterator <Element > pkgrootKids = Util.findSubElements(pkgrootEl).iterator(); 259 while (pkgrootKids.hasNext()) { 260 Element kid = (Element ) pkgrootKids.next(); 261 if (!kid.getLocalName().equals("built-to")) { continue; 263 } 264 String rawtext = Util.findText(kid); 265 assert rawtext != null : "Null content for <built-to> in " + project; 266 String 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 281 public ClassPath[] getProjectClassPaths(String 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 <ClassPath> paths = new ArrayList (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 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 |