KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > 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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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

41 public final class ClassPathProviderImpl implements ClassPathProvider, PropertyChangeListener JavaDoc {
42
43     private static final String JavaDoc BUILD_CLASSES_DIR = "build.classes.dir"; // NOI18N
44
private static final String JavaDoc DIST_JAR = "dist.jar"; // NOI18N
45
private static final String JavaDoc BUILD_TEST_CLASSES_DIR = "build.test.classes.dir"; // NOI18N
46

47     private static final String JavaDoc JAVAC_CLASSPATH = "javac.classpath"; //NOI18N
48
private static final String JavaDoc JAVAC_TEST_CLASSPATH = "javac.test.classpath"; //NOI18N
49
private static final String JavaDoc RUN_CLASSPATH = "run.classpath"; //NOI18N
50
private static final String JavaDoc RUN_TEST_CLASSPATH = "run.test.classpath"; //NOI18N
51

52     
53     private final AntProjectHelper helper;
54     private final File JavaDoc projectDirectory;
55     private final PropertyEvaluator evaluator;
56     private final SourceRoots sourceRoots;
57     private final SourceRoots testSourceRoots;
58     private final ClassPath[] cache = new ClassPath[8];
59
60     private final Map JavaDoc<String JavaDoc,FileObject> dirCache = new HashMap JavaDoc<String JavaDoc,FileObject>();
61
62     public ClassPathProviderImpl(AntProjectHelper helper, PropertyEvaluator evaluator, SourceRoots sourceRoots,
63                                  SourceRoots testSourceRoots) {
64         this.helper = helper;
65         this.projectDirectory = FileUtil.toFile(helper.getProjectDirectory());
66         assert this.projectDirectory != null;
67         this.evaluator = evaluator;
68         this.sourceRoots = sourceRoots;
69         this.testSourceRoots = testSourceRoots;
70         evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator));
71     }
72
73     private synchronized FileObject getDir(String JavaDoc propname) {
74         FileObject fo = (FileObject) this.dirCache.get (propname);
75         if (fo == null || !fo.isValid()) {
76             String JavaDoc prop = evaluator.getProperty(propname);
77             if (prop != null) {
78                 fo = helper.resolveFileObject(prop);
79                 this.dirCache.put (propname, fo);
80             }
81         }
82         return fo;
83     }
84
85     
86     private FileObject[] getPrimarySrcPath() {
87         return this.sourceRoots.getRoots();
88     }
89     
90     private FileObject[] getTestSrcDir() {
91         return this.testSourceRoots.getRoots();
92     }
93     
94     private FileObject getBuildClassesDir() {
95         return getDir(BUILD_CLASSES_DIR);
96     }
97     
98     private FileObject getDistJar() {
99         return getDir(DIST_JAR);
100     }
101     
102     private FileObject getBuildTestClassesDir() {
103         return getDir(BUILD_TEST_CLASSES_DIR);
104     }
105     
106     /**
107      * Find what a given file represents.
108      * @param file a file in the project
109      * @return one of: <dl>
110      * <dt>0</dt> <dd>normal source</dd>
111      * <dt>1</dt> <dd>test source</dd>
112      * <dt>2</dt> <dd>built class (unpacked)</dd>
113      * <dt>3</dt> <dd>built test class</dd>
114      * <dt>4</dt> <dd>built class (in dist JAR)</dd>
115      * <dt>-1</dt> <dd>something else</dd>
116      * </dl>
117      */

