1 19 20 package org.netbeans.modules.java.source.classpath; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.PropertyChangeSupport ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.net.URI ; 28 import java.net.URL ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 import org.netbeans.api.java.classpath.ClassPath; 32 import org.netbeans.api.java.platform.JavaPlatform; 33 import org.netbeans.api.java.platform.JavaPlatformManager; 34 import org.netbeans.modules.java.source.usages.Index; 35 import org.netbeans.spi.java.classpath.ClassPathFactory; 36 import org.netbeans.spi.java.classpath.ClassPathImplementation; 37 import org.netbeans.spi.java.classpath.PathResourceImplementation; 38 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 39 import org.openide.ErrorManager; 40 import org.openide.util.WeakListeners; 41 42 46 public class CacheClassPath implements ClassPathImplementation, PropertyChangeListener { 47 48 private final ClassPath cp; 49 private final boolean translate; 50 private final boolean isBoot; 51 private PropertyChangeSupport listeners; 52 private List <PathResourceImplementation> cache; 53 54 55 private CacheClassPath (ClassPath cp, boolean translate, boolean isBoot) { 56 this.listeners = new PropertyChangeSupport (this); 57 this.cp = cp; 58 this.translate = translate; 59 this.isBoot = isBoot; 60 this.cp.addPropertyChangeListener (WeakListeners.propertyChange(this,cp)); 61 } 62 63 public void removePropertyChangeListener(final PropertyChangeListener listener) { 64 this.listeners.removePropertyChangeListener(listener); 65 } 66 67 public void addPropertyChangeListener(final PropertyChangeListener listener) { 68 this.listeners.addPropertyChangeListener(listener); 69 } 70 71 public void propertyChange (final PropertyChangeEvent event) { 72 if (ClassPath.PROP_ENTRIES.equals(event.getPropertyName())) { 73 synchronized (this) { 74 this.cache = null; 75 } 76 this.listeners.firePropertyChange(PROP_RESOURCES,null,null); 77 } 78 } 79 80 public synchronized List <? extends PathResourceImplementation> getResources() { 81 if (this.cache == null) { 82 List <ClassPath.Entry> entries = this.cp.entries(); 83 this.cache = new LinkedList <PathResourceImplementation> (); 84 if (isBoot && entries.size() == 0) { 85 JavaPlatform defaultPlatform = JavaPlatformManager.getDefault().getDefaultPlatform(); 86 assert defaultPlatform != null; 87 entries = defaultPlatform.getBootstrapLibraries().entries(); 88 assert entries.size() > 0; 89 for (ClassPath.Entry entry : entries) { 90 this.cache.add (ClassPathSupport.createResource(entry.getURL())); 91 } 92 } 93 else { 94 final GlobalSourcePath gsp = GlobalSourcePath.getDefault(); 95 for (ClassPath.Entry entry : entries) { 96 URL url = entry.getURL(); 97 URL [] sourceUrls; 98 if (translate) { 99 sourceUrls = gsp.getSourceRootForBinaryRoot(url, this.cp, true); 100 } 101 else { 102 sourceUrls = new URL [] {url}; 103 } 104 if (sourceUrls != null) { 105 for (URL sourceUrl : sourceUrls) { 106 try { 107 File cacheFolder = Index.getClassFolder(sourceUrl); 108 URL cacheUrl = cacheFolder.toURI().toURL(); 109 if (!cacheFolder.exists()) { 110 cacheUrl = new URL (cacheUrl.toExternalForm()+"/"); } 112 this.cache.add(ClassPathSupport.createResource(cacheUrl)); 113 } catch (IOException ioe) { 114 ErrorManager.getDefault().notify(ioe); 115 } 116 } 117 } else { 118 this.cache.add (ClassPathSupport.createResource(url)); 119 } 120 } 121 } 122 } 123 return this.cache; 124 } 125 126 127 public static ClassPath forClassPath (final ClassPath cp) { 128 assert cp != null; 129 return ClassPathFactory.createClassPath(new CacheClassPath(cp,true,false)); 130 } 131 132 public static ClassPath forBootPath (final ClassPath cp) { 133 assert cp != null; 134 return ClassPathFactory.createClassPath(new CacheClassPath(cp,true,true)); 135 } 136 137 public static ClassPath forSourcePath (final ClassPath sourcePath) { 138 assert sourcePath != null; 139 return ClassPathFactory.createClassPath(new CacheClassPath(sourcePath,false,false)); 140 } 141 142 } 143 | Popular Tags |