本文整理汇总了Java中java.nio.file.StandardWatchEventKinds类的典型用法代码示例。如果您正苦于以下问题:Java StandardWatchEventKinds类的具体用法?Java StandardWatchEventKinds怎么用?Java StandardWatchEventKinds使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
StandardWatchEventKinds类属于java.nio.file包,在下文中一共展示了StandardWatchEventKinds类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: watch
import java.nio.file.StandardWatchEventKinds; //导入依赖的package包/类
private void watch() {
try {
WatchService watchService = directoryPath.getFileSystem().newWatchService();
directoryPath.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
while (true) {
WatchKey watchKey = watchService.take();
for (final WatchEvent<?> event : watchKey.pollEvents()) {
takeActionOnChangeEvent(event);
}
}
} catch (InterruptedException interruptedException) {
System.out.println("Thread got interrupted:" + interruptedException);
} catch (Exception exception) {
exception.printStackTrace();
}
}
示例2: handleEvent
import java.nio.file.StandardWatchEventKinds; //导入依赖的package包/类
@SuppressWarnings ( "unchecked" )
private void handleEvent ( final WatchEvent<?> event )
{
final Kind<?> kind = event.kind ();
if ( kind == StandardWatchEventKinds.OVERFLOW )
{
// FIXME: full rescan
return;
}
final Path path = this.path.resolve ( ( (WatchEvent<Path>)event ).context () );
if ( kind == StandardWatchEventKinds.ENTRY_CREATE )
{
this.listener.event ( path, Event.ADDED );
}
else if ( kind == StandardWatchEventKinds.ENTRY_DELETE )
{
this.listener.event ( path, Event.REMOVED );
}
else if ( kind == StandardWatchEventKinds.ENTRY_MODIFY )
{
this.listener.event ( path, Event.MODIFIED );
}
}
示例3: run
import java.nio.file.StandardWatchEventKinds; //导入依赖的package包/类
@Override
public void run() {
while (true) {
try {
WatchKey key = watchService.take();
if (key == trustedKey) {
for (WatchEvent<?> watchEvent : key.pollEvents()) {
WatchEvent.Kind<?> kind = watchEvent.kind();
if (kind != StandardWatchEventKinds.OVERFLOW) {
synchronizeTrustedCertificates();
}
}
}
if (!key.reset()) {
break;
}
} catch (InterruptedException e) {
logger.error("Watcher interrupted.", e);
}
}
}
示例4: StorageWatcher
import java.nio.file.StandardWatchEventKinds; //导入依赖的package包/类
public StorageWatcher ( final StorageManager storageManager, final BaseWatcher baseWatcher, final Path path, final WatchService watcher ) throws IOException
{
this.storageManager = storageManager;
this.baseWatcher = baseWatcher;
this.watcher = watcher;
this.path = path;
this.key = path.register ( watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY );
baseWatcher.addWatcherMap ( path, this );
final File nativeDir = new File ( path.toFile (), "native" );
baseWatcher.addWatcherMap ( nativeDir.toPath (), this );
logger.debug ( "Checking native dir: {}", nativeDir );
if ( nativeDir.exists () && nativeDir.isDirectory () )
{
this.nativeKey = nativeDir.toPath ().register ( this.watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE );
check ();
}
}
示例5: BaseWatcher
import java.nio.file.StandardWatchEventKinds; //导入依赖的package包/类
public BaseWatcher ( final StorageManager storageManager, final File base ) throws IOException
{
this.storageManager = storageManager;
this.base = base.toPath ();
this.watcher = FileSystems.getDefault ().newWatchService ();
this.baseKey = base.toPath ().register ( this.watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE );
logger.debug ( "Checking for initial storages" );
for ( final File child : this.base.toFile ().listFiles () )
{
logger.debug ( "Found initial storage dir - {}", child );
checkAddStorage ( child.toPath () );
}
startWatcher ();
}
示例6: setWatcherOnThemeFile
import java.nio.file.StandardWatchEventKinds; //导入依赖的package包/类
private static void setWatcherOnThemeFile() {
try {
WatchService watchService = FileSystems.getDefault().newWatchService();
WatchKey watchKey = path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
while (true) {
final WatchKey wk = watchService.take();
for (WatchEvent<?> event : wk.pollEvents()) {
//we only register "ENTRY_MODIFY" so the context is always a Path.
final Path changed = (Path) event.context();
System.out.println(changed);
if (changed.endsWith("Theme.css")) {
System.out.println("Theme.css has changed...reloading stylesheet.");
scene.getStylesheets().clear();
scene.getStylesheets().add("resources/Theme.css");
}
}
boolean valid = wk.reset();
if (!valid)
System.out.println("Watch Key has been reset...");
}
} catch (Exception e) { /*Thrown to void*/ }
}
示例7: run
import java.nio.file.StandardWatchEventKinds; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public void run() {
while (running) {
try {
final WatchKey watchKey = watcher.take();
for (final WatchEvent<?> event : watchKey.pollEvents()) {
Path changed = (Path) event.context();
if (changed == null || event.kind() == StandardWatchEventKinds.OVERFLOW) {
System.out.println("bad file watch event: " + event);
continue;
}
changed = watchedDirectory.resolve(changed);
for (final ListenerAndPath x : listeners) {
if (Thread.interrupted() && !running)
return;
if (changed.startsWith(x.startPath)) {
x.listener.fileChanged(changed, (Kind<Path>) event.kind());
}
}
}
watchKey.reset();
} catch (final InterruptedException e) {}
}
}
示例8: simpleTest
import java.nio.file.StandardWatchEventKinds; //导入依赖的package包/类
public void simpleTest(Path path) throws Exception
{
WatchService watchService=FileSystems.getDefault().newWatchService();
path.register(watchService,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY);
while(true)
{
WatchKey watchKey=watchService.take();
List<WatchEvent<?>> watchEvents = watchKey.pollEvents();
for(WatchEvent<?> event : watchEvents){
//TODO 根据事件类型采取不同的操作。。。。。。。
System.out.println("["+event.context()+"]文件发生了["+event.kind()+"]事件");
}
watchKey.reset();
}
}
示例9: MacOSXWatchKey
import java.nio.file.StandardWatchEventKinds; //导入依赖的package包/类
public MacOSXWatchKey(final Watchable file, final FSEventWatchService service, final WatchEvent.Kind<?>[] events) {
super(service);
this.file = file;
boolean reportCreateEvents = false;
boolean reportModifyEvents = false;
boolean reportDeleteEvents = false;
for(WatchEvent.Kind<?> event : events) {
if(event == StandardWatchEventKinds.ENTRY_CREATE) {
reportCreateEvents = true;
}
else if(event == StandardWatchEventKinds.ENTRY_MODIFY) {
reportModifyEvents = true;
}
else if(event == StandardWatchEventKinds.ENTRY_DELETE) {
reportDeleteEvents = true;
}
}
this.reportCreateEvents = reportCreateEvents;
this.reportDeleteEvents = reportDeleteEvents;
this.reportModifyEvents = reportModifyEvents;
}
示例10: CORSConfigurationFileWatcher
import java.nio.file.StandardWatchEventKinds; //导入依赖的package包/类
/**
* Creates a new CORS filter configuration watcher.
*
* @param filterConfig The filter configuration. Must not be
* {@code null}.
*/
public CORSConfigurationFileWatcher(final FilterConfig filterConfig) {
if (filterConfig == null) {
throw new IllegalArgumentException("The servlet filter configuration must not be null");
}
this.filterConfig = filterConfig;
try {
watcher = FileSystems.getDefault().newWatchService();
Path dir = determineConfigDir();
dir.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);
LOG.fine("CORS Filter: Started watching for configuration file changes within " + dir);
} catch (IOException e) {
LOG.severe("CORS Filter: Failed to initialize file system watcher: " + e.getMessage());
}
}
示例11: FileWatcher
import java.nio.file.StandardWatchEventKinds; //导入依赖的package包/类
private FileWatcher(Map<Path, Consumer<Path>> registeredPaths) {
this.registeredPaths = ImmutableMap.copyOf(registeredPaths);
try {
watchService = FileSystems.getDefault().newWatchService();
ImmutableMap.Builder<WatchKey, Path> watchedDirsBuilder = ImmutableMap.builder();
for (Map.Entry<Path, Consumer<Path>> entry : registeredPaths.entrySet()) {
Path dir = entry.getKey().getParent();
WatchKey key =
dir.register(
watchService,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY);
watchedDirsBuilder.put(key, dir);
}
this.watchedDirs = watchedDirsBuilder.build();
} catch (IOException e) {
throw new UncheckedIOException("Could not create WatchService.", e);
}
executor = Executors.newSingleThreadScheduledExecutor();
}
示例12: translateActionToEvent
import java.nio.file.StandardWatchEventKinds; //导入依赖的package包/类
private WatchEvent.Kind<?> translateActionToEvent(int action) {
switch (action) {
case FILE_ACTION_MODIFIED :
return StandardWatchEventKinds.ENTRY_MODIFY;
case FILE_ACTION_ADDED :
case FILE_ACTION_RENAMED_NEW_NAME :
return StandardWatchEventKinds.ENTRY_CREATE;
case FILE_ACTION_REMOVED :
case FILE_ACTION_RENAMED_OLD_NAME :
return StandardWatchEventKinds.ENTRY_DELETE;
default :
return null; // action not recognized
}
}
示例13: test
import java.nio.file.StandardWatchEventKinds; //导入依赖的package包/类
/**
* Create a WatchService to watch for changes in the given directory
* and then attempt to close the WatchService and change a registration
* at the same time.
*/
static void test(Path dir, ExecutorService pool) throws Exception {
WatchService watcher = FileSystems.getDefault().newWatchService();
// initial registration
dir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE);
// submit tasks to close the WatchService and update the registration
Future<Void> closeResult;
Future<Boolean> registerResult;
if (RAND.nextBoolean()) {
closeResult = pool.submit(newCloserTask(watcher));
registerResult = pool.submit(newRegisterTask(watcher, dir));
} else {
registerResult = pool.submit(newRegisterTask(watcher, dir));
closeResult = pool.submit(newCloserTask(watcher));
}
closeResult.get();
registerResult.get();
}
示例14: processWatchEvent
import java.nio.file.StandardWatchEventKinds; //导入依赖的package包/类
/**
* Handles a watchevent, it currently only handles the DELETE and MODIFY - create isn't handled
* as when you create a new file you get two events - one of the create and one for the modify,
* therefore it can be safely handled in the modify only.
*
* If the delete corresponds to a adapter loaded - it deregisterWithAdapterManager it with the
* adatperManager If the modify is new / modified it passes it to the load method that handles
* re-registering existing adapters
*
* @param event
*/
public void processWatchEvent(final WatchEvent<?> event) {
synchronized (propertiesToAdapter) {
if (LOG.isInfoEnabled()) {
LOG.info("Logging watch event:" + event.kind() + ": " + event.context());
}
// check it is ended with .properties
if (isPropertyFile(event)) {
String path = ((Path) event.context()).toString();
Adapter adapter = propertiesToAdapter.get(path);
// if we have already seen this then deregister it
if (adapter != null) {
removeAdapter(path, adapter);
}
if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE
|| event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) {
File file = new File(getPathToWatch().toString(), ((Path) event.context()).toString());
load(file);
}
}
}
}
示例15: processEvent
import java.nio.file.StandardWatchEventKinds; //导入依赖的package包/类
/** Notify file system event. */
void processEvent() {
while(true) {
WatchKey signal;
try {
signal = watcher.take();
} catch (InterruptedException e) {
return;
}
for(WatchEvent<?> event : signal.pollEvents()) {
Kind<?> kind = event.kind();
if(kind == StandardWatchEventKinds.OVERFLOW) {
continue;
}
Path name = (Path)event.context();
notify(name.toAbsolutePath().toString(), kind);
}
key.reset();
}
}