PageRenderTime 35ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/utils/files/FileMonitor.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 115 lines | 67 code | 15 blank | 33 comment | 6 complexity | ebe25c46af8d666a6c270fde344de733 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  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. package mpv5.utils.files;
  15. import java.io.File;
  16. import java.util.HashMap;
  17. import java.util.Timer;
  18. import java.util.TimerTask;
  19. /**
  20. * Monitor files for changes. This singleton class maintains a map of files to
  21. * monitor and objects to notify when something they change.
  22. */
  23. public class FileMonitor {
  24. private static final FileMonitor SINGLETON = new FileMonitor();
  25. private Timer timer;
  26. private HashMap<String, TimerTask> timerTasks;
  27. private FileMonitor() {
  28. timer = new Timer(true);
  29. timerTasks = new HashMap<String, TimerTask>();
  30. }
  31. /**
  32. * Returns the singleton instance of this class.
  33. * @return the singleton instance
  34. */
  35. public static FileMonitor getInstance() {
  36. return SINGLETON;
  37. }
  38. /**
  39. * Start monitoring a file.
  40. *
  41. * @param listener listener to notify when the file changed.
  42. * @param fileName name of the file to monitor.
  43. * @param period polling period in milliseconds.
  44. */
  45. public void addFileChangeListener(FileChangeListener listener,
  46. String fileName, long period) {
  47. removeFileChangeListener(listener, fileName);
  48. FileMonitorTask task = new FileMonitorTask(listener, fileName);
  49. timerTasks.put(fileName + listener.hashCode(), task);
  50. timer.schedule(task, period, period);
  51. }
  52. /**
  53. * Remove the listener from the notification list.
  54. *
  55. * @param listener the listener to be removed.
  56. */
  57. public void removeFileChangeListener(FileChangeListener listener,
  58. String fileName) {
  59. FileMonitorTask task = (FileMonitorTask) timerTasks.remove(fileName
  60. + listener.hashCode());
  61. if (task != null) {
  62. task.cancel();
  63. }
  64. }
  65. protected void fireFileChangeEvent(FileChangeListener listener,
  66. String fileName) {
  67. listener.fileChanged(fileName);
  68. }
  69. class FileMonitorTask extends TimerTask {
  70. FileChangeListener listener;
  71. String fileName;
  72. File monitoredFile;
  73. long lastModified;
  74. public FileMonitorTask(FileChangeListener listener, String fileName) {
  75. this.listener = listener;
  76. this.fileName = fileName;
  77. this.lastModified = 0;
  78. monitoredFile = new File(fileName);
  79. this.lastModified = getLastModified();
  80. }
  81. private long getLastModified() {
  82. if (monitoredFile.exists()) {
  83. return monitoredFile.lastModified();
  84. } else {
  85. return -1;
  86. }
  87. }
  88. @Override
  89. public void run() {
  90. long lastModifiedInt = getLastModified();
  91. if (lastModifiedInt != this.lastModified) {
  92. this.lastModified = lastModifiedInt;
  93. fireFileChangeEvent(this.listener, this.fileName);
  94. }
  95. }
  96. }
  97. public interface FileChangeListener {
  98. public void fileChanged(String fileName);
  99. }
  100. }