1 19 package org.netbeans.modules.web.project.queries; 20 21 import java.io.File ; 22 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties; 23 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation; 24 import org.netbeans.spi.project.support.ant.AntProjectHelper; 25 import org.openide.ErrorManager; 26 import org.openide.filesystems.FileObject; 27 import org.openide.filesystems.FileUtil; 28 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.Iterator ; 35 import java.util.Map ; 36 import java.util.HashMap ; 37 import javax.swing.event.ChangeListener ; 38 import javax.swing.event.ChangeEvent ; 39 40 import org.netbeans.api.java.queries.SourceForBinaryQuery; 41 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 42 import org.netbeans.modules.web.project.SourceRoots; 43 44 48 public class CompiledSourceForBinaryQuery implements SourceForBinaryQueryImplementation { 49 50 private AntProjectHelper helper; 51 private final PropertyEvaluator evaluator; 52 private final SourceRoots sourceRoots; 53 private final SourceRoots testRoots; 54 private Map cache = new HashMap (); 55 56 public CompiledSourceForBinaryQuery (AntProjectHelper helper, PropertyEvaluator evaluator, SourceRoots srcRoots, SourceRoots testRoots) { 57 this.helper = helper; 58 this.evaluator = evaluator; 59 this.sourceRoots = srcRoots; 60 this.testRoots = testRoots; 61 } 62 63 public SourceForBinaryQuery.Result findSourceRoots(URL binaryRoot) { 64 if (FileUtil.getArchiveFile(binaryRoot) != null) { 65 binaryRoot = FileUtil.getArchiveFile(binaryRoot); 66 } 68 SourceForBinaryQuery.Result res = (SourceForBinaryQuery.Result) cache.get (binaryRoot); 69 if (res != null) { 70 return res; 71 } 72 SourceRoots src = null; 73 if (hasSources(binaryRoot, WebProjectProperties.BUILD_CLASSES_DIR)) { src = this.sourceRoots; 75 } 76 else if (hasSources(binaryRoot, WebProjectProperties.BUILD_EAR_CLASSES_DIR)) { src = this.sourceRoots; 78 } 79 else if (hasSources (binaryRoot, WebProjectProperties.DIST_WAR)) { src = this.sourceRoots; 81 } 82 else if (hasSources (binaryRoot, WebProjectProperties.BUILD_TEST_CLASSES_DIR)) { src = this.testRoots; 84 } 85 if (src == null) { 86 return null; 87 } 88 else { 89 res = new Result (src); 90 cache.put (binaryRoot, res); 91 return res; 92 } 93 } 94 95 96 private boolean hasSources (URL binaryRoot, String binaryProperty) { 97 try { 98 String outDir = evaluator.getProperty(binaryProperty); 99 if (outDir != null) { 100 File f = helper.resolveFile (outDir); 101 URL url = f.toURI().toURL(); 102 if (!f.exists() && !f.getPath().toLowerCase().endsWith(".jar")) { assert !url.toExternalForm().endsWith("/") : f; url = new URL (url.toExternalForm() + "/"); } 107 if (url.equals (binaryRoot)) { 108 return true; 109 } 110 } 111 } catch (MalformedURLException malformedURL) { 112 ErrorManager.getDefault().notify(malformedURL); 113 } 114 return false; 115 } 116 117 private static class Result implements SourceForBinaryQuery.Result, PropertyChangeListener { 118 119 private ArrayList listeners; 120 private SourceRoots sourceRoots; 121 122 public Result (SourceRoots sourceRoots) { 123 this.sourceRoots = sourceRoots; 124 this.sourceRoots.addPropertyChangeListener(this); 125 } 126 127 public FileObject[] getRoots () { 128 return this.sourceRoots.getRoots(); } 130 131 public synchronized void addChangeListener (ChangeListener l) { 132 if (this.listeners == null) { 133 this.listeners = new ArrayList (); 134 } 135 this.listeners.add (l); 136 } 137 138 public synchronized void removeChangeListener (ChangeListener l) { 139 if (this.listeners == null) { 140 return; 141 } 142 this.listeners.remove (l); 143 } 144 145 public void propertyChange(PropertyChangeEvent evt) { 146 if (SourceRoots.PROP_ROOTS.equals(evt.getPropertyName())) { 147 this.fireChange (); 148 } 149 } 150 151 private void fireChange() { 152 Iterator it; 153 synchronized (this) { 154 if (this.listeners == null) { 155 return; 156 } 157 it = ((ArrayList )this.listeners.clone()).iterator(); 158 } 159 ChangeEvent event = new ChangeEvent (this); 160 while (it.hasNext()) { 161 ((ChangeListener )it.next()).stateChanged(event); 162 } 163 } 164 165 } 166 167 } 168 | Popular Tags |