1 19 20 package org.netbeans.modules.j2ee.ejbjarproject.classpath; 21 22 import java.io.File ; 23 import java.util.Map ; 24 import java.util.HashMap ; 25 26 import org.netbeans.api.java.classpath.ClassPath; 27 import org.netbeans.modules.j2ee.ejbjarproject.ui.customizer.EjbJarProjectProperties; 28 import org.netbeans.modules.j2ee.ejbjarproject.SourceRoots; 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.AntProjectListener; 34 import org.netbeans.spi.project.support.ant.AntProjectEvent; 35 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileUtil; 38 39 42 public final class ClassPathProviderImpl implements ClassPathProvider, AntProjectListener { 43 44 private final AntProjectHelper helper; 45 private final File projectDirectory; 46 private final PropertyEvaluator evaluator; 47 private final SourceRoots sourceRoots; 48 private final SourceRoots testSourceRoots; 49 50 64 private final ClassPath[] cache = new ClassPath[9]; 65 66 private final Map dirCache = new HashMap (); 67 68 public ClassPathProviderImpl(AntProjectHelper helper, PropertyEvaluator evaluator, 69 SourceRoots sourceRoots, SourceRoots testSourceRoots) { 70 this.helper = helper; 71 this.projectDirectory = FileUtil.toFile(helper.getProjectDirectory()); 72 assert this.projectDirectory != null; 73 this.evaluator = evaluator; 74 this.sourceRoots = sourceRoots; 75 this.testSourceRoots = testSourceRoots; 76 this.helper.addAntProjectListener (this); 77 } 78 79 private synchronized FileObject getDir(String propname) { 80 FileObject fo = (FileObject) this.dirCache.get (propname); 81 if (fo == null || !fo.isValid()) { 82 String prop = helper.getStandardPropertyEvaluator ().getProperty (propname); 83 if (prop != null) { 84 fo = helper.resolveFileObject(prop); 85 this.dirCache.put (propname, fo); 86 } 87 } 88 return fo; 89 } 90 91 private FileObject[] getPrimarySrcPath() { 92 return this.sourceRoots.getRoots(); 93 } 94 95 private FileObject[] getTestSrcDir() { 96 return this.testSourceRoots.getRoots(); 97 } 98 99 private FileObject getBuildClassesDir() { 100 return getDir("build.classes.dir"); } 102 103 private FileObject getBuildJar() { 104 return getDir("dist.jar"); } 106 107 private FileObject getBuildTestClassesDir() { 108 return getDir("build.test.classes.dir"); } 110 111 123 private int getType(FileObject file) { 124 FileObject[] srcPath = getPrimarySrcPath(); 125 for (int i=0; i < srcPath.length; i++) { 126 FileObject root = srcPath[i]; 127 if (root.equals(file) || FileUtil.isParentOf(root, file)) { 128 return 0; 129 } 130 } 131 srcPath = getTestSrcDir(); 132 for (int i=0; i< srcPath.length; i++) { 133 FileObject root = srcPath[i]; 134 if (root.equals(file) || FileUtil.isParentOf(root, file)) { 135 return 1; 136 } 137 } 138 FileObject dir = getBuildClassesDir(); 139 if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir, file))) { 140 return 2; 141 } 142 dir = getBuildJar(); 143 if (dir != null && (dir.equals(file))) { return 4; 145 } 146 dir = getBuildTestClassesDir(); 147 if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir,file))) { 148 return 3; 149 } 150 return -1; 151 } 152 153 private ClassPath getCompileTimeClasspath(FileObject file) { 154 int type = getType(file); 155 return this.getCompileTimeClasspath(type); 156 } 157 158 private synchronized ClassPath getCompileTimeClasspath(int type) { 159 if (type < 0 || type > 1) { 160 return null; 162 } 163 ClassPath cp = cache[2+type]; 164 if ( cp == null) { 165 if (type == 0) { 166 cp = ClassPathFactory.createClassPath( 167 new ProjectClassPathImplementation(helper, "${javac.classpath}:${" + EjbJarProjectProperties.J2EE_PLATFORM_CLASSPATH 169 + "}", evaluator, false)); } 171 else { 172 cp = ClassPathFactory.createClassPath( 173 new ProjectClassPathImplementation(helper, "${javac.test.classpath}:${" + EjbJarProjectProperties.J2EE_PLATFORM_CLASSPATH 175 + "}", evaluator, false)); } 177 cache[2+type] = cp; 178 } 179 return cp; 180 } 181 182 private synchronized ClassPath getRunTimeClasspath(FileObject file) { 183 int type = getType(file); 184 if (type < 0 || type > 4) { 185 return null; 186 } else if (type > 1) { 187 type -= 2; 188 } 189 190 ClassPath cp = cache[4+type]; 191 if (cp == null) { 192 if (type == 0) { 193 cp = ClassPathFactory.createClassPath( 196 ProjectClassPathSupport.createPropertyBasedClassPathImplementation( 197 projectDirectory, evaluator, new String [] {"debug.classpath"})); } 199 else if (type == 1) { 200 cp = ClassPathFactory.createClassPath( 201 ProjectClassPathSupport.createPropertyBasedClassPathImplementation( 202 projectDirectory, evaluator, new String [] {"run.test.classpath"})); } 204 else if (type == 2) { 205 cp = ClassPathFactory.createClassPath( 208 ProjectClassPathSupport.createPropertyBasedClassPathImplementation( 209 projectDirectory, evaluator, new String [] {"dist.jar"})); } 211 212 cache[4+type] = cp; 213 } 214 return cp; 215 } 216 217 private ClassPath getSourcepath(FileObject file) { 218 int type = getType(file); 219 return this.getSourcepath(type); 220 } 221 222 private synchronized ClassPath getSourcepath(int type) { 223 if (type < 0 || type > 1) { 224 return null; 225 } 226 ClassPath cp = cache[type]; 227 if (cp == null) { 228 switch (type) { 229 case 0: 230 cp = ClassPathFactory.createClassPath(new SourcePathImplementation (this.sourceRoots,this.helper)); 231 break; 232 case 1: 233 cp = ClassPathFactory.createClassPath(new SourcePathImplementation (this.testSourceRoots,this.helper)); 234 break; 235 } 236 } 237 cache[type] = cp; 238 return cp; 239 } 240 241 private synchronized ClassPath getBootClassPath() { 242 ClassPath cp = cache[7]; 243 if (cp == null) { 244 cp = ClassPathFactory.createClassPath(new BootClassPathImplementation(evaluator)); 245 cache[7] = cp; 246 } 247 return cp; 248 } 249 250 public synchronized ClassPath getJ2eePlatformClassPath() { 251 ClassPath cp = cache[8]; 252 if (cp == null) { 253 cp = ClassPathFactory.createClassPath( 254 new ProjectClassPathImplementation(helper, "${" + EjbJarProjectProperties.J2EE_PLATFORM_CLASSPATH + 256 "}", evaluator, false)); cache[8] = cp; 258 } 259 return cp; 260 } 261 262 public ClassPath findClassPath(FileObject file, String type) { 263 if (type.equals(ClassPath.COMPILE)) { 264 return getCompileTimeClasspath(file); 265 } else if (type.equals(ClassPath.EXECUTE)) { 266 return getRunTimeClasspath(file); 267 } else if (type.equals(ClassPath.SOURCE)) { 268 return getSourcepath(file); 269 } else if (type.equals(ClassPath.BOOT)) { 270 return getBootClassPath(); 271 } else { 272 return null; 273 } 274 } 275 276 280 public ClassPath[] getProjectClassPaths(String type) { 281 if (ClassPath.BOOT.equals(type)) { 282 return new ClassPath[]{getBootClassPath()}; 283 } 284 if (ClassPath.COMPILE.equals(type)) { 285 ClassPath[] l = new ClassPath[2]; 286 l[0] = getCompileTimeClasspath(0); 287 l[1] = getCompileTimeClasspath(1); 288 return l; 289 } 290 if (ClassPath.SOURCE.equals(type)) { 291 ClassPath[] l = new ClassPath[2]; 292 l[0] = getSourcepath(0); 293 l[1] = getSourcepath(1); 294 return l; 295 } 296 assert false; 297 return null; 298 } 299 300 304 public ClassPath getProjectSourcesClassPath(String type) { 305 if (ClassPath.SOURCE.equals(type)) { 306 return getSourcepath(0); 307 } 308 if (ClassPath.COMPILE.equals(type)) { 309 return getCompileTimeClasspath(0); 310 } 311 assert false; 312 return null; 313 } 314 315 public void configurationXmlChanged(AntProjectEvent ev) { 316 this.dirCache.clear(); 317 } 318 319 public synchronized void propertiesChanged(AntProjectEvent ev) { 320 this.dirCache.clear(); 321 } 322 323 } 324 325 | Popular Tags |