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