KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > 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.java.j2seproject.queries;
20
21 import java.io.File JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import java.util.List JavaDoc;
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 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.Map JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import javax.swing.event.ChangeListener JavaDoc;
37 import javax.swing.event.ChangeEvent JavaDoc;
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 /**
44  * Finds sources corresponding to binaries in a J2SE project.
45  * @author Jesse Glick, Tomas Zezula
46  */

47 public class CompiledSourceForBinaryQuery implements SourceForBinaryQueryImplementation {
48     
49     private static final String JavaDoc PROP_BUILD_DIR = "build.dir"; //NOI18N
50

51     private final AntProjectHelper helper;
52     private final PropertyEvaluator evaluator;
53     private final SourceRoots sourceRoots;
54     private final SourceRoots testRoots;
55     private Map JavaDoc<URL JavaDoc,SourceForBinaryQuery.Result> cache = new HashMap JavaDoc<URL JavaDoc,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 JavaDoc binaryRoot) {
65         if (FileUtil.getArchiveFile(binaryRoot) != null) {
66             binaryRoot = FileUtil.getArchiveFile(binaryRoot);
67             // XXX check whether this is really the root
68
}
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")) { //NOI18N
75
src = this.sourceRoots;
76         }
77         else if (hasSources (binaryRoot,"dist.jar")) { //NOI18N
78
src = this.sourceRoots;
79         }
80         else if (hasSources (binaryRoot,"build.test.classes.dir")) { //NOI18N
81
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 JavaDoc binaryRoot, String JavaDoc binaryProperty) {
95         try {
96             String JavaDoc outDir = evaluator.getProperty(binaryProperty);
97             if (outDir != null) {
98                 File JavaDoc f = helper.resolveFile (outDir);
99                 URL JavaDoc url = f.toURI().toURL();
100                 if (!f.exists() && !f.getPath().toLowerCase().endsWith(".jar")) { // NOI18N
101
// non-existing
102
assert !url.toExternalForm().endsWith("/") : f; // NOI18N
103
url = new URL JavaDoc(url.toExternalForm() + "/"); // NOI18N
104
}
105                 if (url.equals (binaryRoot)) {
106                     return true;
107                 }
108             }
109         } catch (MalformedURLException JavaDoc malformedURL) {
110             ErrorManager.getDefault().notify(malformedURL);
111         }
112         return false;
113     }
114     
115     private class Result implements SourceForBinaryQuery.Result, PropertyChangeListener JavaDoc {
116
117         private List JavaDoc<ChangeListener JavaDoc> 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             //todo: May need to cache the result
127
List JavaDoc<FileObject> result = new ArrayList JavaDoc<FileObject>();
128             result.addAll(Arrays.asList(this.sourceRoots.getRoots()));
129             try {
130                 String JavaDoc buildDir = evaluator.getProperty(PROP_BUILD_DIR);
131                 if (buildDir != null) {
132                     // generated/wsclient
133
File JavaDoc f = new File JavaDoc (helper.resolveFile (buildDir),"generated/wsclient"); //NOI18N
134
URL JavaDoc url = f.toURI().toURL();
135                     if (!f.exists()) { //NOI18N
136
assert !url.toExternalForm().endsWith("/"); //NOI18N
137
url = new URL JavaDoc (url.toExternalForm()+'/'); //NOI18N
138
}
139                     FileObject root = URLMapper.findFileObject(url);
140                     if (root != null) {
141                         result.add(root);
142                     }
143
144                     // generated/wsimport/client
145
f = new File JavaDoc (helper.resolveFile(buildDir),"generated/wsimport/client"); //NOI18N
146
url = f.toURI().toURL();
147                     if (!f.exists()) { //NOI18N
148
assert !url.toExternalForm().endsWith("/"); //NOI18N
149
url = new URL JavaDoc (url.toExternalForm()+'/'); //NOI18N
150
}
151                     root = URLMapper.findFileObject(url);
152                     if (root != null) {
153                         result.add(root);
154                     }
155                 }
156             } catch (MalformedURLException JavaDoc ex) {
157                 ErrorManager.getDefault ().notify (ex);
158             }
159             return result.toArray(new FileObject[result.size()]);
160         }
161         
162         public synchronized void addChangeListener (ChangeListener JavaDoc l) {
163             if (this.listeners == null) {
164                 this.listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
165             }
166             this.listeners.add (l);
167         }
168         
169         public synchronized void removeChangeListener (ChangeListener JavaDoc l) {
170             if (this.listeners == null) {
171                 return;
172             }
173             this.listeners.remove (l);
174         }
175
176         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
177             if (SourceRoots.PROP_ROOTS.equals(evt.getPropertyName())) {
178                 this.fireChange ();
179             }
180         }
181
182         private void fireChange() {
183             ChangeListener JavaDoc[] ls;
184             synchronized (this) {
185                 if (this.listeners == null) {
186                     return;
187                 }
188                 ls = listeners.toArray(new ChangeListener JavaDoc[listeners.size()]);
189             }
190             ChangeEvent JavaDoc event = new ChangeEvent JavaDoc(this);
191             for (ChangeListener JavaDoc l : ls) {
192                 l.stateChanged(event);
193             }
194         }
195
196     }
197     
198 }
199
Popular Tags