KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > railsprojects > 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.railsprojects.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.railsprojects.SourceRoots;
29 import org.netbeans.modules.ruby.railsprojects.classpath.SourcePathImplementation;
30 import org.netbeans.modules.ruby.spi.project.support.rake.PropertyEvaluator;
31 import org.netbeans.spi.java.classpath.ClassPathFactory;
32 import org.netbeans.spi.java.classpath.ClassPathProvider;
33 import org.netbeans.modules.ruby.spi.project.support.rake.RakeProjectHelper;
34 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
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 J2SE project.
41  */

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

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

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

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

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

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