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.openide.ErrorManager; 34 import org.openide.filesystems.URLMapper; 35 import org.openide.filesystems.FileObject; 36 import org.openide.filesystems.FileStateInvalidException; 37 import org.openide.filesystems.FileUtil; 38 import org.netbeans.api.java.queries.JavadocForBinaryQuery; 39 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment; 40 import org.netbeans.modules.j2ee.deployment.impl.ServerInstance; 41 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry; 42 import org.netbeans.modules.j2ee.deployment.common.api.J2eeLibraryTypeProvider; 43 import org.netbeans.modules.j2ee.deployment.plugins.api.J2eePlatformImpl; 44 import org.netbeans.spi.java.queries.JavadocForBinaryQueryImplementation; 45 import org.netbeans.spi.project.libraries.LibraryImplementation; 46 import org.openide.util.WeakListeners; 47 48 49 53 public class J2eePlatformJavadocForBinaryQuery implements JavadocForBinaryQueryImplementation { 54 55 private static int MAX_DEPTH = 3; 56 private final Map normalizedURLCache = new HashMap (); 57 58 59 public J2eePlatformJavadocForBinaryQuery() { 60 } 61 62 public JavadocForBinaryQuery.Result findJavadoc(final URL b) { 63 class R implements JavadocForBinaryQuery.Result, PropertyChangeListener { 64 65 private LibraryImplementation lib; 66 private ArrayList listeners; 67 private URL [] cachedRoots; 68 69 70 public R (LibraryImplementation lib) { 71 this.lib = lib; 72 this.lib.addPropertyChangeListener (WeakListeners.propertyChange(this,this.lib)); 73 } 74 75 public synchronized URL [] getRoots() { 76 if (this.cachedRoots == null) { 77 List l = lib.getContent(J2eeLibraryTypeProvider.VOLUME_TYPE_JAVADOC); 78 List result = new ArrayList (); 79 for (Iterator it = l.iterator(); it.hasNext();) { 80 URL u = (URL ) it.next (); 81 result.add (getIndexFolder(u)); 82 } 83 this.cachedRoots = (URL [])result.toArray(new URL [result.size()]); 84 } 85 return this.cachedRoots; 86 } 87 88 public synchronized void addChangeListener(ChangeListener l) { 89 assert l != null : "Listener can not be null"; 90 if (this.listeners == null) { 91 this.listeners = new ArrayList (); 92 } 93 this.listeners.add (l); 94 } 95 96 public synchronized void removeChangeListener(ChangeListener l) { 97 assert l != null : "Listener can not be null"; 98 if (this.listeners == null) { 99 return; 100 } 101 this.listeners.remove (l); 102 } 103 104 public void propertyChange (PropertyChangeEvent event) { 105 if (LibraryImplementation.PROP_CONTENT.equals(event.getPropertyName())) { 106 synchronized (this) { 107 this.cachedRoots = null; 108 } 109 this.fireChange (); 110 } 111 } 112 113 private void fireChange () { 114 Iterator it = null; 115 synchronized (this) { 116 if (this.listeners == null) { 117 return; 118 } 119 it = ((ArrayList )this.listeners.clone()).iterator (); 120 } 121 ChangeEvent event = new ChangeEvent (this); 122 while (it.hasNext()) { 123 ((ChangeListener )it.next()).stateChanged(event); 124 } 125 } 126 } 127 128 boolean isNormalizedURL = isNormalizedURL(b); 129 130 String [] serverInstanceIDs = Deployment.getDefault().getServerInstanceIDs(); 131 ServerRegistry servReg = ServerRegistry.getInstance(); 132 for (int i=0; i < serverInstanceIDs.length; i++) { 133 ServerInstance serInst = servReg.getServerInstance(serverInstanceIDs[i]); 134 J2eePlatformImpl platformImpl = serInst.getJ2eePlatformImpl(); 135 if (platformImpl == null) continue; LibraryImplementation[] libs = platformImpl.getLibraries(); 137 for (int j=0; j<libs.length; j++) { 138 List jars = libs[j].getContent(J2eeLibraryTypeProvider.VOLUME_TYPE_CLASSPATH); 139 Iterator it = jars.iterator(); 140 while (it.hasNext()) { 141 URL entry = (URL )it.next(); 142 URL normalizedEntry; 143 if (isNormalizedURL) { 144 normalizedEntry = getNormalizedURL(entry); 145 } 146 else { 147 normalizedEntry = entry; 148 } 149 if (normalizedEntry != null && normalizedEntry.equals(b)) { 150 return new R(libs[j]); 151 } 152 } 153 } 154 } 155 return null; 156 } 157 158 private URL getNormalizedURL (URL url) { 159 if (isNormalizedURL(url)) { 161 return url; 162 } 163 URL normalizedURL = (URL ) this.normalizedURLCache.get (url); 167 if (normalizedURL == null) { 168 FileObject fo = URLMapper.findFileObject(url); 169 if (fo != null) { 170 try { 171 normalizedURL = fo.getURL(); 172 this.normalizedURLCache.put (url, normalizedURL); 173 } catch (FileStateInvalidException e) { 174 ErrorManager.getDefault().notify(e); 175 } 176 } 177 } 178 return normalizedURL; 179 } 180 181 187 private static boolean isNormalizedURL (URL url) { 188 if ("jar".equals(url.getProtocol())) { url = FileUtil.getArchiveFile(url); 190 } 191 return "file".equals(url.getProtocol()); } 193 194 200 private static URL getIndexFolder (URL rootURL) { 201 if (rootURL == null) { 202 return null; 203 } 204 FileObject root = URLMapper.findFileObject(rootURL); 205 if (root == null) { 206 return rootURL; 207 } 208 FileObject result = findIndexFolder (root,1); 209 try { 210 return result == null ? rootURL : result.getURL(); 211 } catch (FileStateInvalidException e) { 212 ErrorManager.getDefault().notify(e); 213 return rootURL; 214 } 215 } 216 217 private static FileObject findIndexFolder (FileObject fo, int depth) { 218 if (depth > MAX_DEPTH) { 219 return null; 220 } 221 if (fo.getFileObject("index-files",null)!=null || fo.getFileObject("index-all.html",null)!=null) { return fo; 223 } 224 FileObject[] children = fo.getChildren(); 225 for (int i=0; i< children.length; i++) { 226 if (children[i].isFolder()) { 227 FileObject result = findIndexFolder(children[i], depth+1); 228 if (result != null) { 229 return result; 230 } 231 } 232 } 233 return null; 234 } 235 } 236 | Popular Tags |