1 19 20 package org.netbeans.modules.retouche.source; 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.retouche.source.ClassIndex; 33 import org.netbeans.modules.retouche.source.usages.ClassIndexManager; 34 import org.netbeans.modules.retouche.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 51 public class CacheClassPath implements ClassPathImplementation, PropertyChangeListener { 52 53 private final ClassPath cp; 54 private final boolean translate; 55 private final boolean isBoot; 56 private PropertyChangeSupport listeners; 57 private List <PathResourceImplementation> cache; 58 59 60 private CacheClassPath (ClassPath cp, boolean translate, boolean isBoot) { 61 this.listeners = new PropertyChangeSupport (this); 62 this.cp = cp; 63 this.translate = translate; 64 this.isBoot = isBoot; 65 this.cp.addPropertyChangeListener (WeakListeners.propertyChange(this,cp)); 66 } 67 68 public void removePropertyChangeListener(final PropertyChangeListener listener) { 69 this.listeners.removePropertyChangeListener(listener); 70 } 71 72 public void addPropertyChangeListener(final PropertyChangeListener listener) { 73 this.listeners.addPropertyChangeListener(listener); 74 } 75 76 public void propertyChange (final PropertyChangeEvent event) { 77 if (ClassPath.PROP_ENTRIES.equals(event.getPropertyName())) { 78 synchronized (this) { 79 this.cache = null; 80 } 81 this.listeners.firePropertyChange(PROP_RESOURCES,null,null); 82 } 83 } 84 85 public synchronized List <? extends PathResourceImplementation> getResources() { 86 if (this.cache == null) { 87 List <ClassPath.Entry> entries = this.cp.entries(); 88 this.cache = new LinkedList <PathResourceImplementation> (); 89 if (isBoot && entries.size() == 0) { 90 for (URL url : ClassIndexManager.getDefault().getBootRoots()) { 98 this.cache.add (ClassPathSupport.createResource(url)); 99 } 100 } 101 else { 102 final GlobalSourcePath gsp = GlobalSourcePath.getDefault(); 103 for (ClassPath.Entry entry : entries) { 104 URL url = entry.getURL(); 105 URL [] sourceUrls; 106 if (translate) { 107 sourceUrls = gsp.getSourceRootForBinaryRoot(url, this.cp, true); 108 } 109 else { 110 sourceUrls = new URL [] {url}; 111 } 112 if (sourceUrls != null) { 113 for (URL sourceUrl : sourceUrls) { 114 try { 115 File cacheFolder = Index.getClassFolder(sourceUrl); 116 URL cacheUrl = cacheFolder.toURI().toURL(); 117 if (!cacheFolder.exists()) { 118 cacheUrl = new URL (cacheUrl.toExternalForm()+"/"); } 120 this.cache.add(ClassPathSupport.createResource(cacheUrl)); 121 } catch (IOException ioe) { 122 ErrorManager.getDefault().notify(ioe); 123 } 124 } 125 } else { 126 this.cache.add (ClassPathSupport.createResource(url)); 127 } 128 } 129 } 130 } 131 return this.cache; 132 } 133 134 135 public static ClassPath forClassPath (final ClassPath cp) { 136 assert cp != null; 137 return ClassPathFactory.createClassPath(new CacheClassPath(cp,true,false)); 138 } 139 140 public static ClassPath forBootPath (final ClassPath cp) { 141 assert cp != null; 142 return ClassPathFactory.createClassPath(new CacheClassPath(cp,true,true)); 143 } 144 145 public static ClassPath forSourcePath (final ClassPath sourcePath) { 146 assert sourcePath != null; 147 return ClassPathFactory.createClassPath(new CacheClassPath(sourcePath,false,false)); 148 } 149 150 } 151 | Popular Tags |