KickJava   Java API By Example, From Geeks To Geeks.

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

46 final class ProjectClassPathImplementation implements ClassPathImplementation, PropertyChangeListener JavaDoc, Runnable JavaDoc {
47     
48     private final PropertyChangeSupport JavaDoc support = new PropertyChangeSupport JavaDoc(this);
49     private final File JavaDoc projectFolder;
50     private List JavaDoc<PathResourceImplementation> resources;
51     private final PropertyEvaluator evaluator;
52     private boolean dirty = false;
53     private final List JavaDoc<String JavaDoc> propertyNames;
54
55     /**
56      * Construct the implementation.
57      * @param projectFolder the folder containing the project, used to resolve relative paths
58      * @param propertyNames the names of an Ant properties which will supply the classpath
59      * @param evaluator a property evaluator used to find the value of the classpath
60      */

61     public ProjectClassPathImplementation(File JavaDoc projectFolder, String JavaDoc[] propertyNames, PropertyEvaluator evaluator) {
62         assert projectFolder != null && propertyNames != null && evaluator != null;
63         this.projectFolder = projectFolder;
64         this.evaluator = evaluator;
65         this.propertyNames = Arrays.asList(propertyNames);
66         evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator));
67     }
68
69     public synchronized List JavaDoc<PathResourceImplementation> getResources() {
70         if (this.resources == null) {
71             this.resources = this.getPath ();
72         }
73         return this.resources;
74     }
75
76     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
77         support.addPropertyChangeListener (listener);
78     }
79
80     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
81         support.removePropertyChangeListener (listener);
82     }
83
84
85     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
86         String JavaDoc prop = evt.getPropertyName();
87         if (prop != null && !propertyNames.contains(evt.getPropertyName())) {
88             // Not interesting to us.
89
return;
90         }
91         // Coalesce changes; can come in fast after huge CP changes (#47910):
92
// XXX any synch needed on dirty flag?
93
if (!dirty) {
94             dirty = true;
95             ProjectManager.mutex().postReadRequest(this);
96         }
97     }
98     
99     public void run() {
100         dirty = false;
101         List JavaDoc<PathResourceImplementation> newRoots = getPath();
102         boolean fire = false;
103         synchronized (this) {
104             if (this.resources != null && !this.resources.equals(newRoots)) {
105                 this.resources = newRoots;
106                 fire = true;
107             }
108         }
109         if (fire) {
110             support.firePropertyChange (PROP_RESOURCES,null,null);
111         }
112     }
113     
114     private List JavaDoc<PathResourceImplementation> getPath() {
115         List JavaDoc<PathResourceImplementation> result = new ArrayList JavaDoc<PathResourceImplementation>();
116         for (String JavaDoc p : propertyNames) {
117             String JavaDoc prop = evaluator.getProperty(p);
118             if (prop != null) {
119                 for (String JavaDoc piece : PropertyUtils.tokenizePath(prop)) {
120                     File JavaDoc f = PropertyUtils.resolveFile(this.projectFolder, piece);
121                     try {
122                         URL JavaDoc entry = f.toURI().toURL();
123                         if (FileUtil.isArchiveFile(entry) || (f.isFile() && f.length()<4)) { //XXX: Not yet closed archive file
124
entry = FileUtil.getArchiveRoot(entry);
125                         } else if (!f.exists()) {
126                             // if file does not exist (e.g. build/classes folder
127
// was not created yet) then corresponding File will
128
// not be ended with slash. Fix that.
129
assert !entry.toExternalForm().endsWith("/") : f; // NOI18N
130
entry = new URL JavaDoc(entry.toExternalForm() + "/"); // NOI18N
131
}
132                         else if (f.isFile()) {
133                             ErrorManager.getDefault().log(ErrorManager.ERROR,"ProjectClassPathImplementation: file: "+f.getAbsolutePath()
134                             +" is not a valid archive file."); //NOI18N
135
continue;
136                         }
137                         result.add(ClassPathSupport.createResource(entry));
138                     } catch (MalformedURLException JavaDoc mue) {
139                         assert false : mue;
140                     }
141                 }
142             }
143         }
144         return Collections.unmodifiableList(result);
145     }
146
147 }
148
Popular Tags