1 19 package org.netbeans.modules.ruby.rubyproject.classpath; 20 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyChangeListener ; 23 import java.io.File ; 24 import java.util.Map ; 25 import java.util.HashMap ; 26 import org.netbeans.api.java.classpath.ClassPath; 27 import org.netbeans.api.project.SourceGroup; 28 import org.netbeans.modules.ruby.spi.project.support.rake.PropertyEvaluator; 29 import org.netbeans.spi.java.classpath.ClassPathFactory; 30 import org.netbeans.spi.java.classpath.ClassPathProvider; 31 import org.netbeans.modules.ruby.rubyproject.SourceRoots; 32 import org.netbeans.modules.ruby.spi.project.support.rake.RakeProjectHelper; 33 import org.openide.filesystems.FileObject; 34 import org.openide.filesystems.FileUtil; 35 import org.openide.util.WeakListeners; 36 37 40 public final class ClassPathProviderImpl implements ClassPathProvider, PropertyChangeListener { 41 42 private static final String BUILD_CLASSES_DIR = "build.classes.dir"; private static final String DIST_JAR = "dist.jar"; private static final String BUILD_TEST_CLASSES_DIR = "build.test.classes.dir"; 46 private static final String JAVAC_CLASSPATH = "javac.classpath"; private static final String JAVAC_TEST_CLASSPATH = "javac.test.classpath"; private static final String RUN_CLASSPATH = "run.classpath"; private static final String RUN_TEST_CLASSPATH = "run.test.classpath"; 51 52 private final RakeProjectHelper helper; 53 private final File projectDirectory; 54 private final PropertyEvaluator evaluator; 55 private final SourceRoots sourceRoots; 56 private final SourceRoots testSourceRoots; 57 private final ClassPath[] cache = new ClassPath[8]; 58 59 private final Map <String ,FileObject> dirCache = new HashMap <String ,FileObject>(); 60 61 public ClassPathProviderImpl(RakeProjectHelper helper, PropertyEvaluator evaluator, SourceRoots sourceRoots, 62 SourceRoots testSourceRoots) { 63 this.helper = helper; 64 this.projectDirectory = FileUtil.toFile(helper.getProjectDirectory()); 65 assert this.projectDirectory != null; 66 this.evaluator = evaluator; 67 this.sourceRoots = sourceRoots; 68 this.testSourceRoots = testSourceRoots; 69 evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator)); 70 } 71 72 private synchronized FileObject getDir(String propname) { 73 FileObject fo = (FileObject) this.dirCache.get (propname); 74 if (fo == null || !fo.isValid()) { 75 String prop = evaluator.getProperty(propname); 76 if (prop != null) { 77 fo = helper.resolveFileObject(prop); 78 this.dirCache.put (propname, fo); 79 } 80 } 81 return fo; 82 } 83 84 85 private FileObject[] getPrimarySrcPath() { 86 return this.sourceRoots.getRoots(); 87 } 88 89 private FileObject[] getTestSrcDir() { 90 return this.testSourceRoots.getRoots(); 91 } 92 93 private FileObject getBuildClassesDir() { 94 return getDir(BUILD_CLASSES_DIR); 95 } 96 97 private FileObject getDistJar() { 98 return getDir(DIST_JAR); 99 } 100 101 private FileObject getBuildTestClassesDir() { 102 return getDir(BUILD_TEST_CLASSES_DIR); 103 } 104 105 117 private int getType(FileObject file) { 118 FileObject[] srcPath = getPrimarySrcPath(); 119 for (int i=0; i < srcPath.length; i++) { 120 FileObject root = srcPath[i]; 121 if (root.equals(file) || FileUtil.isParentOf(root, file)) { 122 return 0; 123 } 124 } 125 srcPath = getTestSrcDir(); 126 for (int i=0; i< srcPath.length; i++) { 127 FileObject root = srcPath[i]; 128 if (root.equals(file) || FileUtil.isParentOf(root, file)) { 129 return 1; 130 } 131 } 132 FileObject dir = getBuildClassesDir(); 133 if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir, file))) { 134 return 2; 135 } 136 dir = getDistJar(); if (dir != null && dir.equals(FileUtil.getArchiveFile(file))) { 138 return 4; 140 } 141 dir = getBuildTestClassesDir(); 142 if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir,file))) { 143 return 3; 144 } 145 return -1; 146 } 147 148 private ClassPath getSourcepath(FileObject file) { 211 int type = getType(file); 212 return this.getSourcepath(type); 213 } 214 215 private ClassPath getSourcepath(int type) { 216 if (type < 0 || type > 1) { 217 return null; 218 } 219 ClassPath cp = cache[type]; 220 if (cp == null) { 221 switch (type) { 222 case 0: 223 cp = ClassPathFactory.createClassPath(new SourcePathImplementation (this.sourceRoots, helper, evaluator)); 224 break; 225 case 1: 226 cp = ClassPathFactory.createClassPath(new SourcePathImplementation (this.testSourceRoots)); 227 break; 228 } 229 } 230 cache[type] = cp; 231 return cp; 232 } 233 234 private ClassPath getBootClassPath() { 235 ClassPath cp = cache[7]; 236 if ( cp== null ) { 237 cp = ClassPathFactory.createClassPath(new BootClassPathImplementation(evaluator)); 238 cache[7] = cp; 239 } 240 return cp; 241 } 242 243 public ClassPath findClassPath(FileObject file, String type) { 244 if (type.equals(ClassPath.SOURCE)) { 247 return getSourcepath(file); 248 } else if (type.equals(ClassPath.BOOT)) { 249 return getBootClassPath(); 250 } else if (type.equals(ClassPath.COMPILE)) { 251 return getBootClassPath(); 253 } else { 254 return null; 255 } 256 } 257 258 262 public ClassPath[] getProjectClassPaths(String type) { 263 if (ClassPath.BOOT.equals(type)) { 264 return new ClassPath[]{getBootClassPath()}; 265 } 266 if (ClassPath.SOURCE.equals(type)) { 273 ClassPath[] l = new ClassPath[2]; 274 l[0] = getSourcepath(0); 275 l[1] = getSourcepath(1); 276 return l; 277 } 278 return null; 280 } 281 282 286 public ClassPath getProjectSourcesClassPath(String type) { 287 if (ClassPath.SOURCE.equals(type)) { 288 return getSourcepath(0); 289 } 290 return null; 295 } 296 297 public synchronized void propertyChange(PropertyChangeEvent evt) { 298 dirCache.remove(evt.getPropertyName()); 299 } 300 301 public String getPropertyName (SourceGroup sg, String type) { 302 FileObject root = sg.getRootFolder(); 303 FileObject[] path = getPrimarySrcPath(); 304 for (int i=0; i<path.length; i++) { 305 if (root.equals(path[i])) { 306 if (ClassPath.COMPILE.equals(type)) { 307 return JAVAC_CLASSPATH; 308 } 309 else if (ClassPath.EXECUTE.equals(type)) { 310 return RUN_CLASSPATH; 311 } 312 else { 313 return null; 314 } 315 } 316 } 317 path = getTestSrcDir(); 318 for (int i=0; i<path.length; i++) { 319 if (root.equals(path[i])) { 320 if (ClassPath.COMPILE.equals(type)) { 321 return JAVAC_TEST_CLASSPATH; 322 } 323 else if (ClassPath.EXECUTE.equals(type)) { 324 return RUN_TEST_CLASSPATH; 325 } 326 else { 327 return null; 328 } 329 } 330 } 331 return null; 332 } 333 334 } 335 | Popular Tags |