1 /*******************************************************************************
2 * Copyright (c) 2010- Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.accessor.impl;
15 import java.io.FileFilter;
16 import java.util.ArrayList;
17 import java.util.HashSet;
18 import java.util.List;
20 import java.util.Timer;
21 import java.util.TimerTask;
22 import java.util.concurrent.CopyOnWriteArrayList;
25 * DirectoryWatch monitors a directory for file additions / removals.
27 * DirectoryWatch is used in the long wait for WatchService.
29 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
31 public class DirectoryWatch {
33 public static final long POLL_INTERVAL = 10000; // seconds
38 List<File> knownFiles;
39 CopyOnWriteArrayList<DirectoryListener> listeners = new CopyOnWriteArrayList<DirectoryListener>();
41 TimerTask task = new TimerTask() {
47 public DirectoryWatch(File directory, FileFilter filter) {
48 this.directory = directory;
50 knownFiles = readFiles();
51 timer = new Timer(directory.toString()+" watcher", true);
52 timer.schedule(task, 1, POLL_INTERVAL);
57 * This method does one last poll.
64 public static class DirectoryEvent {
65 public Set<File> filesAdded = new HashSet<File>();
66 public Set<File> filesRemoved = new HashSet<File>();
69 public interface DirectoryListener {
70 void onWatchEvent(DirectoryEvent e);
73 public void addListener(DirectoryListener listener) {
74 listeners.add(listener);
77 public void removeListener(DirectoryListener listener) {
78 listeners.remove(listener);
82 * Get a snapshot of currently known files
84 * @return a snapshot of files
86 public List<File> files() {
91 * Reload the directory
93 public void refresh() {
98 * Add file to the known list without reading the disk.
99 * The modification is void after next timer refresh.
103 public synchronized void add(File f) {
104 ArrayList<File> newList = new ArrayList<File>( knownFiles );
106 knownFiles = newList;
110 * Remove file from the known list without reading the disk.
111 * The modification is void after next timer refresh.
115 public synchronized void remove(File f) {
116 ArrayList<File> newList = new ArrayList<File>( knownFiles );
118 knownFiles = newList;
123 * @return a list of absolute files
125 private List<File> readFiles() {
126 File[] files = directory.listFiles(filter);
127 List<File> newFiles = new ArrayList<File>( files.length );
128 for (File f : files) {
129 // System.out.println(f);
130 // f = f.getAbsoluteFile();
131 // System.out.println(f);
138 * Read files and spawn events
140 private synchronized void poll() {
141 DirectoryEvent e = read();
142 if (e.filesAdded.isEmpty() && e.filesRemoved.isEmpty()) return;
144 for (DirectoryListener l : listeners)
148 private DirectoryEvent read() {
149 List<File> oldFiles = knownFiles;
150 List<File> newFiles = readFiles();
151 knownFiles = newFiles;
153 DirectoryEvent result = new DirectoryEvent();
154 result.filesAdded.addAll(newFiles);
155 result.filesAdded.removeAll(oldFiles);
157 result.filesRemoved.addAll(oldFiles);
158 result.filesRemoved.removeAll(newFiles);