1 19 20 package org.netbeans.modules.j2ee.deployment.impl.query; 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 33 import org.netbeans.api.java.queries.SourceForBinaryQuery; 34 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment; 35 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance; 36 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry; 37 import org.netbeans.modules.j2ee.deployment.common.api.J2eeLibraryTypeProvider; 38 import org.netbeans.modules.j2ee.deployment.plugins.api.J2eePlatformImpl; 39 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation; 40 import org.netbeans.spi.project.libraries.LibraryImplementation; 41 import org.openide.ErrorManager; 42 import org.openide.filesystems.FileObject; 43 import org.openide.filesystems.FileStateInvalidException; 44 import org.openide.filesystems.FileUtil; 45 import org.openide.filesystems.URLMapper; 46 import org.openide.util.WeakListeners; 47 48 49 53 public class J2eePlatformSourceForBinaryQuery implements SourceForBinaryQueryImplementation { 54 55 private final Map cache = new HashMap (); 56 private final Map normalizedURLCache = new HashMap (); 57 58 59 public J2eePlatformSourceForBinaryQuery() {} 60 61 public SourceForBinaryQuery.Result findSourceRoots(URL binaryRoot) { 62 SourceForBinaryQuery.Result res = (SourceForBinaryQuery.Result) this.cache.get (binaryRoot); 63 if (res != null) { 64 return res; 65 } 66 boolean isNormalizedURL = isNormalizedURL(binaryRoot); 67 68 String [] serverInstanceIDs = Deployment.getDefault().getServerInstanceIDs(); 69 ServerRegistry servReg = ServerRegistry.getInstance(); 70 for (int i=0; i < serverInstanceIDs.length; i++) { 71 ServerInstance serInst = servReg.getServerInstance(serverInstanceIDs[i]); 72 J2eePlatformImpl platformImpl = serInst.getJ2eePlatformImpl(); 73 if (platformImpl == null) continue; LibraryImplementation[] libs = platformImpl.getLibraries(); 75 for (int j=0; j< libs.length; j++) { 76 String type = libs[j].getType (); 77 List classes = libs[j].getContent(J2eeLibraryTypeProvider.VOLUME_TYPE_CLASSPATH); for (Iterator it = classes.iterator(); it.hasNext();) { 79 URL entry = (URL ) it.next(); 80 URL normalizedEntry; 81 if (isNormalizedURL) { 82 normalizedEntry = getNormalizedURL(entry); 83 } 84 else { 85 normalizedEntry = entry; 86 } 87 if (normalizedEntry != null && normalizedEntry.equals(binaryRoot)) { 88 res = new Result(entry, libs[j]); 89 cache.put (binaryRoot, res); 90 return res; 91 } 92 } 93 } 94 } 95 return null; 96 } 97 98 99 private URL getNormalizedURL (URL url) { 100 if (isNormalizedURL(url)) { 102 return url; 103 } 104 URL normalizedURL = (URL ) this.normalizedURLCache.get (url); 108 if (normalizedURL == null) { 109 FileObject fo = URLMapper.findFileObject(url); 110 if (fo != null) { 111 try { 112 normalizedURL = fo.getURL(); 113 this.normalizedURLCache.put (url, normalizedURL); 114 } catch (FileStateInvalidException e) { 115 ErrorManager.getDefault().notify(e); 116 } 117 } 118 } 119 return normalizedURL; 120 } 121 122 128 private static boolean isNormalizedURL (URL url) { 129 if ("jar".equals(url.getProtocol())) { url = FileUtil.getArchiveFile(url); 131 } 132 return "file".equals(url.getProtocol()); } 134 135 136 private static class Result implements SourceForBinaryQuery.Result, PropertyChangeListener { 137 138 private LibraryImplementation lib; 139 private URL entry; 140 private ArrayList listeners; 141 private FileObject[] cache; 142 143 public Result (URL queryFor, LibraryImplementation aLib) { 144 this.entry = queryFor; 145 this.lib = aLib; 146 this.lib.addPropertyChangeListener ((PropertyChangeListener )WeakListeners.create(PropertyChangeListener .class,this,this.lib)); 147 } 148 149 public synchronized FileObject[] getRoots () { 150 if (cache == null) { 151 if (this.lib.getContent(J2eeLibraryTypeProvider.VOLUME_TYPE_CLASSPATH).contains (entry)) { 152 List src = this.lib.getContent(J2eeLibraryTypeProvider.VOLUME_TYPE_SRC); 153 List result = new ArrayList (); 154 for (Iterator sit = src.iterator(); sit.hasNext();) { 155 FileObject sourceRootURL = URLMapper.findFileObject((URL ) sit.next()); 156 if (sourceRootURL!=null) { 157 result.add (sourceRootURL); 158 } 159 } 160 this.cache = (FileObject[]) result.toArray(new FileObject[result.size()]); 161 } 162 else { 163 this.cache = new FileObject[0]; 164 } 165 } 166 return this.cache; 167 } 168 169 public synchronized void addChangeListener (ChangeListener l) { 170 assert l != null : "Listener cannot be null"; if (this.listeners == null) { 172 this.listeners = new ArrayList (); 173 } 174 this.listeners.add (l); 175 } 176 177 public synchronized void removeChangeListener (ChangeListener l) { 178 assert l != null : "Listener cannot be null"; if (this.listeners == null) { 180 return; 181 } 182 this.listeners.remove (l); 183 } 184 185 public void propertyChange (PropertyChangeEvent event) { 186 if (LibraryImplementation.PROP_CONTENT.equals(event.getPropertyName())) { 187 synchronized (this) { 188 this.cache = null; 189 } 190 this.fireChange (); 191 } 192 } 193 194 private void fireChange () { 195 Iterator it = null; 196 synchronized (this) { 197 if (this.listeners == null) { 198 return; 199 } 200 it = ((ArrayList )this.listeners.clone()).iterator(); 201 } 202 ChangeEvent event = new ChangeEvent (this); 203 while (it.hasNext ()) { 204 ((ChangeListener )it.next()).stateChanged(event); 205 } 206 } 207 208 } 209 210 } 211 | Popular Tags |