1 19 package org.netbeans.modules.java.j2seproject.queries; 20 21 import java.io.File ; 22 import java.util.Arrays ; 23 import java.util.List ; 24 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation; 25 import org.netbeans.spi.project.support.ant.AntProjectHelper; 26 import org.openide.ErrorManager; 27 import org.openide.filesystems.FileObject; 28 import org.openide.filesystems.FileUtil; 29 import java.net.URL ; 30 import java.net.MalformedURLException ; 31 import java.beans.PropertyChangeListener ; 32 import java.beans.PropertyChangeEvent ; 33 import java.util.ArrayList ; 34 import java.util.Map ; 35 import java.util.HashMap ; 36 import javax.swing.event.ChangeListener ; 37 import javax.swing.event.ChangeEvent ; 38 import org.netbeans.api.java.queries.SourceForBinaryQuery; 39 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 40 import org.netbeans.modules.java.j2seproject.SourceRoots; 41 import org.openide.filesystems.URLMapper; 42 43 47 public class CompiledSourceForBinaryQuery implements SourceForBinaryQueryImplementation { 48 49 private static final String PROP_BUILD_DIR = "build.dir"; 51 private final AntProjectHelper helper; 52 private final PropertyEvaluator evaluator; 53 private final SourceRoots sourceRoots; 54 private final SourceRoots testRoots; 55 private Map <URL ,SourceForBinaryQuery.Result> cache = new HashMap <URL ,SourceForBinaryQuery.Result>(); 56 57 public CompiledSourceForBinaryQuery(AntProjectHelper helper, PropertyEvaluator evaluator, SourceRoots srcRoots, SourceRoots testRoots) { 58 this.helper = helper; 59 this.evaluator = evaluator; 60 this.sourceRoots = srcRoots; 61 this.testRoots = testRoots; 62 } 63 64 public SourceForBinaryQuery.Result findSourceRoots(URL binaryRoot) { 65 if (FileUtil.getArchiveFile(binaryRoot) != null) { 66 binaryRoot = FileUtil.getArchiveFile(binaryRoot); 67 } 69 SourceForBinaryQuery.Result res = cache.get(binaryRoot); 70 if (res != null) { 71 return res; 72 } 73 SourceRoots src = null; 74 if (hasSources(binaryRoot,"build.classes.dir")) { src = this.sourceRoots; 76 } 77 else if (hasSources (binaryRoot,"dist.jar")) { src = this.sourceRoots; 79 } 80 else if (hasSources (binaryRoot,"build.test.classes.dir")) { src = this.testRoots; 82 } 83 if (src == null) { 84 return null; 85 } 86 else { 87 res = new Result (src); 88 cache.put (binaryRoot, res); 89 return res; 90 } 91 } 92 93 94 private boolean hasSources (URL binaryRoot, String binaryProperty) { 95 try { 96 String outDir = evaluator.getProperty(binaryProperty); 97 if (outDir != null) { 98 File f = helper.resolveFile (outDir); 99 URL url = f.toURI().toURL(); 100 if (!f.exists() && !f.getPath().toLowerCase().endsWith(".jar")) { assert !url.toExternalForm().endsWith("/") : f; url = new URL (url.toExternalForm() + "/"); } 105 if (url.equals (binaryRoot)) { 106 return true; 107 } 108 } 109 } catch (MalformedURLException malformedURL) { 110 ErrorManager.getDefault().notify(malformedURL); 111 } 112 return false; 113 } 114 115 private class Result implements SourceForBinaryQuery.Result, PropertyChangeListener { 116 117 private List <ChangeListener > listeners; 118 private SourceRoots sourceRoots; 119 120 public Result (SourceRoots sourceRoots) { 121 this.sourceRoots = sourceRoots; 122 this.sourceRoots.addPropertyChangeListener(this); 123 } 124 125 public FileObject[] getRoots () { 126 List <FileObject> result = new ArrayList <FileObject>(); 128 result.addAll(Arrays.asList(this.sourceRoots.getRoots())); 129 try { 130 String buildDir = evaluator.getProperty(PROP_BUILD_DIR); 131 if (buildDir != null) { 132 File f = new File (helper.resolveFile (buildDir),"generated/wsclient"); URL url = f.toURI().toURL(); 135 if (!f.exists()) { assert !url.toExternalForm().endsWith("/"); url = new URL (url.toExternalForm()+'/'); } 139 FileObject root = URLMapper.findFileObject(url); 140 if (root != null) { 141 result.add(root); 142 } 143 144 f = new File (helper.resolveFile(buildDir),"generated/wsimport/client"); url = f.toURI().toURL(); 147 if (!f.exists()) { assert !url.toExternalForm().endsWith("/"); url = new URL (url.toExternalForm()+'/'); } 151 root = URLMapper.findFileObject(url); 152 if (root != null) { 153 result.add(root); 154 } 155 } 156 } catch (MalformedURLException ex) { 157 ErrorManager.getDefault ().notify (ex); 158 } 159 return result.toArray(new FileObject[result.size()]); 160 } 161 162 public synchronized void addChangeListener (ChangeListener l) { 163 if (this.listeners == null) { 164 this.listeners = new ArrayList <ChangeListener >(); 165 } 166 this.listeners.add (l); 167 } 168 169 public synchronized void removeChangeListener (ChangeListener l) { 170 if (this.listeners == null) { 171 return; 172 } 173 this.listeners.remove (l); 174 } 175 176 public void propertyChange(PropertyChangeEvent evt) { 177 if (SourceRoots.PROP_ROOTS.equals(evt.getPropertyName())) { 178 this.fireChange (); 179 } 180 } 181 182 private void fireChange() { 183 ChangeListener [] ls; 184 synchronized (this) { 185 if (this.listeners == null) { 186 return; 187 } 188 ls = listeners.toArray(new ChangeListener [listeners.size()]); 189 } 190 ChangeEvent event = new ChangeEvent (this); 191 for (ChangeListener l : ls) { 192 l.stateChanged(event); 193 } 194 } 195 196 } 197 198 } 199 | Popular Tags |