1 19 20 package org.netbeans.modules.web.project.classpath; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.util.Map ; 25 import java.util.HashMap ; 26 27 import org.netbeans.api.java.classpath.ClassPath; 28 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties; 29 import org.netbeans.spi.java.classpath.ClassPathFactory; 30 import org.netbeans.spi.java.classpath.ClassPathProvider; 31 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 32 import org.netbeans.spi.project.support.ant.AntProjectHelper; 33 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 34 import org.netbeans.modules.web.project.SourceRoots; 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 final AntProjectHelper helper; 45 private final PropertyEvaluator evaluator; 46 private final SourceRoots sourceRoots; 47 private final SourceRoots testSourceRoots; 48 private final ClassPath[] cache = new ClassPath[10]; 49 50 private final Map dirCache = new HashMap (); 51 52 public ClassPathProviderImpl(AntProjectHelper helper, PropertyEvaluator evaluator, SourceRoots sourceRoots, SourceRoots testSourceRoots) { 53 this.helper = helper; 54 this.evaluator = evaluator; 55 this.sourceRoots = sourceRoots; 56 this.testSourceRoots = testSourceRoots; 57 evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator)); 58 } 59 60 private synchronized FileObject getDir(String propname) { 61 FileObject fo = (FileObject) this.dirCache.get (propname); 62 if (fo == null || !fo.isValid()) { 63 String prop = evaluator.getProperty(propname); 64 if (prop != null) { 65 fo = helper.resolveFileObject(prop); 66 this.dirCache.put (propname, fo); 67 } 68 } 69 return fo; 70 } 71 72 private FileObject[] getPrimarySrcPath() { 73 return this.sourceRoots.getRoots(); 74 } 75 76 private FileObject[] getTestSrcDir() { 77 return this.testSourceRoots.getRoots(); 78 } 79 80 private FileObject getBuildClassesDir() { 81 return getDir(WebProjectProperties.BUILD_CLASSES_DIR); 82 } 83 84 private FileObject getDistJar() { 85 return getDir(WebProjectProperties.DIST_WAR); 86 } 87 88 private FileObject getBuildTestClassesDir() { 89 return getDir(WebProjectProperties.BUILD_TEST_CLASSES_DIR); 90 } 91 92 private FileObject getDocumentBaseDir() { 93 return getDir(WebProjectProperties.WEB_DOCBASE_DIR); 94 } 95 96 109 private int getType(FileObject file) { 110 FileObject[] srcPath = getPrimarySrcPath(); 111 for (int i=0; i < srcPath.length; i++) { 112 FileObject root = srcPath[i]; 113 if (root.equals(file) || FileUtil.isParentOf(root, file)) { 114 return 0; 115 } 116 } 117 srcPath = getTestSrcDir(); 118 for (int i=0; i< srcPath.length; i++) { 119 FileObject root = srcPath[i]; 120 if (root.equals(file) || FileUtil.isParentOf(root, file)) { 121 return 1; 122 } 123 } 124 FileObject dir = getDocumentBaseDir(); 125 if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir,file))) { 126 return 5; 127 } 128 dir = getBuildClassesDir(); 129 if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir, file))) { 130 return 2; 131 } 132 dir = getDistJar(); if (dir != null && dir.equals(FileUtil.getArchiveFile(file))) { 134 return 4; 136 } 137 dir = getBuildTestClassesDir(); 138 if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir,file))) { 139 return 3; 140 } 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 > 2) && type != 5) { 152 return null; 154 } 155 if (type == 2 || type == 5) 156 type = 0; 157 158 ClassPath cp = cache[3+type]; 159 if ( cp == null) { 160 if (type == 0) { 161 cp = ClassPathFactory.createClassPath( 162 new ProjectClassPathImplementation(helper, "${javac.classpath}:${" 163 + WebProjectProperties.J2EE_PLATFORM_CLASSPATH 164 + "}", evaluator, false)); } 166 else { 167 cp = ClassPathFactory.createClassPath( 168 new ProjectClassPathImplementation(helper, "${javac.test.classpath}:${" 169 + WebProjectProperties.J2EE_PLATFORM_CLASSPATH 170 + "}", evaluator, false)); } 172 cache[3+type] = cp; 173 } 174 return cp; 175 176 } 177 178 private synchronized ClassPath getRunTimeClasspath(FileObject file) { 179 int type = getType(file); 180 if (type < 0 || type > 5) { 181 return null; 186 } 187 switch (type){ 188 case 2: 189 case 3: 190 case 4: type -= 2; break; 191 case 5: type = 0; break; 192 } 193 194 ClassPath cp = cache[6+type]; 195 if ( cp == null ) { 196 if (type == 0) { 197 cp = ClassPathFactory.createClassPath( 204 new ProjectClassPathImplementation(helper, "debug.classpath", evaluator)); } 206 cache[6+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 > 2) && type != 5) { 218 return null; 220 } 221 ClassPath cp = cache[type]; 222 if (cp == null) { 223 switch (type) { 224 case 0: 225 case 2: 226 cp = ClassPathFactory.createClassPath(new SourcePathImplementation (this.sourceRoots,helper)); 227 break; 228 case 1: 229 cp = ClassPathFactory.createClassPath(new SourcePathImplementation (this.testSourceRoots, helper)); 230 break; 231 case 5: 232 cp = ClassPathSupport.createProxyClassPath(new ClassPath[] { 233 ClassPathFactory.createClassPath(new JspSourcePathImplementation(helper, evaluator)), 234 ClassPathFactory.createClassPath(new SourcePathImplementation (this.sourceRoots, helper)), 235 }); 236 break; 237 } 238 cache[type] = cp; 239 } 240 return cp; 241 } 242 243 private synchronized ClassPath getBootClassPath() { 244 ClassPath cp = cache[7]; 245 if (cp == null ) { 246 cp = ClassPathFactory.createClassPath(new BootClassPathImplementation(evaluator)); 247 cache[7] = cp; 248 } 249 return cp; 250 } 251 252 public synchronized ClassPath getJ2eePlatformClassPath() { 253 ClassPath cp = cache[9]; 254 if (cp == null) { 255 cp = ClassPathFactory.createClassPath( 256 new ProjectClassPathImplementation(helper, "${" + WebProjectProperties.J2EE_PLATFORM_CLASSPATH + 258 "}", evaluator, false)); cache[9] = cp; 260 } 261 return cp; 262 } 263 264 public ClassPath findClassPath(FileObject file, String type) { 265 if (type.equals(ClassPath.COMPILE)) { 266 return getCompileTimeClasspath(file); 267 } else if (type.equals(ClassPath.EXECUTE)) { 268 return getRunTimeClasspath(file); 269 } else if (type.equals(ClassPath.SOURCE)) { 270 return getSourcepath(file); 271 } else if (type.equals(ClassPath.BOOT)) { 272 return getBootClassPath(); 273 } else { 274 return null; 275 } 276 } 277 278 282 public ClassPath[] getProjectClassPaths(String type) { 283 if (ClassPath.BOOT.equals(type)) { 284 return new ClassPath[]{getBootClassPath()}; 285 } 286 if (ClassPath.COMPILE.equals(type)) { 287 ClassPath[] l = new ClassPath[2]; 288 l[0] = getCompileTimeClasspath(0); 289 l[1] = getCompileTimeClasspath(1); 290 return l; 291 } 292 if (ClassPath.SOURCE.equals(type)) { 293 ClassPath[] l = new ClassPath[3]; 294 l[0] = getSourcepath(0); 295 l[1] = getSourcepath(5); 296 l[2] = getSourcepath(1); 297 return l; 298 } 299 assert false; 300 return null; 301 } 302 303 307 public ClassPath getProjectSourcesClassPath(String type) { 308 if (ClassPath.BOOT.equals(type)) { 309 return getBootClassPath(); 310 } 311 if (ClassPath.SOURCE.equals(type)) { 312 return getSourcepath(0); 313 } 314 if (ClassPath.COMPILE.equals(type)) { 315 return getCompileTimeClasspath(0); 316 } 317 assert false; 318 return null; 319 } 320 321 public void propertyChange(PropertyChangeEvent evt) { 322 dirCache.remove(evt.getPropertyName()); 323 } 324 } 325 326 | Popular Tags |