本文整理汇总了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;
}
}
}
示例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
);
}
}
}
}
示例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*/ }
}
示例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) {}
}
}
示例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();
}
}
示例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));
}
示例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;
}
示例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;
}
}
}
示例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 () );
}
}
}
}
示例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 );
}
}
}
}
示例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);
}
}
}
}
示例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();
}
}
示例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();
}
}
}
示例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;
}