KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > classpath > ClassPathProviderImpl


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.project.classpath;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.HashMap JavaDoc;
26
27 import org.netbeans.api.java.classpath.ClassPath;
28 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties;
29 import org.netbeans.spi.java.classpath.ClassPathFactory;
30 import org.netbeans.spi.java.classpath.ClassPathProvider;
31 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
32 import org.netbeans.spi.project.support.ant.AntProjectHelper;
33 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
34 import org.netbeans.modules.web.project.SourceRoots;
35 import org.openide.filesystems.FileObject;
36 import org.openide.filesystems.FileUtil;
37 import org.openide.util.WeakListeners;
38
39 /**
40  * Defines the various class paths for a web project.
41  */

42 public final class ClassPathProviderImpl implements ClassPathProvider, PropertyChangeListener JavaDoc {
43     
44     private final AntProjectHelper helper;
45     private final PropertyEvaluator evaluator;
46     private final SourceRoots sourceRoots;
47     private final SourceRoots testSourceRoots;
48     private final ClassPath[] cache = new ClassPath[10];
49
50     private final Map JavaDoc/*<String,FileObject>*/ dirCache = new HashMap JavaDoc();
51
52     public ClassPathProviderImpl(AntProjectHelper helper, PropertyEvaluator evaluator, SourceRoots sourceRoots, SourceRoots testSourceRoots) {
53         this.helper = helper;
54         this.evaluator = evaluator;
55         this.sourceRoots = sourceRoots;
56         this.testSourceRoots = testSourceRoots;
57         evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator));
58     }
59
60     private synchronized FileObject getDir(String JavaDoc propname) {
61         FileObject fo = (FileObject) this.dirCache.get (propname);
62         if (fo == null || !fo.isValid()) {
63             String JavaDoc prop = evaluator.getProperty(propname);
64             if (prop != null) {
65                 fo = helper.resolveFileObject(prop);
66                 this.dirCache.put (propname, fo);
67             }
68         }
69         return fo;
70     }
71     
72     private FileObject[] getPrimarySrcPath() {
73         return this.sourceRoots.getRoots();
74     }
75     
76     private FileObject[] getTestSrcDir() {
77         return this.testSourceRoots.getRoots();
78     }
79
80     private FileObject getBuildClassesDir() {
81         return getDir(WebProjectProperties.BUILD_CLASSES_DIR);
82     }
83     
84     private FileObject getDistJar() {
85         return getDir(WebProjectProperties.DIST_WAR);
86     }
87     
88     private FileObject getBuildTestClassesDir() {
89         return getDir(WebProjectProperties.BUILD_TEST_CLASSES_DIR);
90     }
91
92     private FileObject getDocumentBaseDir() {
93         return getDir(WebProjectProperties.WEB_DOCBASE_DIR);
94     }
95     
96      /**
97      * Find what a given file represents.
98      * @param file a file in the project
99      * @return one of: <dl>
100      * <dt>0</dt> <dd>normal source</dd>
101      * <dt>1</dt> <dd>test source</dd>
102      * <dt>2</dt> <dd>built class (unpacked)</dd>
103      * <dt>3</dt> <dd>built test class</dd>
104      * <dt>4</dt> <dd>built class (in dist JAR)</dd>
105      * <dt>5</dt> <dd>web pages</dd>
106      * <dt>-1</dt> <dd>something else</dd>
107      * </dl>
108      */

