當前位置: 首頁>>代碼示例>>Java>>正文


Java WatchEvent.context方法代碼示例

本文整理匯總了Java中java.nio.file.WatchEvent.context方法的典型用法代碼示例。如果您正苦於以下問題:Java WatchEvent.context方法的具體用法?Java WatchEvent.context怎麽用?Java WatchEvent.context使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.nio.file.WatchEvent的用法示例。


在下文中一共展示了WatchEvent.context方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: run

import java.nio.file.WatchEvent; //導入方法依賴的package包/類
@Override
public void run() {
    WatchService watchService = WatchServiceUtil.watchModify(pluginDir);
    WatchKey key;
    while (watchService != null){
        try {
            key = watchService.take();
            for (WatchEvent<?> watchEvent : key.pollEvents()) {
                if(watchEvent.kind() == ENTRY_MODIFY){
                    String fileName = watchEvent.context() == null ? "" : watchEvent.context().toString();
                    Plugin plugin = PluginLibraryHelper.getPluginByConfigFileName(fileName);
                    if(plugin != null){
                        plugin.init(PluginLibraryHelper.getPluginConfig(plugin));
                        log.info("已完成插件{}的配置重新加載",plugin.pluginName());
                    }
                }
            }
            key.reset();
        } catch (Exception e) {
            log.error("插件配置文件監聽異常",e);
            break;
        }
    }
}
 
開發者ID:DevopsJK,項目名稱:SuitAgent,代碼行數:25,代碼來源:PluginPropertiesWatcher.java

示例2: processSubevents

import java.nio.file.WatchEvent; //導入方法依賴的package包/類
/**
 * Processes subevents of the key.
 * @param key That has new events.
 * @param dir For the key.
 * @throws IOException If a subdirectory cannot be registered.
 */
private void processSubevents(final WatchKey key, final Path dir)
    throws IOException {
    for (final WatchEvent event : key.pollEvents()) {
        final WatchEvent.Kind kind = event.kind();
        final Path name = (Path) event.context();
        final Path child = dir.resolve(name);
        Logger.debug(
            this,
            "%s: %s%n", event.kind().name(), child
        );
        if (kind == ENTRY_CREATE) {
            try {
                if (Files.isDirectory(child)) {
                    this.processSubevents(child);
                }
            } catch (final IOException ex) {
                throw new IOException(
                    "Failed to register subdirectories.",
                    ex
                );
            }
        }
    }
}
 
開發者ID:driver733,項目名稱:VKMusicUploader,代碼行數:31,代碼來源:WatchDirs.java

示例3: setWatcherOnThemeFile

import java.nio.file.WatchEvent; //導入方法依賴的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*/ }
}
 
開發者ID:maximstewart,項目名稱:UDE,代碼行數:23,代碼來源:UFM.java

示例4: run

import java.nio.file.WatchEvent; //導入方法依賴的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) {}
	}
}
 
開發者ID:Njol,項目名稱:Motunautr,代碼行數:26,代碼來源:FileWatcher.java

示例5: processEvent

import java.nio.file.WatchEvent; //導入方法依賴的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();
	}
}
 
開發者ID:yoking-zhang,項目名稱:demo,代碼行數:26,代碼來源:DirectoryWatcher.java

示例6: processWatchKey

import java.nio.file.WatchEvent; //導入方法依賴的package包/類
private void processWatchKey(WatchKey watchKey) throws IOException {
  final long start = System.currentTimeMillis();
  final List<WatchEvent<?>> events = watchKey.pollEvents();

  int processed = 0;

  for (WatchEvent<?> event : events) {
    WatchEvent.Kind<?> kind = event.kind();

    if (!watchEvents.contains(kind)) {
      LOG.trace("Ignoring an {} event to {}", event.context());
      continue;
    }

    WatchEvent<Path> ev = cast(event);
    Path filename = ev.context();

    if (processEvent(kind, filename)) {
      processed++;
    }
  }

  LOG.debug("Handled {} out of {} event(s) for {} in {}", processed, events.size(), watchDirectory, JavaUtils.duration(start));
}
 
開發者ID:PacktPublishing,項目名稱:Mastering-Mesos,代碼行數:25,代碼來源:WatchServiceHelper.java

示例7: filesFromEvents

import java.nio.file.WatchEvent; //導入方法依賴的package包/類
private Set<File> filesFromEvents() throws InterruptedException {
  WatchKey key = watcher.take();
  Set<File> files = new LinkedHashSet<File>();
  if (key != null && key.isValid()) 
  {
    for (WatchEvent<?> event : key.pollEvents()) 
    {
      if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE || event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) 
      {
        Path item = (Path) event.context();
        File file = new File(((Path) key.watchable()).toAbsolutePath() + File.separator + item.getFileName());
        if (log.isDebugEnabled()) {
          log.debug("Watch Event: " + event.kind() + ": " + file);
        }
        if(isJarFile(file))
        {
          files.add(file);
        }
        else
          log.warn("[JAR Loader] Ignoring file "+file);
      }
      
    }
    key.reset();
   
  }
  return files;
}
 
