1 19 20 package org.netbeans.modules.java.j2seplatform.libraries; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.HashMap ; 30 import javax.swing.event.ChangeEvent ; 31 import javax.swing.event.ChangeListener ; 32 import org.netbeans.api.java.queries.SourceForBinaryQuery; 33 import org.netbeans.api.project.libraries.Library; 34 import org.netbeans.api.project.libraries.LibraryManager; 35 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation; 36 import org.openide.ErrorManager; 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.FileStateInvalidException; 39 import org.openide.filesystems.FileUtil; 40 import org.openide.filesystems.URLMapper; 41 import org.openide.util.WeakListeners; 42 43 47 public class J2SELibrarySourceForBinaryQuery implements SourceForBinaryQueryImplementation { 48 49 private final Map cache = new HashMap (); 50 private final Map normalizedURLCache = new HashMap (); 51 52 53 public J2SELibrarySourceForBinaryQuery() {} 54 55 public SourceForBinaryQuery.Result findSourceRoots(URL binaryRoot) { 56 SourceForBinaryQuery.Result res = (SourceForBinaryQuery.Result) this.cache.get (binaryRoot); 57 if (res != null) { 58 return res; 59 } 60 boolean isNormalizedURL = isNormalizedURL(binaryRoot); 61 LibraryManager lm = LibraryManager.getDefault (); 62 Library[] libs = lm.getLibraries(); 63 for (int i=0; i< libs.length; i++) { 64 String type = libs[i].getType (); 65 if (J2SELibraryTypeProvider.LIBRARY_TYPE.equalsIgnoreCase(type)) { 66 List classes = libs[i].getContent(J2SELibraryTypeProvider.VOLUME_TYPE_CLASSPATH); 67 for (Iterator it = classes.iterator(); it.hasNext();) { 68 URL entry = (URL ) it.next(); 69 URL normalizedEntry; 70 if (isNormalizedURL) { 71 normalizedEntry = getNormalizedURL(entry); 72 } 73 else { 74 normalizedEntry = entry; 75 } 76 if (normalizedEntry != null && normalizedEntry.equals(binaryRoot)) { 77 res = new Result(entry, libs[i]); 78 cache.put (binaryRoot, res); 79 return res; 80 } 81 } 82 } 83 } 84 return null; 85 } 86 87 88 private URL getNormalizedURL (URL url) { 89 if (isNormalizedURL(url)) { 91 return url; 92 } 93 URL normalizedURL = (URL ) this.normalizedURLCache.get (url); 97 if (normalizedURL == null) { 98 FileObject fo = URLMapper.findFileObject(url); 99 if (fo != null) { 100 try { 101 normalizedURL = fo.getURL(); 102 this.normalizedURLCache.put (url, normalizedURL); 103 } catch (FileStateInvalidException e) { 104 ErrorManager.getDefault().notify(e); 105 } 106 } 107 } 108 return normalizedURL; 109 } 110 111 117 private static boolean isNormalizedURL (URL url) { 118 if ("jar".equals(url.getProtocol())) { url = FileUtil.getArchiveFile(url); 120 } 121 return "file".equals(url.getProtocol()); } 123 124 125 private static class Result implements SourceForBinaryQuery.Result, PropertyChangeListener { 126 127 private Library lib; 128 private URL entry; 129 private ArrayList listeners; 130 private FileObject[] cache; 131 132 public Result (URL queryFor, Library lib) { 133 this.entry = queryFor; 134 this.lib = lib; 135 this.lib.addPropertyChangeListener ((PropertyChangeListener )WeakListeners.create(PropertyChangeListener .class,this,this.lib)); 136 } 137 138 public synchronized FileObject[] getRoots () { 139 if (this.cache == null) { 140 if (this.lib.getContent(J2SELibraryTypeProvider.VOLUME_TYPE_CLASSPATH).contains(entry)) { 141 List src = this.lib.getContent(J2SELibraryTypeProvider.VOLUME_TYPE_SRC); 142 List result = new ArrayList (); 143 for (Iterator sit = src.iterator(); sit.hasNext();) { 144 FileObject sourceRootURL = URLMapper.findFileObject((URL ) sit.next()); 145 if (sourceRootURL!=null) { 146 result.add (sourceRootURL); 147 } 148 } 149 this.cache = (FileObject[]) result.toArray(new FileObject[result.size()]); 150 } 151 else { 152 this.cache = new FileObject[0]; 153 } 154 } 155 return this.cache; 156 } 157 158 public synchronized void addChangeListener (ChangeListener l) { 159 assert l != null : "Listener cannot be null"; if (this.listeners == null) { 161 this.listeners = new ArrayList (); 162 } 163 this.listeners.add (l); 164 } 165 166 public synchronized void removeChangeListener (ChangeListener l) { 167 assert l != null : "Listener cannot be null"; if (this.listeners == null) { 169 return; 170 } 171 this.listeners.remove (l); 172 } 173 174 public void propertyChange (PropertyChangeEvent event) { 175 if (Library.PROP_CONTENT.equals(event.getPropertyName())) { 176 synchronized (this) { 177 this.cache = null; 178 } 179 this.fireChange (); 180 } 181 } 182 183 private void fireChange () { 184 Iterator it = null; 185 synchronized (this) { 186 if (this.listeners == null) { 187 return; 188 } 189 it = ((ArrayList )this.listeners.clone()).iterator(); 190 } 191 ChangeEvent event = new ChangeEvent (this); 192 while (it.hasNext ()) { 193 ((ChangeListener )it.next()).stateChanged(event); 194 } 195 } 196 197 } 198 199 } 200 | Popular Tags |