109    private int getType(FileObject file) {
110         FileObject[] srcPath = getPrimarySrcPath();
111         for (int i=0; i < srcPath.length; i++) {
112             FileObject root = srcPath[i];
113             if (root.equals(file) || FileUtil.isParentOf(root, file)) {
114                 return 0;
115             }
116         }
117         srcPath = getTestSrcDir();
118         for (int i=0; i< srcPath.length; i++) {
119             FileObject root = srcPath[i];
120             if (root.equals(file) || FileUtil.isParentOf(root, file)) {
121                 return 1;
122             }
123         }
124         FileObject dir = getDocumentBaseDir();
125         if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir,file))) {
126             return 5;
127         }
128         dir = getBuildClassesDir();
129         if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir, file))) {
130             return 2;
131         }
132         dir = getDistJar(); // not really a dir at all, of course
133
if (dir != null && dir.equals(FileUtil.getArchiveFile(file))) {
134             // XXX check whether this is really the root
135
return 4;
136         }
137         dir = getBuildTestClassesDir();
138         if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir,file))) {
139             return 3;
140         }
141         
142         return -1;
143     }
144     
145     private ClassPath getCompileTimeClasspath(FileObject file) {
146         int type = getType(file);
147         return this.getCompileTimeClasspath(type);
148     }
149     
150     private synchronized ClassPath getCompileTimeClasspath(int type) {
151         if ((type < 0 || type > 2) && type != 5) {
152             // Not a source file.
153
return null;
154         }
155         if (type == 2 || type == 5)
156             type = 0;
157         
158         ClassPath cp = cache[3+type];
159         if ( cp == null) {
160             if (type == 0) {
161                 cp = ClassPathFactory.createClassPath(
162                 new ProjectClassPathImplementation(helper, "${javac.classpath}:${"
163                         + WebProjectProperties.J2EE_PLATFORM_CLASSPATH
164                         + "}", evaluator, false)); //NOI18N
165
}
166             else {
167                 cp = ClassPathFactory.createClassPath(
168                 new ProjectClassPathImplementation(helper, "${javac.test.classpath}:${"
169                         + WebProjectProperties.J2EE_PLATFORM_CLASSPATH
170                         + "}", evaluator, false)); //NOI18N
171
}
172             cache[3+type] = cp;
173         }
174         return cp;
175         
176     }
177     
178     private synchronized ClassPath getRunTimeClasspath(FileObject file) {
179         int type = getType(file);
180         if (type < 0 || type > 5) {
181             // Unregistered file, or in a JAR.
182
// For jar:file:$projdir/dist/*.jar!/**/*.class, it is misleading to use
183
// run.classpath since that does not actually contain the file!
184
// (It contains file:$projdir/build/classes/ instead.)
185
return null;
186         }
187         switch (type){
188             case 2:
189             case 3:
190             case 4: type -= 2; break;
191             case 5: type = 0; break;
192         }
193         
194         ClassPath cp = cache[6+type];
195         if ( cp == null ) {
196             if (type == 0) {
197                 //XXX : It should return a classpath for run.classpath property, but
198
// the run.classpath property was removed from the webproject in the past
199
// and I'm a little lazy to return it back in the code:)). In this moment
200
// the run classpath equals to the debug classpath. If the debug classpath
201
// will be different from the run classpath, then the run classpath should
202
// be returned back.
203
cp = ClassPathFactory.createClassPath(
204                 new ProjectClassPathImplementation(helper, "debug.classpath", evaluator)); // NOI18N
205
}
206             cache[6+type] = cp;
207         }
208         return cp;
209     }
210     
211     private ClassPath getSourcepath(FileObject file) {
212         int type = getType(file);
213         return this.getSourcepath(type);
214     }
215     
216     private synchronized ClassPath getSourcepath(int type) {
217         if ((type < 0 || type > 2) && type != 5) {
218             // Unknown.
219
return null;
220         }
221         ClassPath cp = cache[type];
222         if (cp == null) {
223             switch (type) {
224                 case 0:
225                 case 2:
226                     cp = ClassPathFactory.createClassPath(new SourcePathImplementation (this.sourceRoots,helper));
227                     break;
228                 case 1:
229                     cp = ClassPathFactory.createClassPath(new SourcePathImplementation (this.testSourceRoots, helper));
230                     break;
231                 case 5:
232                     cp = ClassPathSupport.createProxyClassPath(new ClassPath[] {
233                         ClassPathFactory.createClassPath(new JspSourcePathImplementation(helper, evaluator)),
234                         ClassPathFactory.createClassPath(new SourcePathImplementation (this.sourceRoots, helper)),
235                     });
236                     break;
237             }
238             cache[type] = cp;
239         }
240         return cp;
241     }
242     
243     private synchronized ClassPath getBootClassPath() {
244         ClassPath cp = cache[7];
245         if (cp == null ) {
246             cp = ClassPathFactory.createClassPath(new BootClassPathImplementation(evaluator));
247             cache[7] = cp;
248         }
249         return cp;
250     }
251     
252     public synchronized ClassPath getJ2eePlatformClassPath() {
253         ClassPath cp = cache[9];
254         if (cp == null) {
255                 cp = ClassPathFactory.createClassPath(
256                 new ProjectClassPathImplementation(helper, "${" + //NOI18N
257
WebProjectProperties.J2EE_PLATFORM_CLASSPATH +
258                         "}", evaluator, false)); //NOI18N
259
cache[9] = cp;
260         }
261         return cp;
262     }
263     
264     public ClassPath findClassPath(FileObject file, String JavaDoc type) {
265         if (type.equals(ClassPath.COMPILE)) {
266             return getCompileTimeClasspath(file);
267         } else if (type.equals(ClassPath.EXECUTE)) {
268             return getRunTimeClasspath(file);
269         } else if (type.equals(ClassPath.SOURCE)) {
270             return getSourcepath(file);
271         } else if (type.equals(ClassPath.BOOT)) {
272             return getBootClassPath();
273         } else {
274             return null;
275         }
276     }
277     
278     /**
279      * Returns array of all classpaths of the given type in the project.
280      * The result is used for example for GlobalPathRegistry registrations.
281      */

282     public ClassPath[] getProjectClassPaths(String JavaDoc type) {
283         if (ClassPath.BOOT.equals(type)) {
284             return new ClassPath[]{getBootClassPath()};
285         }
286         if (ClassPath.COMPILE.equals(type)) {
287             ClassPath[] l = new ClassPath[2];
288             l[0] = getCompileTimeClasspath(0);
289             l[1] = getCompileTimeClasspath(1);
290             return l;
291         }
292         if (ClassPath.SOURCE.equals(type)) {
293             ClassPath[] l = new ClassPath[3];
294             l[0] = getSourcepath(0);
295             l[1] = getSourcepath(5);
296             l[2] = getSourcepath(1);
297             return l;
298         }
299         assert false;
300         return null;
301     }
302     
303     /**
304      * Returns the given type of the classpath for the project sources
305      * (i.e., excluding tests roots). Valid types are SOURCE and COMPILE.
306      */

307     public ClassPath getProjectSourcesClassPath(String JavaDoc type) {
308         if (ClassPath.BOOT.equals(type)) {
309             return getBootClassPath();
310         }
311         if (ClassPath.SOURCE.equals(type)) {
312             return getSourcepath(0);
313         }
314         if (ClassPath.COMPILE.equals(type)) {
315             return getCompileTimeClasspath(0);
316         }
317         assert false;
318         return null;
319     }
320
321     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
322         dirCache.remove(evt.getPropertyName());
323     }
324 }
325
326
Popular Tags