1 19 20 package org.netbeans.modules.j2ee.clientproject.classpath; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.File ; 25 import java.util.Map ; 26 import java.util.HashMap ; 27 28 import org.netbeans.api.java.classpath.ClassPath; 29 import org.netbeans.spi.java.classpath.ClassPathFactory; 30 import org.netbeans.spi.java.classpath.ClassPathProvider; 31 import org.netbeans.spi.java.project.classpath.support.ProjectClassPathSupport; 32 import org.netbeans.spi.project.support.ant.AntProjectHelper; 33 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 34 import org.netbeans.modules.j2ee.clientproject.SourceRoots; 35 import org.netbeans.modules.j2ee.clientproject.ui.customizer.AppClientProjectProperties; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileUtil; 38 import org.openide.util.WeakListeners; 39 40 43 public final class ClassPathProviderImpl implements ClassPathProvider, PropertyChangeListener { 44 45 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"; 49 private final AntProjectHelper helper; 50 private final File projectDirectory; 51 private final PropertyEvaluator evaluator; 52 private final SourceRoots sourceRoots; 53 private final SourceRoots testSourceRoots; 54 private final ClassPath[] cache = new ClassPath[8]; 55 56 private final Map <String ,FileObject> dirCache = new HashMap <String ,FileObject>(); 57 58 public ClassPathProviderImpl(AntProjectHelper helper, PropertyEvaluator evaluator, SourceRoots sourceRoots, 59 SourceRoots testSourceRoots) { 60 this.helper = helper; 61 this.projectDirectory = FileUtil.toFile(helper.getProjectDirectory()); 62 assert this.projectDirectory != null; 63 this.evaluator = evaluator; 64 this.sourceRoots = sourceRoots; 65 this.testSourceRoots = testSourceRoots; 66 evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator)); 67 } 68 69 private synchronized FileObject getDir(String propname) { 70 FileObject fo = (FileObject) this.dirCache.get (propname); 71 if (fo == null || !fo.isValid()) { 72 String prop = helper.getStandardPropertyEvaluator ().getProperty (propname); 73 if (prop != null) { 74 fo = helper.resolveFileObject(prop); 75 this.dirCache.put (propname, fo); 76 } 77 } 78 return fo; 79 } 80 81 82 private FileObject[] getPrimarySrcPath() { 83 return this.sourceRoots.getRoots(); 84 } 85 86 private FileObject[] getTestSrcDir() { 87 return this.testSourceRoots.getRoots(); 88 } 89 90 private FileObject getBuildClassesDir() { 91 return getDir(BUILD_CLASSES_DIR); 92 } 93 94 private FileObject getDistJar() { 95 return getDir(DIST_JAR); 96 } 97 98 private FileObject getBuildTestClassesDir() { 99 return getDir(BUILD_TEST_CLASSES_DIR); 100 } 101 102 114 private int getType(FileObject file) { 115 FileObject[] srcPath = getPrimarySrcPath(); 116 for (int i=0; i < srcPath.length; i++) { 117 FileObject root = srcPath[i]; 118 if (root.equals(file) || FileUtil.isParentOf(root, file)) { 119 return 0; 120 } 121 } 122 srcPath = getTestSrcDir(); 123 for (int i=0; i< srcPath.length; i++) { 124 FileObject root = srcPath[i]; 125 if (root.equals(file) || FileUtil.isParentOf(root, file)) { 126 return 1; 127 } 128 } 129 FileObject dir = getBuildClassesDir(); 130 if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir, file))) { 131 return 2; 132 } 133 dir = getDistJar(); if (dir != null && dir.equals(FileUtil.getArchiveFile(file))) { 135 return 4; 137 } 138 dir = getBuildTestClassesDir(); 139 if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir,file))) { 140 return 3; 141 } 142 return -1; 143 } 144 145 private ClassPath getCompileTimeClasspath(FileObject file) { 146 int type = getType(file); 147 return this.getCompileTimeClasspath(type); 148 } 149 150 private synchronized ClassPath getCompileTimeClasspath(int type) { 151 if (type < 0 || type > 1) { 152 return null; 154 } 155 ClassPath cp = cache[2+type]; 156 if ( cp == null) { 157 if (type == 0) { 158 cp = ClassPathFactory.createClassPath( 159 new ProjectClassPathImplementation(helper, "${javac.classpath}:${" + AppClientProjectProperties.J2EE_PLATFORM_CLASSPATH 161 + "}", evaluator, false)); } 163 else { 164 cp = ClassPathFactory.createClassPath( 165 new ProjectClassPathImplementation(helper, "${javac.test.classpath}:${" + AppClientProjectProperties.J2EE_PLATFORM_CLASSPATH 167 + "}", evaluator, false)); } 169 cache[2+type] = cp; 170 } 171 return cp; 172 } 173 174 private synchronized ClassPath getRunTimeClasspath(FileObject file) { 175 int type = getType(file); 176 if (type < 0 || type > 4) { 177 return null; 182 } else if (type > 1) { 183 type-=2; } 185 ClassPath cp = cache[4+type]; 186 if ( cp == null) { 187 if (type == 0) { 188 cp = ClassPathFactory.createClassPath( 192 ProjectClassPathSupport.createPropertyBasedClassPathImplementation( 193 projectDirectory, evaluator, new String [] {"run.classpath"})); } 195 else if (type == 1) { 196 cp = ClassPathFactory.createClassPath( 197 ProjectClassPathSupport.createPropertyBasedClassPathImplementation( 198 projectDirectory, evaluator, new String [] {"run.test.classpath"})); } 200 else if (type == 2) { 201 cp = ClassPathFactory.createClassPath( 204 ProjectClassPathSupport.createPropertyBasedClassPathImplementation( 205 projectDirectory, evaluator, new String [] {DIST_JAR})); } 207 cache[4+type] = cp; 208 } 209 return cp; 210 } 211 212 private ClassPath getSourcepath(FileObject file) { 213 int type = getType(file); 214 return this.getSourcepath(type); 215 } 216 217 private synchronized 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 synchronized 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.COMPILE)) { 247 return getCompileTimeClasspath(file); 248 } else if (type.equals(ClassPath.EXECUTE)) { 249 return getRunTimeClasspath(file); 250 } else if (type.equals(ClassPath.SOURCE)) { 251 return getSourcepath(file); 252 } else if (type.equals(ClassPath.BOOT)) { 253 return getBootClassPath(); 254 } else { 255 return null; 256 } 257 } 258 259 263 public ClassPath[] getProjectClassPaths(String type) { 264 if (ClassPath.BOOT.equals(type)) { 265 return new ClassPath[]{getBootClassPath()}; 266 } 267 if (ClassPath.COMPILE.equals(type)) { 268 ClassPath[] l = new ClassPath[2]; 269 l[0] = getCompileTimeClasspath(0); 270 l[1] = getCompileTimeClasspath(1); 271 return l; 272 } 273 if (ClassPath.SOURCE.equals(type)) { 274 ClassPath[] l = new ClassPath[2]; 275 l[0] = getSourcepath(0); 276 l[1] = getSourcepath(1); 277 return l; 278 } 279 assert false; 280 return null; 281 } 282 283 287 public ClassPath getProjectSourcesClassPath(String type) { 288 if (ClassPath.SOURCE.equals(type)) { 289 return getSourcepath(0); 290 } 291 if (ClassPath.COMPILE.equals(type)) { 292 return getCompileTimeClasspath(0); 293 } 294 assert false; 295 return null; 296 } 297 298 public synchronized void propertyChange(PropertyChangeEvent evt) { 299 dirCache.remove(evt.getPropertyName()); 300 } 301 302 } 303 304 | Popular Tags |