001: /*
002: * Copyright (c) 2003, 2004 Rafael Steil
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on 02/06/2004 23:29:51
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.util;
044:
045: import java.io.File;
046: import java.util.HashMap;
047: import java.util.Map;
048: import java.util.Timer;
049: import java.util.TimerTask;
050:
051: import org.apache.log4j.Logger;
052:
053: /**
054: * Monitor class for file changes.
055: *
056: * @author Rafael Steil
057: * @version $Id: FileMonitor.java,v 1.9 2007/04/12 02:11:53 rafaelsteil Exp $
058: */
059: public class FileMonitor {
060: private static Logger logger = Logger.getLogger(FileMonitor.class);
061: private static final FileMonitor instance = new FileMonitor();
062: private Timer timer;
063: private Map timerEntries;
064:
065: private FileMonitor() {
066: this .timerEntries = new HashMap();
067: this .timer = new Timer(true);
068: }
069:
070: public static FileMonitor getInstance() {
071: return instance;
072: }
073:
074: /**
075: * Add a file to the monitor
076: *
077: * @param listener The file listener
078: * @param filename The filename to watch
079: * @param period The watch interval.
080: */
081: public void addFileChangeListener(FileChangeListener listener,
082: String filename, long period) {
083: this .removeFileChangeListener(filename);
084:
085: logger.info("Watching " + filename);
086:
087: FileMonitorTask task = new FileMonitorTask(listener, filename);
088:
089: this .timerEntries.put(filename, task);
090: this .timer.schedule(task, period, period);
091: }
092:
093: /**
094: * Stop watching a file
095: *
096: * @param listener The file listener
097: * @param filename The filename to keep watch
098: */
099: public void removeFileChangeListener(String filename) {
100: FileMonitorTask task = (FileMonitorTask) this .timerEntries
101: .remove(filename);
102:
103: if (task != null) {
104: task.cancel();
105: }
106: }
107:
108: private static class FileMonitorTask extends TimerTask {
109: private FileChangeListener listener;
110: private String filename;
111: private File monitoredFile;
112: private long lastModified;
113:
114: public FileMonitorTask(FileChangeListener listener,
115: String filename) {
116: this .listener = listener;
117: this .filename = filename;
118:
119: this .monitoredFile = new File(filename);
120: if (!this .monitoredFile.exists()) {
121: return;
122: }
123:
124: this .lastModified = this .monitoredFile.lastModified();
125: }
126:
127: public void run() {
128: long latestChange = this.monitoredFile.lastModified();
129: if (this.lastModified != latestChange) {
130: this.lastModified = latestChange;
131:
132: this.listener.fileChanged(this.filename);
133: }
134: }
135: }
136: }
|