/*******************************************************************************
* JNotify - Allow java applications to register to File system events.
*
* Copyright (C) 2005 - Content Objects
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
******************************************************************************
*
* Content Objects, Inc., hereby disclaims all copyright interest in the
* library `JNotify' (a Java library for file system events).
*
* Yahali Sherman, 21 November 2005
* Content Objects, VP R&D.
*
******************************************************************************
* Author : Omry Yadan
******************************************************************************/
package net.contentobjects.jnotify.linux;
import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;
import net.contentobjects.jnotify.IJNotify;
import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyException;
import net.contentobjects.jnotify.JNotifyListener;
import net.contentobjects.jnotify.Util;
/** TODO : added by omry at Dec 11, 2005 : Handle move events*/
public class JNotifyAdapterLinux implements IJNotify
{
private Hashtable<Integer, Integer> _linuxWd2Wd;
private Hashtable<Integer, WatchData> _id2Data;
/**
* A set of files which was added by registerToSubTree (auto-watches)
*/
private Hashtable<String, String> _autoWatchesPaths;
private static int _watchIDCounter = 0;
public JNotifyAdapterLinux()
{
JNotify_linux.setNotifyListener(new INotifyListener()
{
public void notify(String name, int wd, int mask, int cookie)
{
try
{
notifyChangeEvent(name, wd, mask, cookie);
}
catch (RuntimeException e)
{
e.printStackTrace(System.out);
}
}
});
_id2Data = new Hashtable<Integer, WatchData>();
_linuxWd2Wd = new Hashtable<Integer, Integer>();
_autoWatchesPaths = new Hashtable<String, String>();
}
public int addWatch(String path, int mask, boolean watchSubtree, JNotifyListener listener)
throws JNotifyException
{
JNotify_linux.debug("JNotifyAdapterLinux.addWatch(path="+path+",mask="+Util.getMaskDesc(mask)+", watchSubtree="+watchSubtree+")");
// map mask to linux inotify mask.
int linuxMask = 0;
if ((mask & JNotify.FILE_CREATED) != 0)
{
linuxMask |= JNotify_linux.IN_CREATE;
}
if ((mask & JNotify.FILE_DELETED) != 0)
{
linuxMask |= JNotify_linux.IN_DELETE;
linuxMask |= JNotify_linux.IN_DELETE_SELF;
}
if ((mask & JNotify.FILE_MODIFIED) != 0)
{
linuxMask |= JNotify_linux.IN_ATTRIB;
linuxMask |= JNotify_linux.IN_MODIFY;
}
if ((mask & JNotify.FILE_RENAMED) != 0)
{
linuxMask |= JNotify_linux.IN_MOVED_FROM;
linuxMask |= JNotify_linux.IN_MOVED_TO;
}
// if watching subdirs, listen on create anyway.
// to know when new sub directories are created.
// these events should not reach the client code.
if (watchSubtree)
{
linuxMask |= JNotify_linux.IN_CREATE;
}
WatchData watchData = createWatch(null, true, new File(path), mask, linuxMask, watchSubtree, listener);
if (watchSubtree)
{
try
{
File file = new File(path);
registerToSubTree(true,watchData, file, false);
}
catch (JNotifyException e)
{
// cleanup
removeWatch(watchData._wd);
// and throw.
throw e;
}
}
return watchData._wd;
}
private WatchData createWatch(WatchData parentWatchData, boolean user,File path, int mask, int linuxMask, boolean watchSubtree, JNotifyListener listener) throws JNotifyException
{
String absPath = path.getPath();
int wd = _watchIDCounter++;
int linuxWd = JNotify_linux.addWatch(absPath, linuxMask);
WatchData watchData = new WatchData(parentWatchData, user, absPath, wd, linuxWd, mask, linuxMask, watchSubtree, listener);
_linuxWd2Wd.put(Integer.valueOf(linuxWd), Integer.valueOf(wd));
_id2Data.put(Integer.valueOf(wd), watchData);
if (!user)
{
_autoWatchesPaths.put(absPath, absPath);
}
return watchData;
}
private void registerToSubTree(boolean isRoot, WatchData parentWatch, File root, boolean fireCreatedEvents) throws JNotifyException
{
if (!parentWatch._user)
{
throw new RuntimeException("!parentWatch._user");
}
// make sure user really requested to be notified on this event.
// (in case of recursive listening, this IN_CREATE flag is always on, even if
// the user is not interester in creation events).
if (fireCreatedEvents && (parentWatch._mask & JNotify.FILE_CREATED) != 0)
{
String name = root.toString().substring(parentWatch._path.length()+1);
parentWatch.notifyFileCreated(name);
}
if (root.isDirectory())
{
// root was already registered by the calling method.
if (!isRoot)
{
try
{
createWatch(parentWatch, false, root, parentWatch._mask, parentWatch._linuxMask, parentWatch._watchSubtree, parentWatch._listener);
}
catch (JNotifyException e)
{
if (e.getErrorCode() == JNotifyException.ERROR_WATCH_LIMIT_REACHED)
JNotify_linux.warn("JNotifyAdapterLinux.registerToSubTree : warning, failed to register " + root + " :" + e.getMessage());
{
throw e;
}
// else, on any other error, try subtree anyway..
}
}
String files[] = root.list();
if (files != null)
{
for (int i = 0; i < files.length; i++)
{
String file = files[i];
registerToSubTree(false, parentWatch, new File(root, file), fireCreatedEvents);
}
}
}
}
public boolean removeWatch(int wd) throws JNotifyException
{
JNotify_linux.debug("JNotifyAdapterLinux.removeWatch("+ wd+ ")");
synchronized (_id2Data)
{
if (_id2Data.containsKey(Integer.valueOf(wd)))
{
WatchData watchData = (WatchData) _id2Data.get(Integer.valueOf(wd));
unwatch(watchData);
return true;
}
else
{
return false;
}
}
}
private void unwatch(WatchData data) throws JNotifyException
{
JNotifyException ex = null;
boolean ok = true;
try
{
JNotify_linux.removeWatch(data._linuxWd);
}
catch (JNotifyException e)
{
e.printStackTrace();
ex = e;
ok = false;
}
if (data._user)
{
for (int i = 0; i < data._subWd.size(); i++)
{
int wd = data._subWd.get(i).intValue();
try
{
JNotify_linux.removeWatch(wd);
}
catch (JNotifyException e)
{
e.printStackTrace();
ex = e;
ok = false;
}
}
}
if (!ok)
{
throw ex;
}
}
protected void notifyChangeEvent(String name, int linuxWd, int linuxMask, int cookie)
{
if (JNotify_linux.DEBUG)
{
debugLinux(name, linuxWd, linuxMask, cookie);
}
synchronized (_id2Data)
{
Integer iwd = (Integer) _linuxWd2Wd.get(Integer.valueOf(linuxWd));
if (iwd == null)
{
// This happens if an exception is thrown because used too many watches.
System.out.println("JNotifyAdapterLinux: warning, recieved event for an unregisted LinuxWD "+ linuxWd +" ignoring...");
return;
}
WatchData watchData = _id2Data.get(iwd);
if (watchData != null)
{
if ((linuxMask & JNotify_linux.IN_CREATE) != 0)
{
File newRootFile = new File(watchData._path, name);
if (watchData._watchSubtree)
{
try
{
createWatch(watchData.getParentWatch()
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
src.zip (22个子文件)
src
jnotify.dll 93KB
JavaHook.java 3KB
net
contentobjects
jnotify
JNotifyException.java 660B
linux
JNotifyAdapterLinux.java 16KB
JNotifyException_linux.java 826B
INotifyListener.java 2KB
JNotify_linux.java 8KB
macosx
FSEventListener.java 233B
JNotifyAdapterMacOSX.java 15KB
JNotify_macosx.java 2KB
JNotifyException_macosx.java 324B
win32
JNotify_win32.java 4KB
IWin32NotifyListener.java 2KB
JNotifyException_win32.java 372B
JNotifyAdapterWin32.java 6KB
JNotify.java 4KB
Util.java 578B
JNotifyAdapter.java 395B
IJNotify.java 2KB
JNotifyListener.java 2KB
lib
jnotify.dll 93KB
FieMonitor.java 2KB
共 22 条
- 1
资源评论
- mlszlj2014-03-20代码不值这个分数,网上很多
mengjingji
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功