KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > rubyproject > 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.ruby.rubyproject.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.modules.ruby.spi.project.support.rake.PropertyEvaluator;
29 import org.netbeans.spi.java.classpath.ClassPathFactory;
30 import org.netbeans.spi.java.classpath.ClassPathProvider;
31 import org.netbeans.modules.ruby.rubyproject.SourceRoots;
32 import org.netbeans.modules.ruby.spi.project.support.rake.RakeProjectHelper;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileUtil;
35 import org.openide.util.WeakListeners;
36
37 /**
38  * Defines the various class paths for a J2SE project.
39  */

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

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

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

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

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