118     private int getType(FileObject file) {
119         FileObject[] srcPath = getPrimarySrcPath();
120         for (int i=0; i < srcPath.length; i++) {
121             FileObject root = srcPath[i];
122             if (root.equals(file) || FileUtil.isParentOf(root, file)) {
123                 return 0;
124             }
125         }
126         srcPath = getTestSrcDir();
127         for (int i=0; i< srcPath.length; i++) {
128             FileObject root = srcPath[i];
129             if (root.equals(file) || FileUtil.isParentOf(root, file)) {
130                 return 1;
131             }
132         }
133         FileObject dir = getBuildClassesDir();
134         if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir, file))) {
135             return 2;
136         }
137         dir = getDistJar(); // not really a dir at all, of course
138
if (dir != null && dir.equals(FileUtil.getArchiveFile(file))) {
139             // XXX check whether this is really the root
140
return 4;
141         }
142         dir = getBuildTestClassesDir();
143         if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir,file))) {
144             return 3;
145         }
146         return -1;
147     }
148     
149     private ClassPath getCompileTimeClasspath(FileObject file) {
150         int type = getType(file);
151         return this.getCompileTimeClasspath(type);
152     }
153     
154     private synchronized ClassPath getCompileTimeClasspath(int type) {
155         if (type < 0 || type > 1) {
156             // Not a source file.
157
return null;
158         }
159         ClassPath cp = cache[2+type];
160         if ( cp == null) {
161             if (type == 0) {
162                 cp = ClassPathFactory.createClassPath(
163                     ProjectClassPathSupport.createPropertyBasedClassPathImplementation(
164                     projectDirectory, evaluator, new String JavaDoc[] {JAVAC_CLASSPATH})); // NOI18N
165
}
166             else {
167                 cp = ClassPathFactory.createClassPath(
168                     ProjectClassPathSupport.createPropertyBasedClassPathImplementation(
169                     projectDirectory, evaluator, new String JavaDoc[] {JAVAC_TEST_CLASSPATH})); // NOI18N
170
}
171             cache[2+type] = cp;
172         }
173         return cp;
174     }
175     
176     private synchronized ClassPath getRunTimeClasspath(FileObject file) {
177         int type = getType(file);
178         if (type < 0 || type > 4) {
179             // Unregistered file, or in a JAR.
180
// For jar:file:$projdir/dist/*.jar!/**/*.class, it is misleading to use
181
// run.classpath since that does not actually contain the file!
182
// (It contains file:$projdir/build/classes/ instead.)
183
return null;
184         } else if (type > 1) {
185             type-=2; //Compiled source transform into source
186
}
187         ClassPath cp = cache[4+type];
188         if ( cp == null) {
189             if (type == 0) {
190                 cp = ClassPathFactory.createClassPath(
191                     ProjectClassPathSupport.createPropertyBasedClassPathImplementation(
192                     projectDirectory, evaluator, new String JavaDoc[] {RUN_CLASSPATH})); // NOI18N
193
}
194             else if (type == 1) {
195                 cp = ClassPathFactory.createClassPath(
196                     ProjectClassPathSupport.createPropertyBasedClassPathImplementation(
197                     projectDirectory, evaluator, new String JavaDoc[] {RUN_TEST_CLASSPATH})); // NOI18N
198
}
199             else if (type == 2) {
200                 //Only to make the CompiledDataNode hapy
201
//Todo: Strictly it should return ${run.classpath} - ${build.classes.dir} + ${dist.jar}
202
cp = ClassPathFactory.createClassPath(
203                     ProjectClassPathSupport.createPropertyBasedClassPathImplementation(
204                     projectDirectory, evaluator, new String JavaDoc[] {DIST_JAR})); // NOI18N
205
}
206             cache[4+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 > 1) {
218             return null;
219         }
220         ClassPath cp = cache[type];
221         if (cp == null) {
222             switch (type) {
223                 case 0:
224                     cp = ClassPathFactory.createClassPath(new SourcePathImplementation (this.sourceRoots, helper, evaluator));
225                     break;
226                 case 1:
227                     cp = ClassPathFactory.createClassPath(new SourcePathImplementation (this.testSourceRoots, helper, evaluator));
228                     break;
229             }
230         }
231         cache[type] = cp;
232         return cp;
233     }
234     
235     private synchronized ClassPath getBootClassPath() {
236         ClassPath cp = cache[7];
237         if ( cp== null ) {
238             cp = ClassPathFactory.createClassPath(new BootClassPathImplementation(evaluator));
239             cache[7] = cp;
240         }
241         return cp;
242     }
243     
244     public ClassPath findClassPath(FileObject file, String JavaDoc type) {
245         if (type.equals(ClassPath.COMPILE)) {
246             return getCompileTimeClasspath(file);
247         } else if (type.equals(ClassPath.EXECUTE)) {
248             return getRunTimeClasspath(file);
249         } else if (type.equals(ClassPath.SOURCE)) {
250             return getSourcepath(file);
251         } else if (type.equals(ClassPath.BOOT)) {
252             return getBootClassPath();
253         } else {
254             return null;
255         }
256     }
257     
258     /**
259      * Returns array of all classpaths of the given type in the project.
260      * The result is used for example for GlobalPathRegistry registrations.
261      */

262     public ClassPath[] getProjectClassPaths(String JavaDoc type) {
263         if (ClassPath.BOOT.equals(type)) {
264             return new ClassPath[]{getBootClassPath()};
265         }
266         if (ClassPath.COMPILE.equals(type)) {
267             ClassPath[] l = new ClassPath[2];
268             l[0] = getCompileTimeClasspath(0);
269             l[1] = getCompileTimeClasspath(1);
270             return l;
271         }
272         if (ClassPath.SOURCE.equals(type)) {
273             ClassPath[] l = new ClassPath[2];
274             l[0] = getSourcepath(0);
275             l[1] = getSourcepath(1);
276             return l;
277         }
278         assert false;
279         return null;
280     }
281
282     /**
283      * Returns the given type of the classpath for the project sources
284      * (i.e., excluding tests roots). Valid types are BOOT, SOURCE and COMPILE.
285      */

286     public ClassPath getProjectSourcesClassPath(String JavaDoc type) {
287         if (ClassPath.BOOT.equals(type)) {
288             return getBootClassPath();
289         }
290         if (ClassPath.SOURCE.equals(type)) {
291             return getSourcepath(0);
292         }
293         if (ClassPath.COMPILE.equals(type)) {
294             return getCompileTimeClasspath(0);
295         }
296         assert false;
297         return null;
298     }
299
300     public synchronized void propertyChange(PropertyChangeEvent JavaDoc evt) {
301         dirCache.remove(evt.getPropertyName());
302     }
303     
304     public String JavaDoc getPropertyName (SourceGroup sg, String JavaDoc type) {
305         FileObject root = sg.getRootFolder();
306         FileObject[] path = getPrimarySrcPath();
307         for (int i=0; i<path.length; i++) {
308             if (root.equals(path[i])) {
309                 if (ClassPath.COMPILE.equals(type)) {
310                     return JAVAC_CLASSPATH;
311                 }
312                 else if (ClassPath.EXECUTE.equals(type)) {
313                     return RUN_CLASSPATH;
314                 }
315                 else {
316                     return null;
317                 }
318             }
319         }
320         path = getTestSrcDir();
321         for (int i=0; i<path.length; i++) {
322             if (root.equals(path[i])) {
323                 if (ClassPath.COMPILE.equals(type)) {
324                     return JAVAC_TEST_CLASSPATH;
325                 }
326                 else if (ClassPath.EXECUTE.equals(type)) {
327                     return RUN_TEST_CLASSPATH;
328                 }
329                 else {
330                     return null;
331                 }
332             }
333         }
334         return null;
335     }
336     
337 }
338
Popular Tags