package bluechip.jnotify;
import java.io.File;
import java.util.HashMap;
import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyException;
import net.contentobjects.jnotify.JNotifyListener;
public class FileMonitor {
private final HashMap<File, FileMonitorConfig> configs;
private JNotifyListener listener;
private boolean running;
private final Object waiter = new Object();
private long minWatchedInterval = 100;
private File lastWatchedFile;
private long lastWatchedTime;
public FileMonitor() {
configs = new HashMap<File, FileMonitorConfig>();
listener = new JNotifyListener() {
public void fileRenamed(int wd, String parent, String oldName, String newName) {
if (accept(parent, oldName, newName)) {
renamed(parent, oldName, newName);
}
}
public void fileModified(int wd, String parent, String name) {
if (accept(parent, name)) {
modified(parent, name);
}
}
public void fileDeleted(int wd, String parent, String name) {
if (accept(parent, name)) {
deleted(parent, name);
}
}
public void fileCreated(int wd, String parent, String name) {
if (accept(parent, name)) {
created(parent, name);
}
}
};
}
public boolean addWatch(FileMonitorConfig config) {
configs.put(config.getFile(), config);
try {
config.watchId = (JNotify.addWatch(config.getPath(), config.getMask(), config
.isWatchSubtree(), listener));
} catch (JNotifyException e) {
return false;
}
return true;
}
private boolean accept(String parent, String name) {
File file = new File(parent, name);
if (file.equals(lastWatchedFile)
&& System.currentTimeMillis() - lastWatchedTime < minWatchedInterval) {
return false;
}
FileMonitorConfig config = configs.get(file);
if (config == null) {
config = configs.get(new File(parent));
}
if (config != null && config.accept(file)) {
lastWatchedFile = file;
lastWatchedTime = System.currentTimeMillis();
return true;
} else
return false;
}
private boolean accept(String parent, String oldName, String newName) {
File file = new File(parent, oldName);
FileMonitorConfig config = configs.get(file);
boolean isFile = true;
if (config == null) {
config = configs.get(new File(parent));
isFile = false;
}
if (config != null && config.accept(file)) {
lastWatchedFile = new File(parent, newName);
if (isFile) {
configs.remove(file);
config.file = lastWatchedFile;
configs.put(lastWatchedFile, config);
}
lastWatchedTime = System.currentTimeMillis();
return true;
} else
return false;
}
public void start() throws JNotifyException {
running = true;
Thread t = new Thread() {
public void run() {
while (running) {
synchronized (waiter) {
try {
waiter.wait();
} catch (InterruptedException e) {
}
}
}
}
};
t.setDaemon(true);
t.start();
}
public void stop() {
running = false;
synchronized (waiter) {
waiter.notify();
}
}
public boolean removeWatch(File file) {
FileMonitorConfig config = configs.remove(file);
if (config == null)
return false;
try {
JNotify.removeWatch(config.watchId);
} catch (JNotifyException e) {
return false;
}
return true;
}
protected void renamed(String parent, String oldName, String newName) {
}
protected void modified(String parent, String name) {
}
protected void deleted(String parent, String name) {
}
protected void created(String parent, String name) {
}
/**
* @return the minWatchedInterval
*/
public long getMinWatchedInterval() {
return minWatchedInterval;
}
/**
* @param minWatchedInterval
* the minWatchedInterval to set
*/
public void setMinWatchedInterval(long minWatchedInterval) {
this.minWatchedInterval = minWatchedInterval;
}
/**
* @return the lastWatchedFile
*/
public File getLastWatchedFile() {
return lastWatchedFile;
}
/**
* @return the lastWatchedTime
*/
public long getLastWatchedTime() {
return lastWatchedTime;
}
}
以下方法由子类实现:
引用
protected void renamed(String parent, String oldName, String newName) {
}
protected void modified(String parent, String name) {
}
protected void deleted(String parent, String name) {
}
protected void created(String parent, String name) {
}
package bluechip.jnotify;
import java.io.File;
import java.io.FileFilter;
import net.contentobjects.jnotify.JNotify;
public final class FileMonitorConfig {
public enum MASK {
/**
* A file created
*/
CREATED(JNotify.FILE_CREATED),
/**
* A file deleted
*/
DELETED(JNotify.FILE_DELETED),
/**
* A file modified
*/
MODIFIED(JNotify.FILE_MODIFIED),
/**
* A file renamed
*/
RENAMED(JNotify.FILE_RENAMED);
private final int mask;
private MASK(int mask) {
this.mask = mask;
}
/**
* @return the mask
*/
public int getMask() {
return mask;
}
}
final String path;
final boolean watchSubtree;
int mask;
FileFilter filter;
int watchId;
File file;
public FileMonitorConfig(String filename, MASK... masks) {
file = new File(filename);
if (!file.isFile()) {
throw new IllegalArgumentException("Not a file: " + filename);
}
this.filter = new FileFilter() {
public boolean accept(File file1) {
return file.equals(file1);
}
};
this.path = file.getParent();
this.watchSubtree = false;
if (masks == null || masks.length == 0) {
this.mask = JNotify.FILE_ANY;
} else {
for (MASK m : masks) {
mask |= m.getMask();
}
}
}
public FileMonitorConfig(String path, boolean watchSubtree, MASK... masks) {
file = new File(path);
if (!file.isDirectory())
throw new IllegalArgumentException("Not a directory: " + path);
this.path = path;
this.watchSubtree = watchSubtree;
if (masks == null || masks.length == 0) {
this.mask = JNotify.FILE_ANY;
} else {
for (MASK m : masks) {
mask |= m.getMask();
}
}
}
/**
* @return the filter
*/
public FileFilter getFilter() {
return filter;
}
/**
* Tests whether or not the specified abstract pathname should be monitored.
*
* @param pathname
* @return
* @see java.io.FileFilter#accept(java.io.File)
*/
public boolean accept(File pathname) {
return filter == null ? true : filter.accept(pathname);
}
/**
* @param filter
* the filter to set
*/
public void setFilter(FileFilter filter) {
if (file.isFile())
throw new UnsupportedOperationException("It is a file,set filter for a directory.");
this.filter = filter;
}
/**
* @return the mask
*/
public int getMask() {
return mask;
}
/**
* @return the path
*/
public String getPath() {
return path;
}
/**
* @return the watchSubtree
*/
public boolean isWatchSubtree() {
return watchSubtree;
}
/**
* @return the file
*/
public File getFile() {
return file;
}
}
分享到:
相关推荐
通过java代码利用JNotify工具实现监控一个文件夹下的文件或者文件夹的动态变化,资料包含文件代码,maven依赖,dll动态库(win和linux)
NULL 博文链接:https://andilyliao.iteye.com/blog/634586
对文件进行每五分钟的监控,把整个文件的内容全部传输至另一文件.以达到两个文件的一致性.
JNotify,一个支持动态监控文件和文件夹(支持级联监控)的架包。在linux系统中,调用的是linux底层的inotify服务,只是添加了对子文件夹级联监控的功能。在windows中,需要添加附件的dll文件,因为windows默认没有...
jnotify32 位
jNotify漂亮的提示插件
这个是Jnotify的源码,相关配置,及jar包。是我千辛万苦找到的,希望能帮上大家,免去寻找的麻烦。
jnotify_64bit jnotify_64bit jnotify_64bit jnotify_64bit
jnotify(包含dll与so) 需要的下载..
用java写的windows下的文件搜索程序,底层索引存储采用bst(二叉排序树),构造索引时采用深度递归算法(偏慢),只支持对文件的索引,不支持对文件夹的索引,采用jnotify监听文件的新建,修改,删除和重命名,同时自动...
支持多任务文件夹监控同步 支持文件创建 修改 删除 重命名事件监控 附件内有源码,供学习参考
jnotify-lib-0.93 通过java调用dll 实现目录变化的系统事件通知,比轮询 多线程监控 更胜一筹
基于 Jquery 的 Nitofication 元件. 提供很基本但實用的功能. 文檔大小只有2K.
jNotify:操作结果信息提示条
jnotify-lib-0.93.rar java监听硬盘的引用包,可以通过java来监听到硬盘上的创建、修改、删除文件的动作等等
文件复制操作,文件夹监控,包括jnotify,java 7 WatchService
jnotify监控文件变化,并将变化的文件通过linux rsync 命令同步到config.properties配置中配置的服务器 ##文件结构说明 src 为java源代码文件 target 为项目编译文件输出目录 pom.xml 为maven项目配置文件 FileSync ...
JNotify 基于Jquery的消息提示插件 参数 var defaults = { // 容器 notifyContainer 'className':'JNotify-danger', //容器 notifyContainer附加类名 'theme':'error', // 主题 “success”、“error”、...
openrasp-jnotify一个改造过的 jnotify 版本改造点,补全linux inotify 事件监听,使监听事件更全面支持更低版本的 linux内核支持32位linux使用 IDEA 编译即可