/src/mpv5/utils/files/FileMonitor.java
Java | 115 lines | 67 code | 15 blank | 33 comment | 6 complexity | ebe25c46af8d666a6c270fde344de733 MD5 | raw file
1// Copyright (C) 2007 Google Inc. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package mpv5.utils.files; 16 17import java.io.File; 18import java.util.HashMap; 19import java.util.Timer; 20import java.util.TimerTask; 21 22/** 23 * Monitor files for changes. This singleton class maintains a map of files to 24 * monitor and objects to notify when something they change. 25 */ 26public class FileMonitor { 27 28 private static final FileMonitor SINGLETON = new FileMonitor(); 29 30 private Timer timer; 31 private HashMap<String, TimerTask> timerTasks; 32 33 private FileMonitor() { 34 timer = new Timer(true); 35 timerTasks = new HashMap<String, TimerTask>(); 36 } 37 38 /** 39 * Returns the singleton instance of this class. 40 * @return the singleton instance 41 */ 42 public static FileMonitor getInstance() { 43 return SINGLETON; 44 } 45 46 /** 47 * Start monitoring a file. 48 * 49 * @param listener listener to notify when the file changed. 50 * @param fileName name of the file to monitor. 51 * @param period polling period in milliseconds. 52 */ 53 public void addFileChangeListener(FileChangeListener listener, 54 String fileName, long period) { 55 removeFileChangeListener(listener, fileName); 56 FileMonitorTask task = new FileMonitorTask(listener, fileName); 57 timerTasks.put(fileName + listener.hashCode(), task); 58 timer.schedule(task, period, period); 59 } 60 61 /** 62 * Remove the listener from the notification list. 63 * 64 * @param listener the listener to be removed. 65 */ 66 public void removeFileChangeListener(FileChangeListener listener, 67 String fileName) { 68 FileMonitorTask task = (FileMonitorTask) timerTasks.remove(fileName 69 + listener.hashCode()); 70 if (task != null) { 71 task.cancel(); 72 } 73 } 74 75 protected void fireFileChangeEvent(FileChangeListener listener, 76 String fileName) { 77 listener.fileChanged(fileName); 78 } 79 80 class FileMonitorTask extends TimerTask { 81 FileChangeListener listener; 82 String fileName; 83 File monitoredFile; 84 long lastModified; 85 86 public FileMonitorTask(FileChangeListener listener, String fileName) { 87 this.listener = listener; 88 this.fileName = fileName; 89 this.lastModified = 0; 90 monitoredFile = new File(fileName); 91 this.lastModified = getLastModified(); 92 } 93 94 private long getLastModified() { 95 if (monitoredFile.exists()) { 96 return monitoredFile.lastModified(); 97 } else { 98 return -1; 99 } 100 } 101 102 @Override 103 public void run() { 104 long lastModifiedInt = getLastModified(); 105 if (lastModifiedInt != this.lastModified) { 106 this.lastModified = lastModifiedInt; 107 fireFileChangeEvent(this.listener, this.fileName); 108 } 109 } 110 } 111 112 public interface FileChangeListener { 113 public void fileChanged(String fileName); 114 } 115}