]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/impl/DirectoryWatch.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / accessor / impl / DirectoryWatch.java
index 1e83fd4939bb778fb9dafe03d3abd8ab9949b5b8..854e7e8ab9df9e39f3d8e114369fb99d9eed5fbb 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2010- Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- * \r
- * Contributors:\r
- *    VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.databoard.accessor.impl;\r
-\r
-import java.io.File;\r
-import java.io.FileFilter;\r
-import java.util.ArrayList;\r
-import java.util.HashSet;\r
-import java.util.List;\r
-import java.util.Set;\r
-import java.util.Timer;\r
-import java.util.TimerTask;\r
-import java.util.concurrent.CopyOnWriteArrayList;\r
-\r
-/**\r
- * DirectoryWatch monitors a directory for file additions / removals.\r
- * <p>\r
- * DirectoryWatch is used in the long wait for WatchService.   \r
- *\r
- * @author Toni Kalajainen <toni.kalajainen@vtt.fi>\r
- */\r
-public class DirectoryWatch {\r
-\r
-       public static final long POLL_INTERVAL = 10000; // seconds\r
-       \r
-       Timer timer;\r
-       FileFilter filter;\r
-       File directory;\r
-       List<File> knownFiles;\r
-       CopyOnWriteArrayList<DirectoryListener> listeners = new CopyOnWriteArrayList<DirectoryListener>();\r
-\r
-       TimerTask task = new TimerTask() { \r
-               public void run() {\r
-                       poll();\r
-               } \r
-       };\r
-       \r
-       public DirectoryWatch(File directory, FileFilter filter) {\r
-               this.directory = directory;\r
-               this.filter = filter;\r
-               knownFiles = readFiles();\r
-               timer = new Timer(directory.toString()+" watcher", true);\r
-               timer.schedule(task, 1, POLL_INTERVAL);\r
-       }\r
-       \r
-       /**\r
-        * Close the timer. \r
-        * This method does one last poll.\r
-        */\r
-       public void close() {           \r
-               timer.cancel();\r
-               poll();\r
-       }\r
-       \r
-       public static class DirectoryEvent {\r
-               public Set<File> filesAdded = new HashSet<File>();\r
-               public Set<File> filesRemoved = new HashSet<File>();\r
-       }\r
-       \r
-       public interface DirectoryListener {\r
-               void onWatchEvent(DirectoryEvent e);\r
-       }\r
-       \r
-       public void addListener(DirectoryListener listener) {\r
-               listeners.add(listener);                \r
-       }\r
-       \r
-       public void removeListener(DirectoryListener listener) {                \r
-               listeners.remove(listener);\r
-       }\r
-       \r
-       /**\r
-        * Get a snapshot of currently known files\r
-        * \r
-        * @return a snapshot of files\r
-        */\r
-       public List<File> files() {\r
-               return knownFiles;\r
-       }\r
-       \r
-       /**\r
-        * Reload the directory\r
-        */\r
-       public void refresh() {\r
-               poll();\r
-       }\r
-       \r
-       /**\r
-        * Add file to the known list without reading the disk. \r
-        * The modification is void after next timer refresh. \r
-        * \r
-        * @param f\r
-        */\r
-       public synchronized void add(File f) {\r
-               ArrayList<File> newList = new ArrayList<File>( knownFiles );\r
-               newList.add(f);\r
-               knownFiles = newList;\r
-       }\r
-       \r
-       /**\r
-        * Remove file from the known list without reading the disk.\r
-        * The modification is void after next timer refresh. \r
-        * \r
-        * @param f\r
-        */\r
-       public synchronized void remove(File f) {\r
-               ArrayList<File> newList = new ArrayList<File>( knownFiles );\r
-               newList.remove(f);\r
-               knownFiles = newList;           \r
-       }\r
-       \r
-       /**\r
-        * Read files\r
-        * @return a list of absolute files\r
-        */\r
-       private List<File> readFiles() {                \r
-               File[] files = directory.listFiles(filter);\r
-               List<File> newFiles = new ArrayList<File>( files.length );\r
-               for (File f : files) {\r
-//                     System.out.println(f);\r
-//                     f = f.getAbsoluteFile();\r
-//                     System.out.println(f);\r
-                       newFiles.add(f);                \r
-               }\r
-               return newFiles;\r
-       }\r
-       \r
-       /**\r
-        * Read files and spawn events\r
-        */\r
-       private synchronized void poll() {\r
-               DirectoryEvent e = read();\r
-               if (e.filesAdded.isEmpty() && e.filesRemoved.isEmpty()) return;\r
-               // Spawn an event\r
-               for (DirectoryListener l : listeners)\r
-                       l.onWatchEvent(e);\r
-       }\r
-       \r
-       private DirectoryEvent read() {\r
-               List<File> oldFiles = knownFiles;\r
-               List<File> newFiles = readFiles();\r
-               knownFiles = newFiles;\r
-\r
-               DirectoryEvent result = new DirectoryEvent();\r
-               result.filesAdded.addAll(newFiles);\r
-               result.filesAdded.removeAll(oldFiles);\r
-\r
-               result.filesRemoved.addAll(oldFiles);\r
-               result.filesRemoved.removeAll(newFiles);\r
-               \r
-               return result;\r
-       }\r
-       \r
-}\r
-\r
+/*******************************************************************************
+ * Copyright (c) 2010- Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * 
+ * Contributors:
+ *    VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.databoard.accessor.impl;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.Timer;
+import java.util.TimerTask;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+/**
+ * DirectoryWatch monitors a directory for file additions / removals.
+ * <p>
+ * DirectoryWatch is used in the long wait for WatchService.   
+ *
+ * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
+ */
+public class DirectoryWatch {
+
+       public static final long POLL_INTERVAL = 10000; // seconds
+       
+       Timer timer;
+       FileFilter filter;
+       File directory;
+       List<File> knownFiles;
+       CopyOnWriteArrayList<DirectoryListener> listeners = new CopyOnWriteArrayList<DirectoryListener>();
+
+       TimerTask task = new TimerTask() { 
+               public void run() {
+                       poll();
+               } 
+       };
+       
+       public DirectoryWatch(File directory, FileFilter filter) {
+               this.directory = directory;
+               this.filter = filter;
+               knownFiles = readFiles();
+               timer = new Timer(directory.toString()+" watcher", true);
+               timer.schedule(task, 1, POLL_INTERVAL);
+       }
+       
+       /**
+        * Close the timer. 
+        * This method does one last poll.
+        */
+       public void close() {           
+               timer.cancel();
+               poll();
+       }
+       
+       public static class DirectoryEvent {
+               public Set<File> filesAdded = new HashSet<File>();
+               public Set<File> filesRemoved = new HashSet<File>();
+       }
+       
+       public interface DirectoryListener {
+               void onWatchEvent(DirectoryEvent e);
+       }
+       
+       public void addListener(DirectoryListener listener) {
+               listeners.add(listener);                
+       }
+       
+       public void removeListener(DirectoryListener listener) {                
+               listeners.remove(listener);
+       }
+       
+       /**
+        * Get a snapshot of currently known files
+        * 
+        * @return a snapshot of files
+        */
+       public List<File> files() {
+               return knownFiles;
+       }
+       
+       /**
+        * Reload the directory
+        */
+       public void refresh() {
+               poll();
+       }
+       
+       /**
+        * Add file to the known list without reading the disk. 
+        * The modification is void after next timer refresh. 
+        * 
+        * @param f
+        */
+       public synchronized void add(File f) {
+               ArrayList<File> newList = new ArrayList<File>( knownFiles );
+               newList.add(f);
+               knownFiles = newList;
+       }
+       
+       /**
+        * Remove file from the known list without reading the disk.
+        * The modification is void after next timer refresh. 
+        * 
+        * @param f
+        */
+       public synchronized void remove(File f) {
+               ArrayList<File> newList = new ArrayList<File>( knownFiles );
+               newList.remove(f);
+               knownFiles = newList;           
+       }
+       
+       /**
+        * Read files
+        * @return a list of absolute files
+        */
+       private List<File> readFiles() {                
+               File[] files = directory.listFiles(filter);
+               List<File> newFiles = new ArrayList<File>( files.length );
+               for (File f : files) {
+//                     System.out.println(f);
+//                     f = f.getAbsoluteFile();
+//                     System.out.println(f);
+                       newFiles.add(f);                
+               }
+               return newFiles;
+       }
+       
+       /**
+        * Read files and spawn events
+        */
+       private synchronized void poll() {
+               DirectoryEvent e = read();
+               if (e.filesAdded.isEmpty() && e.filesRemoved.isEmpty()) return;
+               // Spawn an event
+               for (DirectoryListener l : listeners)
+                       l.onWatchEvent(e);
+       }
+       
+       private DirectoryEvent read() {
+               List<File> oldFiles = knownFiles;
+               List<File> newFiles = readFiles();
+               knownFiles = newFiles;
+
+               DirectoryEvent result = new DirectoryEvent();
+               result.filesAdded.addAll(newFiles);
+               result.filesAdded.removeAll(oldFiles);
+
+               result.filesRemoved.addAll(oldFiles);
+               result.filesRemoved.removeAll(newFiles);
+               
+               return result;
+       }
+       
+}
+