KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > 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.clientproject.classpath;
20
21 import java.beans.PropertyChangeEvent JavaDoc;
22 import org.netbeans.spi.java.classpath.ClassPathImplementation;
23 import org.netbeans.spi.java.classpath.PathResourceImplementation;
24 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
25 import org.netbeans.spi.project.support.ant.AntProjectHelper;
26 import org.openide.filesystems.FileUtil;
27
28 import java.util.List JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.beans.PropertyChangeListener JavaDoc;
32 import java.beans.PropertyChangeSupport JavaDoc;
33 import java.net.MalformedURLException JavaDoc;
34 import java.io.File JavaDoc;
35 import java.net.URL JavaDoc;
36 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
37
38 import org.netbeans.spi.project.support.ant.PropertyUtils;
39 import org.openide.util.WeakListeners;
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 String JavaDoc resolved;
47     private List JavaDoc<PathResourceImplementation> resources;
48     private boolean isProperty;
49     private final PropertyEvaluator evaluator;
50
51     public ProjectClassPathImplementation (AntProjectHelper helper, String JavaDoc expression, PropertyEvaluator evaluator, boolean isProperty) {
52         assert helper != null && expression != null;
53         this.helper = helper;
54         this.evaluator = evaluator;
55         this.expression = expression;
56         this.isProperty = isProperty;
57         evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator));
58         if (!isProperty) {
59             resolved = evaluator.evaluate (expression);
60         }
61     }
62     
63     public ProjectClassPathImplementation (AntProjectHelper helper, String JavaDoc property, PropertyEvaluator evaluator) {
64         this(helper, property, evaluator, true);
65     }
66
67     public synchronized List JavaDoc<PathResourceImplementation> getResources() {
68         if (this.resources == null) {
69             this.resources = this.getPath ();
70         }
71         return this.resources;
72     }
73
74     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
75         support.addPropertyChangeListener (listener);
76     }
77
78     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
79         support.removePropertyChangeListener (listener);
80     }
81
82     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
83         if (isProperty && !evt.getPropertyName().equals(expression)) {
84             // Not interesting to us.
85
return;
86         }
87         if (!isProperty) {
88             String JavaDoc eval = evaluator.evaluate (expression);
89             if (eval == resolved) {
90                 return;
91             } else {
92                 resolved = eval;
93             }
94         }
95         
96         List JavaDoc<PathResourceImplementation> newRoots = getPath();
97         boolean fire = false;
98         synchronized (this) {
99             if (this.resources != null && !this.resources.equals(newRoots)) {
100                 this.resources = newRoots;
101                 fire = true;
102             }
103         }
104         if (fire) {
105             support.firePropertyChange (PROP_RESOURCES,null,null);
106         }
107     }
108
109     private List JavaDoc<PathResourceImplementation> getPath() {
110         List JavaDoc<PathResourceImplementation> result = new ArrayList JavaDoc<PathResourceImplementation>();
111         String JavaDoc prop = isProperty ? evaluator.getProperty(expression) : resolved;
112         if (prop != null) {
113             String JavaDoc[] pieces = PropertyUtils.tokenizePath(prop);
114             for (int i = 0; i < pieces.length; i++) {
115                 File JavaDoc f = helper.resolveFile(pieces[i]);
116                 try {
117                     URL JavaDoc entry = f.toURI().toURL();
118                     if (FileUtil.isArchiveFile(entry)) {
119                         entry = FileUtil.getArchiveRoot(entry);
120                     } else if (!f.exists()) {
121                         // if file does not exist (e.g. build/classes folder
122
// was not created yet) then corresponding File will
123
// not be ended with slash. Fix that.
124
assert !entry.toExternalForm().endsWith("/") : f; // NOI18N
125
entry = new URL JavaDoc(entry.toExternalForm() + "/"); // NOI18N
126
}
127                     result.add(ClassPathSupport.createResource(entry));
128                 } catch (MalformedURLException JavaDoc mue) {
129                     assert false : mue;
130                 }
131             }
132         }
133         return Collections.unmodifiableList(result);
134     }
135 }
136
Popular Tags