1 19 package org.netbeans.modules.j2ee.ejbjarproject.queries; 20 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyChangeListener ; 23 import java.io.File ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import javax.swing.event.ChangeEvent ; 27 import org.netbeans.modules.j2ee.ejbjarproject.SourceRoots; 28 import org.netbeans.modules.j2ee.ejbjarproject.ui.customizer.EjbJarProjectProperties; 29 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation; 30 import org.netbeans.spi.project.support.ant.AntProjectHelper; 31 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 32 import org.openide.ErrorManager; 33 import org.openide.filesystems.FileObject; 34 import java.net.URL ; 35 import java.net.MalformedURLException ; 36 import javax.swing.event.ChangeListener ; 37 import org.netbeans.api.java.queries.SourceForBinaryQuery; 38 import java.util.Map ; 39 import java.util.HashMap ; 40 import org.openide.filesystems.FileUtil; 41 42 46 public class CompiledSourceForBinaryQuery implements SourceForBinaryQueryImplementation { 47 48 private AntProjectHelper helper; 49 private final PropertyEvaluator evaluator; 50 private final SourceRoots sourceRoots; 51 private final SourceRoots testRoots; 52 private Map cache = new HashMap (); 53 54 public CompiledSourceForBinaryQuery (AntProjectHelper helper,PropertyEvaluator evaluator, 55 SourceRoots srcRoots, SourceRoots testRoots) { 56 this.helper = helper; 57 this.evaluator = evaluator; 58 this.sourceRoots = srcRoots; 59 this.testRoots = testRoots; 60 } 61 62 public SourceForBinaryQuery.Result findSourceRoots(URL binaryRoot) { 63 if (FileUtil.getArchiveFile(binaryRoot) != null) { 64 binaryRoot = FileUtil.getArchiveFile(binaryRoot); 65 } 67 SourceForBinaryQuery.Result res = (SourceForBinaryQuery.Result) cache.get (binaryRoot); 68 if (res != null) { 69 return res; 70 } 71 SourceRoots src = null; 72 if (hasSources(binaryRoot, EjbJarProjectProperties.BUILD_CLASSES_DIR)) { src = this.sourceRoots; 74 } 75 else if (hasSources (binaryRoot, EjbJarProjectProperties.DIST_JAR)) { src = this.sourceRoots; 77 } 78 else if (hasSources (binaryRoot, EjbJarProjectProperties.BUILD_TEST_CLASSES_DIR)) { src = this.testRoots; 80 } 81 if (src == null) { 82 return null; 83 } 84 else { 85 res = new Result (src); 86 cache.put (binaryRoot, res); 87 return res; 88 } 89 } 90 91 92 private boolean hasSources (URL binaryRoot, String binaryProperty) { 93 try { 94 if (binaryRoot.getProtocol().equals("jar")) { String surl = binaryRoot.toExternalForm(); 100 if (surl.endsWith("!/")) { binaryRoot = new URL (surl.substring(4, surl.length() - 2)); 102 } else if (surl.lastIndexOf("!/") == -1) { binaryRoot = new URL (surl.substring(4)); 105 } else { 106 } 109 } 110 String outDir = helper.getStandardPropertyEvaluator ().getProperty (binaryProperty); 111 if (outDir != null) { 112 File f = helper.resolveFile (outDir); 113 URL url = f.toURI().toURL(); 114 if (!f.exists() && !f.getPath().toLowerCase().endsWith(".jar")) { assert !url.toExternalForm().endsWith("/") : f; url = new URL (url.toExternalForm() + "/"); } 119 if (url.equals (binaryRoot)) { 120 return true; 121 } 122 } 123 } catch (MalformedURLException malformedURL) { 124 ErrorManager.getDefault().notify(malformedURL); 125 } 126 return false; 127 } 128 129 private static class Result implements SourceForBinaryQuery.Result, PropertyChangeListener { 130 131 private ArrayList listeners; 132 private SourceRoots sourceRoots; 133 134 public Result (SourceRoots sourceRoots) { 135 this.sourceRoots = sourceRoots; 136 this.sourceRoots.addPropertyChangeListener(this); 137 } 138 139 public FileObject[] getRoots () { 140 return this.sourceRoots.getRoots(); } 142 143 public void addChangeListener (ChangeListener l) { 144 } 146 147 public void removeChangeListener (ChangeListener l) { 148 } 150 151 public void propertyChange(PropertyChangeEvent evt) { 152 if (SourceRoots.PROP_ROOTS.equals(evt.getPropertyName())) { 153 this.fireChange (); 154 } 155 } 156 157 private void fireChange() { 158 Iterator it; 159 synchronized (this) { 160 if (this.listeners == null) { 161 return; 162 } 163 it = ((ArrayList )this.listeners.clone()).iterator(); 164 } 165 ChangeEvent event = new ChangeEvent (this); 166 while (it.hasNext()) { 167 ((ChangeListener )it.next()).stateChanged(event); 168 } 169 } 170 171 } 172 173 } 174 | Popular Tags |