001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.tasklist.impl;
043:
044: import org.netbeans.modules.tasklist.filter.TaskFilter;
045: import java.util.ArrayList;
046: import java.util.Collection;
047: import java.util.HashMap;
048: import java.util.HashSet;
049: import java.util.Iterator;
050: import java.util.LinkedList;
051: import java.util.List;
052: import java.util.Map;
053: import java.util.Queue;
054: import java.util.Set;
055: import org.netbeans.spi.tasklist.FileTaskScanner;
056: import org.netbeans.spi.tasklist.Task;
057: import org.openide.filesystems.FileObject;
058: import org.openide.util.Exceptions;
059:
060: /**
061: *
062: * @author S. Aubrecht
063: */
064: class FileScanningWorker implements Runnable {
065:
066: private TaskCache cache;
067: private TaskList taskList;
068: private boolean isCancel = false;
069:
070: private TaskManagerImpl.FileScannerProgress progress;
071: private Set<FileTaskScanner> preparedScanners = new HashSet<FileTaskScanner>();
072:
073: private Iterator<FileObject> resourceIterator;
074: private Queue<FileObject> priorityResourceIterator = new LinkedList<FileObject>();
075: private Map<FileObject, Collection<FileTaskScanner>> priorityResource2scanner = new HashMap<FileObject, Collection<FileTaskScanner>>();
076:
077: private TaskFilter filter;
078:
079: private final Object SCAN_LOCK = new Object();
080: private final Object SLEEP_LOCK = new Object();
081:
082: /** Creates a new instance of Scanner */
083: public FileScanningWorker(TaskCache cache, TaskList taskList,
084: TaskFilter filter,
085: TaskManagerImpl.FileScannerProgress progress) {
086: this .cache = cache;
087: this .taskList = taskList;
088: this .filter = filter;
089: this .progress = progress;
090: }
091:
092: public void scan(Iterator<FileObject> resources, TaskFilter filter) {
093: abort();
094:
095: synchronized (SLEEP_LOCK) {
096:
097: this .filter = filter;
098:
099: List<? extends FileTaskScanner> providers = ScannerList
100: .getFileScannerList().getScanners();
101: for (FileTaskScanner ts : providers) {
102: if (filter.isEnabled(ts)
103: && !preparedScanners.contains(ts)) {
104: ts.notifyPrepare();
105: preparedScanners.add(ts);
106: }
107: }
108: this .resourceIterator = resources;
109:
110: wakeup();
111: }
112: }
113:
114: public void priorityScan(FileTaskScanner scanner, FileObject... res) {
115: boolean wakeupNeeded = false;
116: synchronized (SCAN_LOCK) {
117:
118: wakeupNeeded = isCancel || !hasNext();
119:
120: if (filter.isEnabled(scanner)) {
121: if (!preparedScanners.contains(scanner)) {
122: scanner.notifyPrepare();
123: preparedScanners.add(scanner);
124: }
125: for (FileObject rc : res) {
126: Collection<FileTaskScanner> scanners = priorityResource2scanner
127: .get(rc);
128: if (null == scanners) {
129: scanners = new ArrayList<FileTaskScanner>(10);
130: priorityResource2scanner.put(rc, scanners);
131: }
132: if (!priorityResourceIterator.contains(rc)) {
133: priorityResourceIterator.offer(rc);
134: }
135: }
136: }
137:
138: }
139:
140: if (wakeupNeeded) {
141: wakeup();
142: }
143: }
144:
145: public void priorityScan(FileObject... res) {
146: boolean wakeupNeeded = false;
147: synchronized (SCAN_LOCK) {
148:
149: wakeupNeeded = isCancel || !hasNext();
150:
151: List<? extends FileTaskScanner> scanners = ScannerList
152: .getFileScannerList().getScanners();
153: for (FileTaskScanner ts : scanners) {
154: if (filter.isEnabled(ts)
155: && !preparedScanners.contains(ts)) {
156: ts.notifyPrepare();
157: preparedScanners.add(ts);
158: }
159: }
160:
161: for (FileObject rc : res) {
162: priorityResource2scanner.remove(rc);
163: if (!priorityResourceIterator.contains(rc)) {
164: priorityResourceIterator.offer(rc);
165: }
166: }
167:
168: }
169:
170: if (wakeupNeeded) {
171: wakeup();
172: }
173: }
174:
175: public void run() {
176: synchronized (SLEEP_LOCK) {
177: while (true) {
178:
179: if (killed) {
180: return;
181: }
182:
183: progress.started();
184:
185: Set<FileTaskScanner> scannersToNotify = null;
186: ScanItem item = new ScanItem();
187: while (true) {
188:
189: synchronized (SCAN_LOCK) {
190: if (getNext(item)) {
191: if (!scan(item)) {
192: isCancel = true;
193: }
194: } else {
195: isCancel = true;
196: }
197: if (isCancel) {
198: scannersToNotify = new HashSet<FileTaskScanner>(
199: preparedScanners);
200: }
201: }
202:
203: if (isCancel) {
204: break;
205: }
206: }
207:
208: cleanUp(scannersToNotify);
209:
210: try {
211: SLEEP_LOCK.wait();
212: } catch (InterruptedException e) {
213: //ignore
214: }
215: }
216: }
217: }
218:
219: private void wakeup() {
220: synchronized (SLEEP_LOCK) {
221: isCancel = false;
222: SLEEP_LOCK.notifyAll();
223: }
224: }
225:
226: void abort() {
227: isCancel = true;
228: }
229:
230: private boolean killed = false;
231:
232: void kill() {
233: abort();
234: killed = true;
235: wakeup();
236: }
237:
238: private List<Task> scannedTasks = new LinkedList<Task>();
239:
240: private boolean scan(ScanItem item) {
241: if (isCancel)
242: return false;
243:
244: boolean atLeastOneProviderIsActive = false;
245:
246: for (FileTaskScanner scanner : item.scanners) {
247: //check filter for enabled providers
248: if (!filter.isEnabled(scanner))
249: continue;
250:
251: //check filter for visible items limit
252: if (filter.isTaskCountLimitReached(taskList
253: .countTasks(scanner)))
254: continue;
255:
256: atLeastOneProviderIsActive = true;
257: scannedTasks.clear();
258:
259: if (cache.isUpToDate(item.resource, scanner)) {
260: cache.getTasks(item.resource, scanner, scannedTasks);
261: } else {
262: List<? extends Task> newTasks = null;
263: try {
264: if (item.resource.isValid())
265: newTasks = scanner.scan(item.resource);
266: } catch (Throwable e) {
267: //don't let uncaught exceptions break the thread synchronization
268: Exceptions.printStackTrace(e);
269: }
270: if (null == newTasks) {
271: cache
272: .getTasks(item.resource, scanner,
273: scannedTasks);
274: } else {
275: scannedTasks.addAll(newTasks);
276: cache.scanned(item.resource, scanner, scannedTasks);
277: }
278: }
279:
280: if (isCancel) {
281: return false;
282: }
283: taskList.update(scanner, item.resource, scannedTasks,
284: filter);
285: }
286: return atLeastOneProviderIsActive;
287: }
288:
289: private void cleanUp(Set<FileTaskScanner> scannersToNotify) {
290: progress.finished();
291:
292: synchronized (this ) {
293: resourceIterator = null;
294: priorityResourceIterator.clear();
295: priorityResource2scanner.clear();
296: }
297: notifyFinished(scannersToNotify);
298: }
299:
300: private void notifyFinished(Set<FileTaskScanner> scannersToNotify) {
301: if (null != scannersToNotify) {
302: for (FileTaskScanner ts : scannersToNotify) {
303: ts.notifyFinish();
304: }
305: }
306: preparedScanners.clear();
307: }
308:
309: private boolean getNext(ScanItem item) {
310: item.resource = priorityResourceIterator.poll();
311: item.scanners = preparedScanners;
312:
313: if (null != item.resource) {
314: item.scanners = priorityResource2scanner.get(item.resource);
315: if (null == item.scanners)
316: item.scanners = preparedScanners;
317: } else if (null != resourceIterator
318: && resourceIterator.hasNext()) {
319: item.resource = resourceIterator.next();
320: }
321: return null != item.resource;
322: }
323:
324: private boolean hasNext() {
325: return priorityResourceIterator.size() > 0
326: || (null != resourceIterator && resourceIterator
327: .hasNext());
328: }
329:
330: private static class ScanItem {
331: FileObject resource;
332: Collection<FileTaskScanner> scanners;
333: }
334: }
|