開發者ID:javanotes,項目名稱:reactive-data,代碼行數:29,代碼來源:JarModuleLoader.java

示例8: handleEvent

import java.nio.file.WatchEvent; //導入方法依賴的package包/類
/**
 * Handle event.
 *
 * @param key the key
 */
private void handleEvent(final WatchKey key) {
    this.readLock.lock();
    try {
        for (final WatchEvent<?> event : key.pollEvents()) {
            if (event.count() <= 1) {
                final WatchEvent.Kind kind = event.kind();

                //The filename is the context of the event.
                final WatchEvent<Path> ev = (WatchEvent<Path>) event;
                final Path filename = ev.context();

                final Path parent = (Path) key.watchable();
                final Path fullPath = parent.resolve(filename);
                final File file = fullPath.toFile();

                LOGGER.trace("Detected event [{}] on file [{}]. Loading change...", kind, file);
                if (kind.name().equals(ENTRY_CREATE.name()) && file.exists()) {
                    handleCreateEvent(file);
                } else if (kind.name().equals(ENTRY_DELETE.name())) {
                    handleDeleteEvent();
                } else if (kind.name().equals(ENTRY_MODIFY.name()) && file.exists()) {
                    handleModifyEvent(file);
                }
            }

        }
    } finally {
        this.readLock.unlock();
    }
}
 
開發者ID:hsj-xiaokang,項目名稱:springboot-shiro-cas-mybatis,代碼行數:36,代碼來源:JsonServiceRegistryConfigWatcher.java

示例9: run

import java.nio.file.WatchEvent; //導入方法依賴的package包/類
@Override
public void run() {
    WatchService watchService = WatchServiceUtil.watchModify(confDir);
    WatchKey key;
    while (watchService != null){
        try {
            key = watchService.take();
            for (WatchEvent<?> watchEvent : key.pollEvents()) {
                if(watchEvent.kind() == ENTRY_MODIFY){
                    String fileName = watchEvent.context() == null ? "" : watchEvent.context().toString();
                    if("authorization.properties".equals(fileName)){
                        log.info("檢測到授權文件authorization.properties有改動,正在重新配置相關插件配置");
                        Set<Plugin> plugins = PluginLibraryHelper.getPluginsAboutAuthorization();
                        for (Plugin plugin : plugins) {
                            plugin.init(PluginLibraryHelper.getPluginConfig(plugin));
                            log.info("已完成插件{}的配置重新加載",plugin.pluginName());
                        }
                    }
                }
            }
            key.reset();
        } catch (Exception e) {
            log.error("插件配置文件監聽異常",e);
            break;
        }
    }
}
 
開發者ID:DevopsJK,項目名稱:SuitAgent,代碼行數:28,代碼來源:ConfDirWatcher.java

示例10: handleEvent

import java.nio.file.WatchEvent; //導入方法依賴的package包/類
public void handleEvent ( final Path watchable, final WatchEvent<?> event ) throws IOException
{
    final Path path = (Path)event.context ();
    logger.debug ( "Change {} for base: {} on {}", new Object[] { event.kind (), watchable, path } );

    if ( watchable.endsWith ( "native" ) && path.toString ().equals ( "settings.xml" ) )
    {
        if ( event.kind () != StandardWatchEventKinds.ENTRY_DELETE )
        {
            check ();
        }
        else
        {
            storageRemoved ();
        }
    }
    else
    {
        if ( path.toString ().equals ( "settings.xml" ) )
        {
            if ( event.kind () == StandardWatchEventKinds.ENTRY_CREATE )
            {
                this.nativeKey = new File ( watchable.toFile (), "native" ).toPath ().register ( this.watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE );
            }
        }
        else if ( path.toString ().endsWith ( ".hds" ) )
        {
            if ( this.id != null )
            {
                this.storageManager.fileChanged ( this.path.toFile (), this.id, path.toFile () );
            }
        }
    }
}
 
開發者ID:eclipse,項目名稱:neoscada,代碼行數:35,代碼來源:BaseWatcher.java

示例11: checkBaseEvents

import java.nio.file.WatchEvent; //導入方法依賴的package包/類
private void checkBaseEvents ( final WatchKey key, final List<WatchEvent<?>> events )
{
    for ( final WatchEvent<?> event : events )
    {
        if ( ! ( event.context () instanceof Path ) )
        {
            continue;
        }

        final Path path = this.base.resolve ( (Path)event.context () );

        logger.debug ( "Event for {}, {} : {} -> {}", new Object[] { event.context (), key.watchable (), event.kind (), path } );

        if ( event.kind () == StandardWatchEventKinds.ENTRY_DELETE )
        {
            // check delete
            checkDeleteStorage ( path );
        }
        else
        {
            try
            {
                checkAddStorage ( path );
            }
            catch ( final IOException e )
            {
                logger.warn ( "Failed to check for storage", e );
            }
        }
    }
}
 
開發者ID:eclipse,項目名稱:neoscada,代碼行數:32,代碼來源:BaseWatcher.java

示例12: processFileSystemChanges

