KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > 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.j2ee.ejbjarproject.queries;
20
21 import java.beans.PropertyChangeEvent JavaDoc;
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.io.File JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import javax.swing.event.ChangeEvent JavaDoc;
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 JavaDoc;
35 import java.net.MalformedURLException JavaDoc;
36 import javax.swing.event.ChangeListener JavaDoc;
37 import org.netbeans.api.java.queries.SourceForBinaryQuery;
38 import java.util.Map JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import org.openide.filesystems.FileUtil;
41
42 /**
43  * Finds sources corresponding to binaries in a J2SE project.
44  * @author Jesse Glick, Tomas Zezula
45  */

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 JavaDoc/*<URL,SourceForBinaryQuery.Result>*/ cache = new HashMap JavaDoc ();
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 JavaDoc binaryRoot) {
63         if (FileUtil.getArchiveFile(binaryRoot) != null) {
64             binaryRoot = FileUtil.getArchiveFile(binaryRoot);
65             // XXX check whether this is really the root
66
}
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)) { //NOI18N
73
src = this.sourceRoots;
74         }
75         else if (hasSources (binaryRoot, EjbJarProjectProperties.DIST_JAR)) { //NOI18N
76
src = this.sourceRoots;
77         }
78         else if (hasSources (binaryRoot, EjbJarProjectProperties.BUILD_TEST_CLASSES_DIR)) { //NOI18N
79
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 JavaDoc binaryRoot, String JavaDoc binaryProperty) {
93         try {
94             //TODO: Fix this. Use FileUtil.getArchiveFile.
95
if (binaryRoot.getProtocol().equals("jar")) { // NOI18N
96
// We are interested in the JAR file itself.
97
// Note that this impl therefore accepts *both* file:/tmp/foo.jar
98
// and jar:file:/tmp/foo.jar!/ as equivalent (like URLClassLoader).
99
String JavaDoc surl = binaryRoot.toExternalForm();
100                 if (surl.endsWith("!/")) { // NOI18N
101
binaryRoot = new URL JavaDoc(surl.substring(4, surl.length() - 2));
102                 } else if (surl.lastIndexOf("!/") == -1) { // NOI18N
103
// Legal??
104
binaryRoot = new URL JavaDoc(surl.substring(4));
105                 } else {
106                     // Some specific path, e.g. jar:file:/tmp/foo.jar!/foo/,
107
// which we do not support.
108
}
109             }
110             String JavaDoc outDir = helper.getStandardPropertyEvaluator ().getProperty (binaryProperty);
111             if (outDir != null) {
112                 File JavaDoc f = helper.resolveFile (outDir);
113                 URL JavaDoc url = f.toURI().toURL();
114                 if (!f.exists() && !f.getPath().toLowerCase().endsWith(".jar")) { // NOI18N
115
// non-existing
116
assert !url.toExternalForm().endsWith("/") : f; // NOI18N
117
url = new URL JavaDoc(url.toExternalForm() + "/"); // NOI18N
118
}
119                 if (url.equals (binaryRoot)) {
120                     return true;
121                 }
122             }
123         } catch (MalformedURLException JavaDoc malformedURL) {
124             ErrorManager.getDefault().notify(malformedURL);
125         }
126         return false;
127     }
128     
129     private static class Result implements SourceForBinaryQuery.Result, PropertyChangeListener JavaDoc {
130         
131         private ArrayList JavaDoc 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(); //No need to cache it, SourceRoots does
141
}
142         
143         public void addChangeListener (ChangeListener JavaDoc l) {
144             //TODO: Implement this if needed (source folder can be changed)
145
}
146         
147         public void removeChangeListener (ChangeListener JavaDoc l) {
148             //TODO: Implement this if needed (source folder can be changed)
149
}
150         
151         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
152             if (SourceRoots.PROP_ROOTS.equals(evt.getPropertyName())) {
153                 this.fireChange ();
154             }
155         }
156
157         private void fireChange() {
158             Iterator JavaDoc it;
159             synchronized (this) {
160                 if (this.listeners == null) {
161                     return;
162                 }
163                 it = ((ArrayList JavaDoc)this.listeners.clone()).iterator();
164             }
165             ChangeEvent JavaDoc event = new ChangeEvent JavaDoc(this);
166             while (it.hasNext()) {
167                 ((ChangeListener JavaDoc)it.next()).stateChanged(event);
168             }
169         }
170         
171     }
172     
173 }
174
Popular Tags