Lines 1-7
Link Here
|
1 |
/* |
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
3 |
* |
4 |
* Copyright 2008 Sun Microsystems, Inc. All rights reserved. |
4 |
* Copyright 2008-2010 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
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 |
7 |
* General Public License Version 2 only ("GPL") or the Common |
Lines 74-84
Link Here
|
74 |
public class MavenFileOwnerQueryImpl implements FileOwnerQueryImplementation { |
74 |
public class MavenFileOwnerQueryImpl implements FileOwnerQueryImplementation { |
75 |
|
75 |
|
76 |
private Set<NbMavenProjectImpl> set; |
76 |
private Set<NbMavenProjectImpl> set; |
77 |
private final Object lock = new Object(); |
77 |
private final Object setLock = new Object(); |
|
|
78 |
|
79 |
private Set<NbMavenProjectImpl> cachedProjects; |
80 |
private Set<NbMavenProjectImpl> projectsToAddToCache; |
78 |
private final Object cacheLock = new Object(); |
81 |
private final Object cacheLock = new Object(); |
|
|
82 |
|
83 |
private PropertyChangeListener projectListener; |
79 |
private final List<ChangeListener> listeners; |
84 |
private final List<ChangeListener> listeners; |
80 |
private Set<NbMavenProjectImpl> cachedProjects; |
85 |
|
81 |
private PropertyChangeListener projectListener; |
|
|
82 |
private static final Logger LOG = Logger.getLogger(MavenFileOwnerQueryImpl.class.getName()); |
86 |
private static final Logger LOG = Logger.getLogger(MavenFileOwnerQueryImpl.class.getName()); |
83 |
|
87 |
|
84 |
/** Creates a new instance of MavenFileBuiltQueryImpl */ |
88 |
/** Creates a new instance of MavenFileBuiltQueryImpl */ |
Lines 86-96
Link Here
|
86 |
set = new HashSet<NbMavenProjectImpl>(); |
90 |
set = new HashSet<NbMavenProjectImpl>(); |
87 |
listeners = new ArrayList<ChangeListener>(); |
91 |
listeners = new ArrayList<ChangeListener>(); |
88 |
cachedProjects = null; |
92 |
cachedProjects = null; |
|
|
93 |
projectsToAddToCache = null; |
89 |
projectListener = new PropertyChangeListener() { |
94 |
projectListener = new PropertyChangeListener() { |
90 |
public void propertyChange(PropertyChangeEvent evt) { |
95 |
public void propertyChange(PropertyChangeEvent evt) { |
91 |
if (NbMavenProjectImpl.PROP_PROJECT.equals(evt.getPropertyName())) { |
96 |
if (NbMavenProjectImpl.PROP_PROJECT.equals(evt.getPropertyName())) { |
92 |
synchronized (cacheLock) { |
97 |
Object evtSource = evt.getSource(); |
93 |
cachedProjects = null; |
98 |
if (evtSource instanceof NbMavenProjectImpl) { |
|
|
99 |
// try adding the changed project and its subprojects again to cache |
100 |
// new subprojects might have been added/activated for the changed project |
101 |
synchronized (cacheLock) { |
102 |
if (null == projectsToAddToCache) { |
103 |
projectsToAddToCache = new HashSet<NbMavenProjectImpl>(1); |
104 |
} |
105 |
projectsToAddToCache.add((NbMavenProjectImpl) evtSource); |
106 |
} |
94 |
} |
107 |
} |
95 |
} |
108 |
} |
96 |
} |
109 |
} |
Lines 111-117
Link Here
|
111 |
} |
124 |
} |
112 |
|
125 |
|
113 |
public void addMavenProject(NbMavenProjectImpl project) { |
126 |
public void addMavenProject(NbMavenProjectImpl project) { |
114 |
synchronized (lock) { |
127 |
synchronized (setLock) { |
115 |
if (!set.contains(project)) { |
128 |
if (!set.contains(project)) { |
116 |
LOG.fine("Adding Maven project:" + project.getArtifactRelativeRepositoryPath()); |
129 |
LOG.fine("Adding Maven project:" + project.getArtifactRelativeRepositoryPath()); |
117 |
set.add(project); |
130 |
set.add(project); |
Lines 119-131
Link Here
|
119 |
} |
132 |
} |
120 |
} |
133 |
} |
121 |
synchronized (cacheLock) { |
134 |
synchronized (cacheLock) { |
122 |
cachedProjects = null; |
135 |
// add the new project to cache incrementally if it has not been |
|
|
136 |
// added to cache already from "set" where we added it just above |
137 |
if (null != cachedProjects && !cachedProjects.contains(project)) { |
138 |
if (null == projectsToAddToCache) { |
139 |
projectsToAddToCache = new HashSet<NbMavenProjectImpl>(1); |
140 |
} |
141 |
projectsToAddToCache.add(project); |
142 |
} |
123 |
} |
143 |
} |
124 |
|
144 |
|
125 |
fireChange(); |
145 |
fireChange(); |
126 |
} |
146 |
} |
127 |
public void removeMavenProject(NbMavenProjectImpl project) { |
147 |
public void removeMavenProject(NbMavenProjectImpl project) { |
128 |
synchronized (lock) { |
148 |
synchronized (setLock) { |
129 |
if (set.contains(project)) { |
149 |
if (set.contains(project)) { |
130 |
LOG.fine("Removing Maven project:" + project.getArtifactRelativeRepositoryPath()); |
150 |
LOG.fine("Removing Maven project:" + project.getArtifactRelativeRepositoryPath()); |
131 |
set.remove(project); |
151 |
set.remove(project); |
Lines 165-178
Link Here
|
165 |
* get the list of currently opened maven projects.. kind of hack, but well.. |
185 |
* get the list of currently opened maven projects.. kind of hack, but well.. |
166 |
*/ |
186 |
*/ |
167 |
public Set<Project> getOpenedProjects() { |
187 |
public Set<Project> getOpenedProjects() { |
168 |
synchronized (lock) { |
188 |
synchronized (setLock) { |
169 |
return new HashSet<Project>(set); |
189 |
return new HashSet<Project>(set); |
170 |
} |
190 |
} |
171 |
} |
191 |
} |
172 |
|
192 |
|
173 |
public Set<FileObject> getOpenedProjectRoots() { |
193 |
public Set<FileObject> getOpenedProjectRoots() { |
174 |
Set<FileObject> toRet = new HashSet<FileObject>(); |
194 |
Set<FileObject> toRet = new HashSet<FileObject>(); |
175 |
synchronized (lock) { |
195 |
synchronized (setLock) { |
176 |
for (NbMavenProjectImpl prj : set) { |
196 |
for (NbMavenProjectImpl prj : set) { |
177 |
//TODO have generic and other source roots included to cater for projects with external source roots |
197 |
//TODO have generic and other source roots included to cater for projects with external source roots |
178 |
toRet.add(prj.getProjectDirectory()); |
198 |
toRet.add(prj.getProjectDirectory()); |
Lines 245-281
Link Here
|
245 |
return ProjectManager.mutex().readAccess(new Action<Set<NbMavenProjectImpl>>() { |
265 |
return ProjectManager.mutex().readAccess(new Action<Set<NbMavenProjectImpl>>() { |
246 |
public Set<NbMavenProjectImpl> run() { |
266 |
public Set<NbMavenProjectImpl> run() { |
247 |
synchronized (cacheLock) { |
267 |
synchronized (cacheLock) { |
248 |
Set<NbMavenProjectImpl> currentProjects; |
268 |
// is cachedProjects up-to-date? |
249 |
List<NbMavenProjectImpl> iterating; |
269 |
if (cachedProjects != null && null == projectsToAddToCache) { |
250 |
if (cachedProjects != null) { |
|
|
251 |
return new HashSet<NbMavenProjectImpl>(cachedProjects); |
270 |
return new HashSet<NbMavenProjectImpl>(cachedProjects); |
252 |
} |
271 |
} |
253 |
synchronized (lock) { |
272 |
// cachedProjects empty? |
254 |
currentProjects = new HashSet<NbMavenProjectImpl>(set); |
273 |
if (null == cachedProjects) { |
255 |
iterating = new ArrayList<NbMavenProjectImpl>(set); |
274 |
// full build of the cache |
|
|
275 |
Set<NbMavenProjectImpl> currentProjects; |
276 |
List<NbMavenProjectImpl> iterating; |
277 |
synchronized (setLock) { |
278 |
currentProjects = new HashSet<NbMavenProjectImpl>(set); |
279 |
iterating = new ArrayList<NbMavenProjectImpl>(set); |
280 |
} |
281 |
int index = 0; |
282 |
// iterate all opened projects and figure their subprojects.. |
283 |
// consider these as well. do so recursively. |
284 |
while (index < iterating.size()) { |
285 |
NbMavenProjectImpl prj = iterating.get(index); |
286 |
addSubProjects(currentProjects, iterating, prj); |
287 |
index = index + 1; |
288 |
} |
289 |
cachedProjects = currentProjects; |
290 |
projectsToAddToCache = null; |
256 |
} |
291 |
} |
257 |
int index = 0; |
292 |
// non-empty projectsToAddToCache? |
258 |
// iterate all opened projects and figure their subprojects.. consider these as well. do so recursively. |
293 |
if (null != projectsToAddToCache) { |
259 |
//TODO performance.. this could be expensive, maybe cache somehow |
294 |
// incrementally adding to the cache |
260 |
while (index < iterating.size()) { |
295 |
Set<NbMavenProjectImpl> currentProjects; |
261 |
NbMavenProjectImpl prj = iterating.get(index); |
296 |
List<NbMavenProjectImpl> iterating; |
262 |
SubprojectProvider sub = prj.getLookup().lookup(SubprojectProvider.class); |
297 |
currentProjects = new HashSet<NbMavenProjectImpl>(cachedProjects); |
263 |
if (sub != null) { |
298 |
currentProjects.addAll(projectsToAddToCache); |
264 |
Set<? extends Project> subs = sub.getSubprojects(); |
299 |
iterating = new ArrayList<NbMavenProjectImpl>(projectsToAddToCache); |
265 |
subs.removeAll(currentProjects); |
300 |
int index = 0; |
266 |
for (Project p : subs) { |
301 |
// iterate all new or changed (after propChange) projects |
267 |
if (p instanceof NbMavenProjectImpl) { |
302 |
// and figure their subprojects.. |
268 |
currentProjects.add((NbMavenProjectImpl)p); |
303 |
// consider these as well. do so recursively. |
269 |
iterating.add((NbMavenProjectImpl)p); |
304 |
while (index < iterating.size()) { |
270 |
} |
305 |
NbMavenProjectImpl prj = iterating.get(index); |
271 |
} |
306 |
addSubProjects(currentProjects, iterating, prj); |
|
|
307 |
index = index + 1; |
272 |
} |
308 |
} |
273 |
index = index + 1; |
309 |
cachedProjects = currentProjects; |
|
|
310 |
projectsToAddToCache = null; |
274 |
} |
311 |
} |
275 |
cachedProjects = currentProjects; |
|
|
276 |
return new HashSet<NbMavenProjectImpl>(cachedProjects); |
312 |
return new HashSet<NbMavenProjectImpl>(cachedProjects); |
277 |
} |
313 |
} |
278 |
} |
314 |
} |
279 |
}); |
315 |
}); |
280 |
} |
316 |
} |
|
|
317 |
|
318 |
private void addSubProjects(Set<NbMavenProjectImpl> finalset, List<NbMavenProjectImpl> iteratinglist, NbMavenProjectImpl prj) { |
319 |
SubprojectProvider sub = prj.getLookup().lookup(SubprojectProvider.class); |
320 |
if (sub != null) { |
321 |
Set<? extends Project> subs = sub.getSubprojects(); |
322 |
for (Project p : subs) { |
323 |
if (p instanceof NbMavenProjectImpl) { |
324 |
finalset.add((NbMavenProjectImpl) p); |
325 |
if (!iteratinglist.contains((NbMavenProjectImpl)p)) |
326 |
iteratinglist.add((NbMavenProjectImpl) p); |
327 |
} |
328 |
} |
329 |
} |
330 |
} |
281 |
} |
331 |
} |