KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > 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.j2ee.clientproject.classpath;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.File JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27
28 import org.netbeans.api.java.classpath.ClassPath;
29 import org.netbeans.spi.java.classpath.ClassPathFactory;
30 import org.netbeans.spi.java.classpath.ClassPathProvider;
31 import org.netbeans.spi.java.project.classpath.support.ProjectClassPathSupport;
32 import org.netbeans.spi.project.support.ant.AntProjectHelper;
33 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
34 import org.netbeans.modules.j2ee.clientproject.SourceRoots;
35 import org.netbeans.modules.j2ee.clientproject.ui.customizer.AppClientProjectProperties;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileUtil;
38 import org.openide.util.WeakListeners;
39
40 /**
41  * Defines the various class paths for a J2SE project.
42  */

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

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

114     private int getType(FileObject file) {
115         FileObject[] srcPath = getPrimarySrcPath();
116         for (int i=0; i < srcPath.length; i++) {
117             FileObject root = srcPath[i];
118             if (root.equals(file) || FileUtil.isParentOf(root, file)) {
119                 return 0;
120             }
121         }
122         srcPath = getTestSrcDir();
123         for (int i=0; i< srcPath.length; i++) {
124             FileObject root = srcPath[i];
125             if (root.equals(file) || FileUtil.isParentOf(root, file)) {
126                 return 1;
127             }
128         }
129         FileObject dir = getBuildClassesDir();
130         if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir, file))) {
131             return 2;
132         }
133         dir = getDistJar(); // not really a dir at all, of course
134
if (dir != null && dir.equals(FileUtil.getArchiveFile(file))) {
135             // XXX check whether this is really the root
136
return 4;
137         }
138         dir = getBuildTestClassesDir();
139         if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir,file))) {
140             return 3;
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 > 1) {
152             // Not a source file.
153
return null;
154         }
155         ClassPath cp = cache[2+type];
156         if ( cp == null) {
157             if (type == 0) {
158                 cp = ClassPathFactory.createClassPath(
159                 new ProjectClassPathImplementation(helper, "${javac.classpath}:${" //NOI18N
160
+ AppClientProjectProperties.J2EE_PLATFORM_CLASSPATH
161                         + "}", evaluator, false)); //NOI18N
162
}
163             else {
164                 cp = ClassPathFactory.createClassPath(
165                 new ProjectClassPathImplementation(helper, "${javac.test.classpath}:${" // NOI18N
166
+ AppClientProjectProperties.J2EE_PLATFORM_CLASSPATH
167                         + "}", evaluator, false)); // NOI18N
168
}
169             cache[2+type] = cp;
170         }
171         return cp;
172     }
173     
174     private synchronized ClassPath getRunTimeClasspath(FileObject file) {
175         int type = getType(file);
176         if (type < 0 || type > 4) {
177             // Unregistered file, or in a JAR.
178
// For jar:file:$projdir/dist/*.jar!/**/*.class, it is misleading to use
179
// run.classpath since that does not actually contain the file!
180
// (It contains file:$projdir/build/classes/ instead.)
181
return null;
182         } else if (type > 1) {
183             type-=2; //Compiled source transform into source
184
}
185         ClassPath cp = cache[4+type];
186         if ( cp == null) {
187             if (type == 0) {
188                 // XXX: should return run.classpath, but since there's no run classpath,
189
// in and EJB project, using debug.classpath instead
190
//TODO: appclient can have run.classpath
191
cp = ClassPathFactory.createClassPath(
192                     ProjectClassPathSupport.createPropertyBasedClassPathImplementation(
193                     projectDirectory, evaluator, new String JavaDoc[] {"run.classpath"})); // NOI18N
194
}
195             else if (type == 1) {
196                 cp = ClassPathFactory.createClassPath(
197                     ProjectClassPathSupport.createPropertyBasedClassPathImplementation(
198                     projectDirectory, evaluator, new String JavaDoc[] {"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 JavaDoc[] {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 synchronized 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 synchronized 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.COMPILE)) {
247             return getCompileTimeClasspath(file);
248         } else if (type.equals(ClassPath.EXECUTE)) {
249             return getRunTimeClasspath(file);
250         } else if (type.equals(ClassPath.SOURCE)) {
251             return getSourcepath(file);
252         } else if (type.equals(ClassPath.BOOT)) {
253             return getBootClassPath();
254         } else {
255             return null;
256         }
257     }
258     
259     /**
260      * Returns array of all classpaths of the given type in the project.
261      * The result is used for example for GlobalPathRegistry registrations.
262      */

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

287     public ClassPath getProjectSourcesClassPath(String JavaDoc type) {
288         if (ClassPath.SOURCE.equals(type)) {
289             return getSourcepath(0);
290         }
291         if (ClassPath.COMPILE.equals(type)) {
292             return getCompileTimeClasspath(0);
293         }
294         assert false;
295         return null;
296     }
297
298     public synchronized void propertyChange(PropertyChangeEvent JavaDoc evt) {
299         dirCache.remove(evt.getPropertyName());
300     }
301     
302 }
303
304
Popular Tags