KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > classpath > ProjectClassPathImplementation


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.web.project.classpath;
20
21 import java.beans.PropertyChangeEvent JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.beans.PropertyChangeSupport JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.io.File JavaDoc;
29 import java.net.URL JavaDoc;
30 import org.netbeans.spi.java.classpath.ClassPathImplementation;
31 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
32 import org.netbeans.spi.project.support.ant.AntProjectHelper;
33 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
34 import org.netbeans.spi.project.support.ant.PropertyUtils;
35 import org.openide.filesystems.FileUtil;
36 import org.openide.util.WeakListeners;
37
38 /**
39  * Implementation of a single classpath that is derived from one Ant property.
40  */

41 final class ProjectClassPathImplementation implements ClassPathImplementation, PropertyChangeListener JavaDoc {
42
43     private PropertyChangeSupport JavaDoc support = new PropertyChangeSupport JavaDoc(this);
44     private AntProjectHelper helper;
45     private String JavaDoc expression;
46     private boolean isProperty;
47     private String JavaDoc resolved;
48     private List JavaDoc resources;
49     private final PropertyEvaluator evaluator;
50
51     /**
52      * Construct the implementation.
53      * @param helper an Ant project, used to resolve file paths
54      * @param propertyName the name of an Ant property or an expression which will supply the classpath
55      * @param evaluator a property evaluator used to find the value of the classpath
56      */

57     public ProjectClassPathImplementation(AntProjectHelper helper, String JavaDoc expression, PropertyEvaluator evaluator, boolean isProperty) {
58         assert helper != null && expression != null;
59         this.helper = helper;
60         this.evaluator = evaluator;
61         this.expression = expression;
62         this.isProperty = isProperty;
63         evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator));
64         if (!isProperty) {
65             resolved = evaluator.evaluate (expression);
66         }
67     }
68         
69     /**
70      * Construct the implementation.
71      * @param helper an Ant project, used to resolve file paths
72      * @param propertyName the name of an Ant property which will supply the classpath
73      * @param evaluator a property evaluator used to find the value of the classpath
74      */

75     public ProjectClassPathImplementation(AntProjectHelper helper, String JavaDoc propertyName, PropertyEvaluator evaluator) {
76         this (helper, propertyName, evaluator, true);
77     }
78
79     public synchronized List JavaDoc /*<PathResourceImplementation>*/ getResources() {
80         if (this.resources == null) {
81             this.resources = this.getPath ();
82         }
83         return this.resources;
84     }
85
86     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
87         support.addPropertyChangeListener (listener);
88     }
89
90     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
91         support.removePropertyChangeListener (listener);
92     }
93
94
95     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
96         if (isProperty && !evt.getPropertyName().equals(expression)) {
97             // Not interesting to us.
98
return;
99         }
100         if (!isProperty) {
101             String JavaDoc eval = evaluator.evaluate (expression);
102             if (eval.equals(resolved)) {
103                 return;
104             } else {
105                 resolved = eval;
106             }
107         }
108         
109         List JavaDoc newRoots = getPath ();
110         boolean fire = false;
111         synchronized (this) {
112             if (this.resources != null && !this.resources.equals(newRoots)) {
113                 this.resources = newRoots;
114                 fire = true;
115             }
116         }
117         if (fire) {
118             support.firePropertyChange (PROP_RESOURCES,null,null);
119         }
120     }
121     
122     private List JavaDoc getPath() {
123         List JavaDoc result = new ArrayList JavaDoc ();
124         String JavaDoc prop = isProperty ? evaluator.getProperty(expression) : resolved;
125         if (prop != null) {
126             String JavaDoc[] pieces = PropertyUtils.tokenizePath(prop);
127             for (int i = 0; i < pieces.length; i++) {
128                 File JavaDoc f = helper.resolveFile(pieces[i]);
129                 try {
130                     URL JavaDoc entry = f.toURI().toURL();
131                     if (FileUtil.isArchiveFile(entry)) {
132                         entry = FileUtil.getArchiveRoot(entry);
133                     } else if (!f.exists()) {
134                         // if file does not exist (e.g. build/classes folder
135
// was not created yet) then corresponding File will
136
// not be ended with slash. Fix that.
137
assert !entry.toExternalForm().endsWith("/") : f; // NOI18N
138
entry = new URL JavaDoc(entry.toExternalForm() + "/"); // NOI18N
139
}
140                     result.add(ClassPathSupport.createResource(entry));
141                 } catch (MalformedURLException JavaDoc mue) {
142                     assert false : mue;
143                 }
144             }
145         }
146         return Collections.unmodifiableList(result);
147     }
148
149 }
150
Popular Tags