1 19 package org.netbeans.modules.java.j2seproject.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.spi.java.classpath.ClassPathFactory; 29 import org.netbeans.spi.java.classpath.ClassPathProvider; 30 import org.netbeans.spi.java.project.classpath.support.ProjectClassPathSupport; 31 import org.netbeans.spi.project.support.ant.AntProjectHelper; 32 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 33 import org.netbeans.modules.java.j2seproject.SourceRoots; 34 import org.openide.filesystems.FileObject; 35 import org.openide.filesystems.FileUtil; 36 import org.openide.util.WeakListeners; 37 38 41 public final class ClassPathProviderImpl implements ClassPathProvider, PropertyChangeListener { 42 43 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"; 47 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"; 52 53 private final AntProjectHelper helper; 54 private final File projectDirectory; 55 private final PropertyEvaluator evaluator; 56 private final SourceRoots sourceRoots; 57 private final SourceRoots testSourceRoots; 58 private final ClassPath[] cache = new ClassPath[8]; 59 60 private final Map <String ,FileObject> dirCache = new HashMap <String ,FileObject>(); 61 62 public ClassPathProviderImpl(AntProjectHelper helper, PropertyEvaluator evaluator, SourceRoots sourceRoots, 63 SourceRoots testSourceRoots) { 64 this.helper = helper; 65 this.projectDirectory = FileUtil.toFile(helper.getProjectDirectory()); 66 assert this.projectDirectory != null; 67 this.evaluator = evaluator; 68 this.sourceRoots = sourceRoots; 69 this.testSourceRoots = testSourceRoots; 70 evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator)); 71 } 72 73 private synchronized FileObject getDir(String propname) { 74 FileObject fo = (FileObject) this.dirCache.get (propname); 75 if (fo == null || !fo.isValid()) { 76 String prop = evaluator.getProperty(propname); 77 if (prop != null) { 78 fo = helper.resolveFileObject(prop); 79 this.dirCache.put (propname, fo); 80 } 81 } 82 return fo; 83 } 84 85 86 private FileObject[] getPrimarySrcPath() { 87 return this.sourceRoots.getRoots(); 88 } 89 90 private FileObject[] getTestSrcDir() { 91 return this.testSourceRoots.getRoots(); 92 } 93 94 private FileObject getBuildClassesDir() { 95 return getDir(BUILD_CLASSES_DIR); 96 } 97 98 private FileObject getDistJar() { 99 return getDir(DIST_JAR); 100 } 101 102 private FileObject getBuildTestClassesDir() { 103 return getDir(BUILD_TEST_CLASSES_DIR); 104 } 105 106 118 private int getType(FileObject file) { 119 FileObject[] srcPath = getPrimarySrcPath(); 120 for (int i=0; i < srcPath.length; i++) { 121 FileObject root = srcPath[i]; 122 if (root.equals(file) || FileUtil.isParentOf(root, file)) { 123 return 0; 124 } 125 } 126 srcPath = getTestSrcDir(); 127 for (int i=0; i< srcPath.length; i++) { 128 FileObject root = srcPath[i]; 129 if (root.equals(file) || FileUtil.isParentOf(root, file)) { 130 return 1; 131 } 132 } 133 FileObject dir = getBuildClassesDir(); 134 if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir, file))) { 135 return 2; 136 } 137 dir = getDistJar(); if (dir != null && dir.equals(FileUtil.getArchiveFile(file))) { 139 return 4; 141 } 142 dir = getBuildTestClassesDir(); 143 if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir,file))) { 144 return 3; 145 } 146 return -1; 147 } 148 149 private ClassPath getCompileTimeClasspath(FileObject file) { 150 int type = getType(file); 151 return this.getCompileTimeClasspath(type); 152 } 153 154 private synchronized ClassPath getCompileTimeClasspath(int type) { 155 if (type < 0 || type > 1) { 156 return null; 158 } 159 ClassPath cp = cache[2+type]; 160 if ( cp == null) { 161 if (type == 0) { 162 cp = ClassPathFactory.createClassPath( 163 ProjectClassPathSupport.createPropertyBasedClassPathImplementation( 164 projectDirectory, evaluator, new String [] {JAVAC_CLASSPATH})); } 166 else { 167 cp = ClassPathFactory.createClassPath( 168 ProjectClassPathSupport.createPropertyBasedClassPathImplementation( 169 projectDirectory, evaluator, new String [] {JAVAC_TEST_CLASSPATH})); } 171 cache[2+type] = cp; 172 } 173 return cp; 174 } 175 176 private synchronized ClassPath getRunTimeClasspath(FileObject file) { 177 int type = getType(file); 178 if (type < 0 || type > 4) { 179 return null; 184 } else if (type > 1) { 185 type-=2; } 187 ClassPath cp = cache[4+type]; 188 if ( cp == null) { 189 if (type == 0) { 190 cp = ClassPathFactory.createClassPath( 191 ProjectClassPathSupport.createPropertyBasedClassPathImplementation( 192 projectDirectory, evaluator, new String [] {RUN_CLASSPATH})); } 194 else if (type == 1) { 195 cp = ClassPathFactory.createClassPath( 196 ProjectClassPathSupport.createPropertyBasedClassPathImplementation( 197 projectDirectory, evaluator, new String [] {RUN_TEST_CLASSPATH})); } 199 else if (type == 2) { 200 cp = ClassPathFactory.createClassPath( 203 ProjectClassPathSupport.createPropertyBasedClassPathImplementation( 204 projectDirectory, evaluator, new String [] {DIST_JAR})); } 206 cache[4+type] = cp; 207 } 208 return cp; 209 } 210 211 private ClassPath getSourcepath(FileObject file) { 212 int type = getType(file); 213 return this.getSourcepath(type); 214 } 215 216 private synchronized ClassPath getSourcepath(int type) { 217 if (type < 0 || type > 1) { 218 return null; 219 } 220 ClassPath cp = cache[type]; 221 if (cp == null) { 222 switch (type) { 223 case 0: 224 cp = ClassPathFactory.createClassPath(new SourcePathImplementation (this.sourceRoots, helper, evaluator)); 225 break; 226 case 1: 227 cp = ClassPathFactory.createClassPath(new SourcePathImplementation (this.testSourceRoots, helper, evaluator)); 228 break; 229 } 230 } 231 cache[type] = cp; 232 return cp; 233 } 234 235 private synchronized ClassPath getBootClassPath() { 236 ClassPath cp = cache[7]; 237 if ( cp== null ) { 238 cp = ClassPathFactory.createClassPath(new BootClassPathImplementation(evaluator)); 239 cache[7] = cp; 240 } 241 return cp; 242 } 243 244 public ClassPath findClassPath(FileObject file, String type) { 245 if (type.equals(ClassPath.COMPILE)) { 246 return getCompileTimeClasspath(file); 247 } else if (type.equals(ClassPath.EXECUTE)) { 248 return getRunTimeClasspath(file); 249 } else if (type.equals(ClassPath.SOURCE)) { 250 return getSourcepath(file); 251 } else if (type.equals(ClassPath.BOOT)) { 252 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.COMPILE.equals(type)) { 267 ClassPath[] l = new ClassPath[2]; 268 l[0] = getCompileTimeClasspath(0); 269 l[1] = getCompileTimeClasspath(1); 270 return l; 271 } 272 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 assert false; 279 return null; 280 } 281 282 286 public ClassPath getProjectSourcesClassPath(String type) { 287 if (ClassPath.BOOT.equals(type)) { 288 return getBootClassPath(); 289 } 290 if (ClassPath.SOURCE.equals(type)) { 291 return getSourcepath(0); 292 } 293 if (ClassPath.COMPILE.equals(type)) { 294 return getCompileTimeClasspath(0); 295 } 296 assert false; 297 return null; 298 } 299 300 public synchronized void propertyChange(PropertyChangeEvent evt) { 301 dirCache.remove(evt.getPropertyName()); 302 } 303 304 public String getPropertyName (SourceGroup sg, String type) { 305 FileObject root = sg.getRootFolder(); 306 FileObject[] path = getPrimarySrcPath(); 307 for (int i=0; i<path.length; i++) { 308 if (root.equals(path[i])) { 309 if (ClassPath.COMPILE.equals(type)) { 310 return JAVAC_CLASSPATH; 311 } 312 else if (ClassPath.EXECUTE.equals(type)) { 313 return RUN_CLASSPATH; 314 } 315 else { 316 return null; 317 } 318 } 319 } 320 path = getTestSrcDir(); 321 for (int i=0; i<path.length; i++) { 322 if (root.equals(path[i])) { 323 if (ClassPath.COMPILE.equals(type)) { 324 return JAVAC_TEST_CLASSPATH; 325 } 326 else if (ClassPath.EXECUTE.equals(type)) { 327 return RUN_TEST_CLASSPATH; 328 } 329 else { 330 return null; 331 } 332 } 333 } 334 return null; 335 } 336 337 } 338 | Popular Tags |