001: /*
002: The contents of this file are subject to the Mozilla Public License Version 1.1
003: (the "License"); you may not use this file except in compliance with the
004: License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005:
006: Software distributed under the License is distributed on an "AS IS" basis,
007: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: for the specific language governing rights and
009: limitations under the License.
010:
011: The Original Code is "The Columba Project"
012:
013: The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
014: Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
015:
016: All Rights Reserved.
017: */
018: package org.columba.core.filemonitor;
019:
020: /*
021: TODO
022: - make it possible to monitor a directory
023: - make it possible to stop monitoring a file
024: */
025:
026: import java.io.File;
027: import java.util.HashMap;
028: import java.util.HashSet;
029: import java.util.Map;
030: import java.util.Set;
031:
032: /**
033: * @author Celso Pinto <cpinto@yimports.com>
034: */
035: public class ObserverThread extends Thread {
036:
037: static final int DEFAULT_POLLING_INTERVAL = 60;
038:
039: private final Map<File, Set> watchedFiles;
040:
041: private long lastExecution;
042: private int pollingInterval;
043: private boolean terminate = false;
044:
045: public ObserverThread(int interval) {
046:
047: watchedFiles = new HashMap<File, Set>();
048: lastExecution = System.currentTimeMillis();
049: pollingInterval = interval;
050:
051: }
052:
053: public void terminate() {
054: terminate = true;
055: }
056:
057: public void monitorFile(File file, FileObserver observer) {
058:
059: synchronized (watchedFiles) {
060:
061: Set<FileObserver> observerSet;
062:
063: observerSet = watchedFiles.get(file);
064:
065: if (observerSet == null) {
066:
067: observerSet = new HashSet<FileObserver>();
068: watchedFiles.put(file, observerSet);
069:
070: } else {
071:
072: if (observerSet.contains(observer))
073: return;
074:
075: }
076:
077: observerSet.add(observer);
078:
079: }
080:
081: }
082:
083: public void run() {
084:
085: while (!terminate) {
086:
087: performCheck();
088:
089: try {
090: sleep(pollingInterval);
091: } catch (InterruptedException ex) {
092: }
093:
094: }
095:
096: }
097:
098: private void performCheck() {
099:
100: synchronized (watchedFiles) {
101:
102: File file;
103: Set<FileObserver> observers;
104:
105: for (Map.Entry<File, Set> entry : watchedFiles.entrySet()) {
106:
107: file = entry.getKey();
108: observers = entry.getValue();
109:
110: if (!file.exists()) {
111:
112: for (FileObserver observer : observers)
113: observer.fileChanged(file,
114: FileObserver.FILE_REMOVED);
115:
116: watchedFiles.remove(file);
117: continue;
118:
119: }
120:
121: if (file.lastModified() <= lastExecution)
122: continue;
123:
124: for (FileObserver observer : observers)
125: observer.fileChanged(file,
126: FileObserver.FILE_CHANGED);
127:
128: }
129:
130: }
131:
132: lastExecution = System.currentTimeMillis();
133:
134: }
135: }
|