import java.nio.file.WatchEvent; //導入方法依賴的package包/類
private void processFileSystemChanges(final Path watchedDirectory, final WatchKey key) {
    final ChangesProcessor processor = getChangesProcessorOrNull(watchedDirectory);

    if (processor == null) {
        log.warn("Ignoring change in {}: no change processor found", watchedDirectory);
        return;
    }

    processor.start();

    for (WatchEvent<?> event : key.pollEvents()) {
        final WatchEvent.Kind<?> kind = event.kind();
        final Object eventContext = event.context();

        log.debug("Processing {} {} in {}", kind.name(), eventContext, watchedDirectory);

        if (kind == StandardWatchEventKinds.OVERFLOW) {
            log.info("event overflow in {}. Reimporting and registering watchedDirectory '{}' to avoid half synced state",
                    watchedDirectory, watchedDirectory);
            if (Files.exists(watchedDirectory)) {
                registerQuietly(watchedDirectory);
            }
            processor.processChange(kind, watchedDirectory, true);
        } else {
            final Path changedRelPath = (Path) eventContext;
            final Path changedAbsPath = watchedDirectory.resolve(changedRelPath);
            final boolean isDirectory = isDirectory(changedAbsPath, kind);
            if (watchedFiles.matches(changedAbsPath, isDirectory)) {
                if (isDirectory && kind == StandardWatchEventKinds.ENTRY_CREATE) {
                    registerQuietly(changedAbsPath);
                }
                processor.processChange(kind, changedAbsPath, isDirectory);
            } else {
                log.debug("Skipping excluded path {}", changedAbsPath);
            }
        }
    }
}
 
開發者ID:openweb-nl,項目名稱:hippo-groovy-updater,代碼行數:39,代碼來源:FileSystemWatcher.java

示例13: signalEvent

import java.nio.file.WatchEvent; //導入方法依賴的package包/類
/**
 * Adds the event to this key and signals it.
 *
 * @param kind    event kind
 * @param context context
 */
@SuppressWarnings("unchecked")
final void signalEvent(WatchEvent.Kind<?> kind, Object context) {
    synchronized(this) {
        int size = events.size();
        if(size > 1) {
            // don't let list get too big
            if(size >= MAX_EVENT_LIST_SIZE) {
                kind = StandardWatchEventKinds.OVERFLOW;
                context = null;
            }

            // repeated event
            WatchEvent<?> prev = events.get(size - 1);
            if(kind == prev.kind()) {
                boolean isRepeat;
                if(context == null) {
                    isRepeat = (prev.context() == null);
                }
                else {
                    isRepeat = context.equals(prev.context());
                }
                if(isRepeat) {
                    ((Event<?>) prev).increment();
                    return;
                }
            }
        }

        // non-repeated event
        events.add(new Event<Object>((WatchEvent.Kind<Object>) kind, context));
        signal();
    }
}
 
開發者ID:iterate-ch,項目名稱:cyberduck,代碼行數:40,代碼來源:AbstractWatchKey.java

示例14: pollConfigFileForChanges

import java.nio.file.WatchEvent; //導入方法依賴的package包/類
/**
 * Polls the configuration file for changes.
 */
private void pollConfigFileForChanges() {

	WatchKey key = watcher.poll();
	try {
		if (key == null) {
			return;
		}

		for (WatchEvent<?> event : key.pollEvents()) {
			Kind<?> kind = event.kind();
			if (StandardWatchEventKinds.OVERFLOW == kind) {
				continue;
			}

			// The filename is the context of the event.
			Path filename = (Path) event.context();

			if (configFile.endsWith(filename.toString())) {
				LOG.info("CORS Filter: Detected change in " + configFile + " , configuration reload required");
				reloadRequired = true;
			}
		}
	} finally {
		if (key != null) {
			key.reset();
		}
	}
}
 
開發者ID:sdcuike,項目名稱:cors-filter,代碼行數:32,代碼來源:CORSConfigurationFileWatcher.java

示例15: call

import java.nio.file.WatchEvent; //導入方法依賴的package包/類
@Override
public Void call() throws IOException, InterruptedException {
    this.logger.info(String.format("Start watching %s", this.source));

    final WatchService watchService = this.source.getFileSystem().newWatchService();
    final WatchKey expected = this.source.getParent().register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);

    for (; ; ) {
        WatchKey actual = watchService.take();

        if (!actual.equals(expected)) {
            this.logger.warning(String.format("Unknown watch key: %s", actual));
            continue;
        }

        for (WatchEvent<?> watchEvent : actual.pollEvents()) {
            Path changed = (Path) watchEvent.context();

            if (!this.source.getFileName().equals(changed)) {
                this.logger.fine(String.format("Discarding unimportant file change: %s", changed));
                continue;
            }

            this.callback.run();
        }

        if (!actual.reset()) {
            this.logger.warning(String.format("Watch key is no longer valid: %s", actual));
            break;
        }
    }

    this.logger.info(String.format("Stop watching %s", this.source));
    return null;
}
 
開發者ID:cloudfoundry,項目名稱:java-buildpack-security-provider,代碼行數:36,代碼來源:FileWatcher.java


注:本文中的java.nio.file.WatchEvent.context方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。