KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > queries > CompiledSourceForBinaryQuery


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.web.project.queries;
20
21 import java.io.File JavaDoc;
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 JavaDoc;
30 import java.net.MalformedURLException JavaDoc;
31 import java.beans.PropertyChangeListener JavaDoc;
32 import java.beans.PropertyChangeEvent JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import javax.swing.event.ChangeListener JavaDoc;
38 import javax.swing.event.ChangeEvent JavaDoc;
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 /**
45  * Finds sources corresponding to binaries in a J2SE project.
46  * @author Jesse Glick, Tomas Zezula
47  */

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 JavaDoc/*<URL,SourceForBinaryQuery.Result>*/ cache = new HashMap JavaDoc ();
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 JavaDoc binaryRoot) {
64         if (FileUtil.getArchiveFile(binaryRoot) != null) {
65             binaryRoot = FileUtil.getArchiveFile(binaryRoot);
66             // XXX check whether this is really the root
67
}
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)) { //NOI18N
74
src = this.sourceRoots;
75         }
76         else if (hasSources(binaryRoot, WebProjectProperties.BUILD_EAR_CLASSES_DIR)) { //NOI18N
77
src = this.sourceRoots;
78         }
79         else if (hasSources (binaryRoot, WebProjectProperties.DIST_WAR)) { //NOI18N
80
src = this.sourceRoots;
81         }
82         else if (hasSources (binaryRoot, WebProjectProperties.BUILD_TEST_CLASSES_DIR)) { //NOI18N
83
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 JavaDoc binaryRoot, String JavaDoc binaryProperty) {
97         try {
98             String JavaDoc outDir = evaluator.getProperty(binaryProperty);
99             if (outDir != null) {
100                 File JavaDoc f = helper.resolveFile (outDir);
101                 URL JavaDoc url = f.toURI().toURL();
102                 if (!f.exists() && !f.getPath().toLowerCase().endsWith(".jar")) { // NOI18N
103
// non-existing
104
assert !url.toExternalForm().endsWith("/") : f; // NOI18N
105
url = new URL JavaDoc(url.toExternalForm() + "/"); // NOI18N
106
}
107                 if (url.equals (binaryRoot)) {
108                     return true;
109                 }
110             }
111         } catch (MalformedURLException JavaDoc malformedURL) {
112             ErrorManager.getDefault().notify(malformedURL);
113         }
114         return false;
115     }
116     
117     private static class Result implements SourceForBinaryQuery.Result, PropertyChangeListener JavaDoc {
118         
119         private ArrayList JavaDoc 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(); //No need to cache it, SourceRoots does
129
}
130         
131         public synchronized void addChangeListener (ChangeListener JavaDoc l) {
132             if (this.listeners == null) {
133                 this.listeners = new ArrayList JavaDoc();
134             }
135             this.listeners.add (l);
136         }
137         
138         public synchronized void removeChangeListener (ChangeListener JavaDoc l) {
139             if (this.listeners == null) {
140                 return;
141             }
142             this.listeners.remove (l);
143         }
144
145         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
146             if (SourceRoots.PROP_ROOTS.equals(evt.getPropertyName())) {
147                 this.fireChange ();
148             }
149         }
150
151         private void fireChange() {
152             Iterator JavaDoc it;
153             synchronized (this) {
154                 if (this.listeners == null) {
155                     return;
156                 }
157                 it = ((ArrayList JavaDoc)this.listeners.clone()).iterator();
158             }
159             ChangeEvent JavaDoc event = new ChangeEvent JavaDoc(this);
160             while (it.hasNext()) {
161                 ((ChangeListener JavaDoc)it.next()).stateChanged(event);
162             }
163         }
164         
165     }
166     
167 }
168
Popular Tags