1 19 20 package org.netbeans.modules.j2ee.earproject.classpath; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.lang.ref.Reference ; 25 import java.lang.ref.SoftReference ; 26 import java.util.HashMap ; 27 import java.util.Map ; 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.project.support.ant.AntProjectHelper; 32 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 33 import org.openide.filesystems.FileObject; 34 import org.openide.filesystems.FileUtil; 35 import org.openide.util.WeakListeners; 36 37 40 public final class ClassPathProviderImpl implements ClassPathProvider, PropertyChangeListener { 41 42 private static final int TYPE_NORMAL = 0; 43 private static final int TYPE_BUILT_UNPACKED = 2; 44 private static final int TYPE_BUILT_JAR = 3; 45 private static final int TYPE_OTHER = -1; 46 47 private static final String BUILD_CLASSES_DIR = "build.classes.dir"; private static final String DIST_JAR = "dist.jar"; private static final String DOC_BASE_DIR = "web.docbase.dir"; 51 private final AntProjectHelper helper; 52 private final PropertyEvaluator evaluator; 53 @SuppressWarnings ("unchecked") 54 private final Reference <ClassPath>[] cache = new SoftReference [8]; 55 56 private final Map <String ,FileObject> dirCache = new HashMap <String ,FileObject>(); 57 58 public ClassPathProviderImpl(AntProjectHelper helper, PropertyEvaluator evaluator) { 59 this.helper = helper; 60 this.evaluator = evaluator; 61 evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator)); 62 } 63 64 private synchronized FileObject getDir(String propname) { 65 FileObject fo = this.dirCache.get(propname); 66 if (fo == null || !fo.isValid()) { 67 String prop = evaluator.getProperty(propname); 68 if (prop != null) { 69 fo = helper.resolveFileObject(prop); 70 this.dirCache.put(propname, fo); 71 } 72 } 73 return fo; 74 } 75 76 private FileObject getBuildClassesDir() { 77 return getDir(BUILD_CLASSES_DIR); 78 } 79 80 private FileObject getDistJar() { 81 return getDir(DIST_JAR); 82 } 83 84 private FileObject getDocumentBaseDir() { 85 return getDir(DOC_BASE_DIR); 86 } 87 88 98 private int getType(FileObject file) { 99 FileObject dir = getDocumentBaseDir(); 100 if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir,file))) { 101 return TYPE_BUILT_UNPACKED; 102 } 103 dir = getBuildClassesDir(); 104 if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir, file))) { 105 return TYPE_BUILT_JAR; 106 } 107 dir = getDistJar(); if (dir != null && dir.equals(FileUtil.getArchiveFile(file))) { 109 return TYPE_BUILT_JAR; 111 } 112 return TYPE_OTHER; 113 } 114 115 private ClassPath getCompileTimeClasspath(FileObject file) { 116 int type = getType(file); 117 return this.getCompileTimeClasspath(type); 118 } 119 120 private synchronized ClassPath getCompileTimeClasspath(int type) { 121 if (type < TYPE_NORMAL || type > TYPE_BUILT_UNPACKED) { 122 return null; 124 } 125 if (type == TYPE_BUILT_UNPACKED) type = TYPE_NORMAL; 126 ClassPath cp = null; 127 if (cache[TYPE_BUILT_JAR + type] == null || (cp = cache[TYPE_BUILT_JAR + type].get()) == null) { 128 if (type == TYPE_NORMAL) { 129 cp = ClassPathFactory.createClassPath( 130 new ProjectClassPathImplementation(helper, "${javac.classpath}:${build.classes.dir}", evaluator, false)); } 132 cache[TYPE_BUILT_JAR + type] = new SoftReference <ClassPath>(cp); 133 } 134 return cp; 135 } 136 137 private synchronized ClassPath getRunTimeClasspath(FileObject file) { 138 int type = getType(file); 139 if (type < TYPE_NORMAL || type > 4) { 140 return null; 145 } 146 switch (type){ 147 case TYPE_BUILT_UNPACKED: type = TYPE_NORMAL; break; 148 case TYPE_BUILT_JAR: 149 case 4: type -=3; break; 150 } 151 152 ClassPath cp = null; 153 if (cache[6+type] == null || (cp = cache[6+type].get())== null) { 154 if (type == TYPE_NORMAL) { 155 cp = ClassPathFactory.createClassPath( 162 new ProjectClassPathImplementation(helper, "debug.classpath", evaluator)); } 164 cache[6+type] = new SoftReference <ClassPath>(cp); 165 } 166 return cp; 167 } 168 169 private synchronized ClassPath getBootClassPath() { 170 ClassPath cp = null; 171 if (cache[7] == null || (cp = cache[7].get()) == null) { 172 cp = ClassPathFactory.createClassPath(new BootClassPathImplementation(evaluator)); 173 cache[7] = new SoftReference <ClassPath>(cp); 174 } 175 return cp; 176 } 177 178 public ClassPath findClassPath(FileObject file, String type) { 179 if (type.equals(ClassPath.COMPILE)) { 180 return getCompileTimeClasspath(file); 181 } else if (type.equals(ClassPath.EXECUTE)) { 182 return getRunTimeClasspath(file); 183 } else if (type.equals(ClassPath.BOOT)) { 184 return getBootClassPath(); 185 } else { 186 return null; 187 } 188 } 189 190 194 public ClassPath[] getProjectClassPaths(String type) { 195 if (ClassPath.BOOT.equals(type)) { 196 return new ClassPath[]{getBootClassPath()}; 197 } 198 if (ClassPath.COMPILE.equals(type)) { 199 ClassPath[] l = new ClassPath[1]; 200 l[0] = getCompileTimeClasspath(TYPE_NORMAL); 201 return l; 202 } 203 assert false; 204 return null; 205 } 206 207 public void propertyChange(PropertyChangeEvent evt) { 208 dirCache.remove(evt.getPropertyName()); 209 } 210 211 } 212 213 | Popular Tags |