KickJava   Java API By Example, From Geeks To Geeks.

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

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

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

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