Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2008 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* If you wish your version of this file to be governed by only the CDDL |
25 |
* or only the GPL Version 2, indicate your decision by adding |
26 |
* "[Contributor] elects to include this software in this distribution |
27 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
28 |
* single choice of license, a recipient has the option to distribute |
29 |
* your version of this file under either the CDDL, the GPL Version 2 or |
30 |
* to extend the choice of license to its licensees as provided above. |
31 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
32 |
* Version 2 license, then the option applies only if the new code is |
33 |
* made subject to such option by the copyright holder. |
34 |
* |
35 |
* Contributor(s): |
36 |
* |
37 |
* Portions Copyrighted 2008 Sun Microsystems, Inc. |
38 |
*/ |
39 |
package org.netbeans.spi.java.project.support; |
40 |
|
41 |
import java.beans.PropertyChangeEvent; |
42 |
import java.beans.PropertyChangeListener; |
43 |
import java.lang.reflect.Field; |
44 |
import java.util.ArrayList; |
45 |
import java.util.Collections; |
46 |
import java.util.HashMap; |
47 |
import java.util.List; |
48 |
import java.util.Map; |
49 |
import org.netbeans.api.java.classpath.ClassPath; |
50 |
import org.netbeans.spi.java.classpath.ClassPathFactory; |
51 |
import org.netbeans.spi.java.classpath.ClassPathImplementation; |
52 |
import org.netbeans.spi.java.classpath.ClassPathProvider; |
53 |
import org.netbeans.spi.java.classpath.PathResourceImplementation; |
54 |
import org.netbeans.spi.project.LookupMerger; |
55 |
import org.openide.filesystems.FileObject; |
56 |
import org.openide.util.Exceptions; |
57 |
import org.openide.util.Lookup; |
58 |
import org.openide.util.LookupEvent; |
59 |
import org.openide.util.LookupListener; |
60 |
import org.openide.util.WeakListeners; |
61 |
|
62 |
/** |
63 |
* Lookup Merger implementation for ClassPathProvider |
64 |
* |
65 |
* @author Tomas Zezula, Milos Kleint |
66 |
*/ |
67 |
final class ClassPathProviderMerger implements LookupMerger<ClassPathProvider> { |
68 |
|
69 |
private final ClassPathProvider defaultProvider; |
70 |
|
71 |
ClassPathProviderMerger(final ClassPathProvider defaultProvider) { |
72 |
assert defaultProvider != null; |
73 |
this.defaultProvider = defaultProvider; |
74 |
} |
75 |
|
76 |
public Class<ClassPathProvider> getMergeableClass() { |
77 |
return ClassPathProvider.class; |
78 |
} |
79 |
|
80 |
public ClassPathProvider merge(Lookup lookup) { |
81 |
return new CPProvider(lookup); |
82 |
} |
83 |
|
84 |
private class CPProvider implements ClassPathProvider { |
85 |
|
86 |
private final Lookup lookup; |
87 |
private final Map<FileObject, Map<String, ClassPath>> cpCache = new HashMap<FileObject, Map<String, ClassPath>>(); |
88 |
|
89 |
public CPProvider(final Lookup lookup) { |
90 |
assert lookup != null; |
91 |
this.lookup = lookup; |
92 |
} |
93 |
|
94 |
public ClassPath findClassPath(FileObject file, String type) { |
95 |
synchronized (cpCache) { |
96 |
Map<String, ClassPath> cptype = cpCache.get(file); |
97 |
if (cptype != null) { |
98 |
ClassPath path = cptype.get(type); |
99 |
if (path != null) { |
100 |
return path; |
101 |
} |
102 |
} |
103 |
} |
104 |
ProxyClassPathImplementation result = new ProxyClassPathImplementation(defaultProvider, lookup, file, type); |
105 |
ClassPath cp = ClassPathFactory.createClassPath(result); |
106 |
synchronized (cpCache) { |
107 |
Map<String, ClassPath> cptype = cpCache.get(file); |
108 |
if (cptype == null) { |
109 |
cptype = new HashMap<String, ClassPath>(); |
110 |
cpCache.put(file, cptype); |
111 |
} |
112 |
cptype.put(type, cp); |
113 |
} |
114 |
return cp; |
115 |
} |
116 |
} |
117 |
|
118 |
/** ProxyClassPathImplementation provides read only proxy for ClassPathImplementations. |
119 |
* The order of the resources is given by the order of its delegates. |
120 |
* The proxy is designed to be used as a union of class paths. |
121 |
* E.g. to be able to easily iterate or listen on all design resources = sources + compile resources |
122 |
*/ |
123 |
public class ProxyClassPathImplementation implements ClassPathImplementation { |
124 |
|
125 |
private ClassPathImplementation[] classPaths; |
126 |
private List<PathResourceImplementation> resourcesCache; |
127 |
private ArrayList<PropertyChangeListener> listeners; |
128 |
private LookupListener lookupList; |
129 |
private Lookup.Result<ClassPathProvider> providers; |
130 |
private ClassPathProvider mainProvider; |
131 |
private PropertyChangeListener classPathsListener; |
132 |
private FileObject file; |
133 |
private String type; |
134 |
|
135 |
public ProxyClassPathImplementation(ClassPathProvider dominant, Lookup context, FileObject fo, String type) { |
136 |
assert dominant != null; |
137 |
this.type = type; |
138 |
this.file = fo; |
139 |
mainProvider = dominant; |
140 |
providers = context.lookupResult(ClassPathProvider.class); |
141 |
classPathsListener = new DelegatesListener(); |
142 |
|
143 |
checkProviders(); |
144 |
lookupList = new LookupListener() { |
145 |
public void resultChanged(LookupEvent ev) { |
146 |
checkProviders(); |
147 |
} |
148 |
}; |
149 |
providers.addLookupListener(lookupList); |
150 |
|
151 |
} |
152 |
|
153 |
private void checkProviders() { |
154 |
List<ClassPathImplementation> impls = new ArrayList<ClassPathImplementation>(); |
155 |
ClassPath mainResult = mainProvider.findClassPath(file, type); |
156 |
if (mainResult != null) { |
157 |
impls.add(getClassPathImplementation(mainResult)); |
158 |
} |
159 |
|
160 |
for (ClassPathProvider prvd : providers.allInstances()) { |
161 |
ClassPath path = prvd.findClassPath(file, type); |
162 |
if (path != null) { |
163 |
impls.add(getClassPathImplementation(path)); |
164 |
} |
165 |
} |
166 |
for (ClassPathImplementation cpImpl : impls) { |
167 |
if (cpImpl == null) { |
168 |
continue; |
169 |
} |
170 |
cpImpl.addPropertyChangeListener(WeakListeners.propertyChange(classPathsListener, cpImpl)); |
171 |
} |
172 |
synchronized (this) { |
173 |
if (classPaths != null) { |
174 |
//TODO how to remove weak listeners |
175 |
} |
176 |
this.classPaths = impls.toArray(new ClassPathImplementation[impls.size()]); |
177 |
} |
178 |
PropertyChangeEvent ev = new PropertyChangeEvent(this, ClassPathImplementation.PROP_RESOURCES, null, null); |
179 |
firePropertyChange(ev); |
180 |
} |
181 |
|
182 |
public List<? extends PathResourceImplementation> getResources() { |
183 |
synchronized (this) { |
184 |
if (this.resourcesCache != null) { |
185 |
return this.resourcesCache; |
186 |
} |
187 |
} |
188 |
|
189 |
ArrayList<PathResourceImplementation> result = new ArrayList<PathResourceImplementation>(classPaths.length * 10); |
190 |
for (ClassPathImplementation cpImpl : classPaths) { |
191 |
List<? extends PathResourceImplementation> subPath = cpImpl.getResources(); |
192 |
assert subPath != null : "ClassPathImplementation.getResources() returned null. ClassPathImplementation.class: " + cpImpl.getClass().toString() + " ClassPathImplementation: " + cpImpl.toString(); |
193 |
result.addAll(subPath); |
194 |
} |
195 |
|
196 |
synchronized (this) { |
197 |
if (this.resourcesCache == null) { |
198 |
resourcesCache = Collections.unmodifiableList(result); |
199 |
} |
200 |
return this.resourcesCache; |
201 |
} |
202 |
} |
203 |
|
204 |
public synchronized void addPropertyChangeListener(PropertyChangeListener listener) { |
205 |
if (this.listeners == null) { |
206 |
this.listeners = new ArrayList<PropertyChangeListener>(); |
207 |
} |
208 |
this.listeners.add(listener); |
209 |
} |
210 |
|
211 |
public synchronized void removePropertyChangeListener(PropertyChangeListener listener) { |
212 |
if (this.listeners == null) { |
213 |
return; |
214 |
} |
215 |
this.listeners.remove(listener); |
216 |
} |
217 |
|
218 |
@Override |
219 |
public String toString() { |
220 |
StringBuilder builder = new StringBuilder("["); //NOI18N |
221 |
|
222 |
for (ClassPathImplementation cpImpl : this.classPaths) { |
223 |
builder.append(cpImpl.toString()); |
224 |
builder.append(", "); //NOI18N |
225 |
|
226 |
} |
227 |
builder.append("]"); //NOI18N |
228 |
|
229 |
return builder.toString(); |
230 |
} |
231 |
|
232 |
private void firePropertyChange(PropertyChangeEvent event) { |
233 |
PropertyChangeListener[] _listeners; |
234 |
synchronized (this) { |
235 |
resourcesCache = null; //Clean the cache |
236 |
if (listeners == null) { |
237 |
return; |
238 |
} |
239 |
_listeners = listeners.toArray(new PropertyChangeListener[ProxyClassPathImplementation.this.listeners.size()]); |
240 |
} |
241 |
for (PropertyChangeListener l : _listeners) { |
242 |
l.propertyChange(event); |
243 |
} |
244 |
} |
245 |
|
246 |
private class DelegatesListener implements PropertyChangeListener { |
247 |
|
248 |
public void propertyChange(PropertyChangeEvent evt) { |
249 |
PropertyChangeEvent event = new PropertyChangeEvent(ProxyClassPathImplementation.this, evt.getPropertyName(), null, null); |
250 |
firePropertyChange(event); |
251 |
} |
252 |
} |
253 |
} |
254 |
|
255 |
static Field implField; |
256 |
static { |
257 |
try { |
258 |
implField = ClassPath.class.getDeclaredField("impl"); |
259 |
implField.setAccessible(true); |
260 |
} catch (Exception x) { |
261 |
Exceptions.printStackTrace(x); |
262 |
} |
263 |
} |
264 |
/** |
265 |
* sort of hack. JavaSupport APIs are meant to stay project free (thus the LookupMerger cannot be placed there). |
266 |
* But the Java project Support module has no access to the ClassPathAccessor.DEFAULT instance |
267 |
* to be able to get the ClassPathImplementation out of the ClassPath instance. |
268 |
* (as the ProxyClassPathImplemntation in Java Support API module does). |
269 |
*/ |
270 |
static ClassPathImplementation getClassPathImplementation(ClassPath path) { |
271 |
assert implField != null : "ClassPath.impl field is gone."; |
272 |
Object toRet = null; |
273 |
try { |
274 |
toRet = implField.get(path); |
275 |
} catch (IllegalArgumentException ex) { |
276 |
Exceptions.printStackTrace(ex); |
277 |
} catch (IllegalAccessException ex) { |
278 |
Exceptions.printStackTrace(ex); |
279 |
} |
280 |
return (ClassPathImplementation)toRet; |
281 |
} |
282 |
} |