KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > earproject > 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.earproject.classpath;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.lang.ref.Reference JavaDoc;
25 import java.lang.ref.SoftReference JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
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.project.support.ant.AntProjectHelper;
32 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
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 an Enterprise Application project.
39  */

40 public final class ClassPathProviderImpl implements ClassPathProvider, PropertyChangeListener JavaDoc {
41     
42     private static final int TYPE_NORMAL = 0;
43     private static final int TYPE_BUILT_UNPACKED = 2;
44     private static final int TYPE_BUILT_JAR = 3;
45     private static final int TYPE_OTHER = -1;
46     
47     private static final String JavaDoc BUILD_CLASSES_DIR = "build.classes.dir"; // NOI18N
48
private static final String JavaDoc DIST_JAR = "dist.jar"; // NOI18N
49
private static final String JavaDoc DOC_BASE_DIR = "web.docbase.dir"; // NOI18N
50

51     private final AntProjectHelper helper;
52     private final PropertyEvaluator evaluator;
53     @SuppressWarnings JavaDoc("unchecked")
54     private final Reference JavaDoc<ClassPath>[] cache = new SoftReference JavaDoc[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) {
59         this.helper = helper;
60         this.evaluator = evaluator;
61         evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator));
62     }
63     
64     private synchronized FileObject getDir(String JavaDoc propname) {
65         FileObject fo = this.dirCache.get(propname);
66         if (fo == null || !fo.isValid()) {
67             String JavaDoc prop = evaluator.getProperty(propname);
68             if (prop != null) {
69                 fo = helper.resolveFileObject(prop);
70                 this.dirCache.put(propname, fo);
71             }
72         }
73         return fo;
74     }
75     
76     private FileObject getBuildClassesDir() {
77         return getDir(BUILD_CLASSES_DIR);
78     }
79     
80     private FileObject getDistJar() {
81         return getDir(DIST_JAR);
82     }
83     
84     private FileObject getDocumentBaseDir() {
85         return getDir(DOC_BASE_DIR);
86     }
87     
88     /**
89      * Find what a given file represents.
90      * @param file a file in the project
91      * @return one of: <dl>
92      * <dt>0</dt> <dd>normal source</dd>
93      * <dt>2</dt> <dd>built class (unpacked)</dd>
94      * <dt>3</dt> <dd>built class (in dist JAR)</dd>
95      * <dt>-1</dt> <dd>something else</dd>
96      * </dl>
97      */

98     private int getType(FileObject file) {
99         FileObject dir = getDocumentBaseDir();
100         if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir,file))) {
101             return TYPE_BUILT_UNPACKED;
102         }
103         dir = getBuildClassesDir();
104         if (dir != null && (dir.equals(file) || FileUtil.isParentOf(dir, file))) {
105             return TYPE_BUILT_JAR;
106         }
107         dir = getDistJar(); // not really a dir at all, of course
108
if (dir != null && dir.equals(FileUtil.getArchiveFile(file))) {
109             // XXX check whether this is really the root
110
return TYPE_BUILT_JAR;
111         }
112         return TYPE_OTHER;
113     }
114     
115     private ClassPath getCompileTimeClasspath(FileObject file) {
116         int type = getType(file);
117         return this.getCompileTimeClasspath(type);
118     }
119     
120     private synchronized ClassPath getCompileTimeClasspath(int type) {
121         if (type < TYPE_NORMAL || type > TYPE_BUILT_UNPACKED) {
122             // Not a source file.
123
return null;
124         }
125         if (type == TYPE_BUILT_UNPACKED) type = TYPE_NORMAL;
126         ClassPath cp = null;
127         if (cache[TYPE_BUILT_JAR + type] == null || (cp = cache[TYPE_BUILT_JAR + type].get()) == null) {
128             if (type == TYPE_NORMAL) {
129                 cp = ClassPathFactory.createClassPath(
130                         new ProjectClassPathImplementation(helper, "${javac.classpath}:${build.classes.dir}", evaluator, false)); //NOI18N
131
}
132             cache[TYPE_BUILT_JAR + type] = new SoftReference JavaDoc<ClassPath>(cp);
133         }
134         return cp;
135     }
136     
137     private synchronized ClassPath getRunTimeClasspath(FileObject file) {
138         int type = getType(file);
139         if (type < TYPE_NORMAL || type > 4) {
140             // Unregistered file, or in a JAR.
141
// For jar:file:$projdir/dist/*.jar!/**/*.class, it is misleading to use
142
// run.classpath since that does not actually contain the file!
143
// (It contains file:$projdir/build/classes/ instead.)
144
return null;
145         }
146         switch (type){
147             case TYPE_BUILT_UNPACKED: type = TYPE_NORMAL; break;
148             case TYPE_BUILT_JAR:
149             case 4: type -=3; break;
150         }
151         
152         ClassPath cp = null;
153         if (cache[6+type] == null || (cp = cache[6+type].get())== null) {
154             if (type == TYPE_NORMAL) {
155                 //XXX : It should return a classpath for run.classpath property, but
156
// the run.classpath property was removed from the webproject in the past
157
// and I'm a little lazy to return it back in the code:)). In this moment
158
// the run classpath equals to the debug classpath. If the debug classpath
159
// will be different from the run classpath, then the run classpath should
160
// be returned back.
161
cp = ClassPathFactory.createClassPath(
162                         new ProjectClassPathImplementation(helper, "debug.classpath", evaluator)); // NOI18N
163
}
164             cache[6+type] = new SoftReference JavaDoc<ClassPath>(cp);
165         }
166         return cp;
167     }
168     
169     private synchronized ClassPath getBootClassPath() {
170         ClassPath cp = null;
171         if (cache[7] == null || (cp = cache[7].get()) == null) {
172             cp = ClassPathFactory.createClassPath(new BootClassPathImplementation(evaluator));
173             cache[7] = new SoftReference JavaDoc<ClassPath>(cp);
174         }
175         return cp;
176     }
177     
178     public ClassPath findClassPath(FileObject file, String JavaDoc type) {
179         if (type.equals(ClassPath.COMPILE)) {
180             return getCompileTimeClasspath(file);
181         } else if (type.equals(ClassPath.EXECUTE)) {
182             return getRunTimeClasspath(file);
183         } else if (type.equals(ClassPath.BOOT)) {
184             return getBootClassPath();
185         } else {
186             return null;
187         }
188     }
189     
190     /**
191      * Returns array of all classpaths of the given type in the project.
192      * The result is used for example for GlobalPathRegistry registrations.
193      */

194     public ClassPath[] getProjectClassPaths(String JavaDoc type) {
195         if (ClassPath.BOOT.equals(type)) {
196             return new ClassPath[]{getBootClassPath()};
197         }
198         if (ClassPath.COMPILE.equals(type)) {
199             ClassPath[] l = new ClassPath[1];
200             l[0] = getCompileTimeClasspath(TYPE_NORMAL);
201             return l;
202         }
203         assert false;
204         return null;
205     }
206     
207     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
208         dirCache.remove(evt.getPropertyName());
209     }
210     
211 }
212
213
Popular Tags