KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > 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.ejbjarproject.classpath;
21
22 import java.io.File JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.HashMap JavaDoc;
25
26 import org.netbeans.api.java.classpath.ClassPath;
27 import org.netbeans.modules.j2ee.ejbjarproject.ui.customizer.EjbJarProjectProperties;
28 import org.netbeans.modules.j2ee.ejbjarproject.SourceRoots;
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.AntProjectListener;
34 import org.netbeans.spi.project.support.ant.AntProjectEvent;
35 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileUtil;
38
39 /**
40  * Defines the various class paths for a EJB project.
41  */

42 public final class ClassPathProviderImpl implements ClassPathProvider, AntProjectListener {
43     
44     private final AntProjectHelper helper;
45     private final File JavaDoc projectDirectory;
46     private final PropertyEvaluator evaluator;
47     private final SourceRoots sourceRoots;
48     private final SourceRoots testSourceRoots;
49     
50     /**
51      * Cache for classpaths:
52      * <dl>
53      * <dt>0</dt> <dd>sources classpath</dd>
54      * <dt>1</dt> <dd>test sources classpath</dd>
55      * <dt>2</dt> <dd>sources compile classpath</dd>
56      * <dt>3</dt> <dd>test sources compile classpath</dd>
57      * <dt>4</dt> <dd>sources and built sources run classpath</dd>
58      * <dt>5</dt> <dd>test sources and built test sources run classpath</dd>
59      * <dt>6</dt> <dd>XXX: todo</dd>
60      * <dt>7</dt> <dd>boot classpath</dd>
61      * <dt>8</dt> <dd>J2EE platform classpath</dd>
62      * </dl>
63      */

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

123     private int getType(FileObject file) {
124         FileObject[] srcPath = getPrimarySrcPath();
125         for (int i=0; i < srcPath.length; i++) {
126             FileObject root = srcPath[i];
127             if (root.equals(file) || FileUtil.isParentOf(root, file)) {
128                 return 0;
129             }
130         }
131         srcPath = getTestSrcDir();
132         for (int i=0; i< srcPath.length; i++) {
133             FileObject root = srcPath[i];
134             if (root.equals(file) || FileUtil.isParentOf(root, file)) {
135                 return 1;
136             }
137         }
138         FileObject dir = getBuildClassesDir();
139         if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir, file))) {
140             return 2;
141         }
142         dir = getBuildJar();
143         if (dir != null && (dir.equals(file))) { //TODO: When MasterFs check also isParentOf
144
return 4;
145         }
146         dir = getBuildTestClassesDir();
147         if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir,file))) {
148             return 3;
149         }
150         return -1;
151     }
152     
153     private ClassPath getCompileTimeClasspath(FileObject file) {
154         int type = getType(file);
155         return this.getCompileTimeClasspath(type);
156     }
157     
158     private synchronized ClassPath getCompileTimeClasspath(int type) {
159         if (type < 0 || type > 1) {
160             // Not a source file.
161
return null;
162         }
163         ClassPath cp = cache[2+type];
164         if ( cp == null) {
165             if (type == 0) {
166                 cp = ClassPathFactory.createClassPath(
167                 new ProjectClassPathImplementation(helper, "${javac.classpath}:${" //NOI18N
168
+ EjbJarProjectProperties.J2EE_PLATFORM_CLASSPATH
169                         + "}", evaluator, false)); //NOI18N
170
}
171             else {
172                 cp = ClassPathFactory.createClassPath(
173                 new ProjectClassPathImplementation(helper, "${javac.test.classpath}:${" // NOI18N
174
+ EjbJarProjectProperties.J2EE_PLATFORM_CLASSPATH
175                         + "}", evaluator, false)); // NOI18N
176
}
177             cache[2+type] = cp;
178         }
179         return cp;
180     }
181     
182     private synchronized ClassPath getRunTimeClasspath(FileObject file) {
183         int type = getType(file);
184         if (type < 0 || type > 4) {
185             return null;
186         } else if (type > 1) {
187             type -= 2;
188         }
189         
190         ClassPath cp = cache[4+type];
191         if (cp == null) {
192             if (type == 0) {
193                 // XXX: should return run.classpath, but since there's no run classpath,
194
// in and EJB project, using debug.classpath instead
195
cp = ClassPathFactory.createClassPath(
196                     ProjectClassPathSupport.createPropertyBasedClassPathImplementation(
197                     projectDirectory, evaluator, new String JavaDoc[] {"debug.classpath"})); // NOI18N
198
}
199             else if (type == 1) {
200                 cp = ClassPathFactory.createClassPath(
201                     ProjectClassPathSupport.createPropertyBasedClassPathImplementation(
202                     projectDirectory, evaluator, new String JavaDoc[] {"run.test.classpath"})); // NOI18N
203
}
204             else if (type == 2) {
205                 //Only to make the CompiledDataNode hapy
206
//Todo: Strictly it should return ${run.classpath} - ${build.classes.dir} + ${dist.jar}
207
cp = ClassPathFactory.createClassPath(
208                     ProjectClassPathSupport.createPropertyBasedClassPathImplementation(
209                     projectDirectory, evaluator, new String JavaDoc[] {"dist.jar"})); // NOI18N
210
}
211             
212             cache[4+type] = cp;
213         }
214         return cp;
215     }
216     
217     private ClassPath getSourcepath(FileObject file) {
218         int type = getType(file);
219         return this.getSourcepath(type);
220     }
221     
222     private synchronized ClassPath getSourcepath(int type) {
223         if (type < 0 || type > 1) {
224             return null;
225         }
226         ClassPath cp = cache[type];
227         if (cp == null) {
228             switch (type) {
229                 case 0:
230                     cp = ClassPathFactory.createClassPath(new SourcePathImplementation (this.sourceRoots,this.helper));
231                     break;
232                 case 1:
233                     cp = ClassPathFactory.createClassPath(new SourcePathImplementation (this.testSourceRoots,this.helper));
234                     break;
235             }
236         }
237         cache[type] = cp;
238         return cp;
239     }
240
241     private synchronized ClassPath getBootClassPath() {
242         ClassPath cp = cache[7];
243         if (cp == null) {
244             cp = ClassPathFactory.createClassPath(new BootClassPathImplementation(evaluator));
245             cache[7] = cp;
246         }
247         return cp;
248     }
249     
250     public synchronized ClassPath getJ2eePlatformClassPath() {
251         ClassPath cp = cache[8];
252         if (cp == null) {
253                 cp = ClassPathFactory.createClassPath(
254                 new ProjectClassPathImplementation(helper, "${" + //NOI18N
255
EjbJarProjectProperties.J2EE_PLATFORM_CLASSPATH +
256                         "}", evaluator, false)); //NOI18N
257
cache[8] = cp;
258         }
259         return cp;
260     }
261     
262     public ClassPath findClassPath(FileObject file, String JavaDoc type) {
263         if (type.equals(ClassPath.COMPILE)) {
264             return getCompileTimeClasspath(file);
265         } else if (type.equals(ClassPath.EXECUTE)) {
266             return getRunTimeClasspath(file);
267         } else if (type.equals(ClassPath.SOURCE)) {
268             return getSourcepath(file);
269         } else if (type.equals(ClassPath.BOOT)) {
270             return getBootClassPath();
271         } else {
272             return null;
273         }
274     }
275     
276     /**
277      * Returns array of all classpaths of the given type in the project.
278      * The result is used for example for GlobalPathRegistry registrations.
279      */

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

304     public ClassPath getProjectSourcesClassPath(String JavaDoc type) {
305         if (ClassPath.SOURCE.equals(type)) {
306             return getSourcepath(0);
307         }
308         if (ClassPath.COMPILE.equals(type)) {
309             return getCompileTimeClasspath(0);
310         }
311         assert false;
312         return null;
313     }
314
315     public void configurationXmlChanged(AntProjectEvent ev) {
316         this.dirCache.clear();
317     }
318
319     public synchronized void propertiesChanged(AntProjectEvent ev) {
320         this.dirCache.clear();
321     }
322
323 }
324
325
Popular Tags