Issue #5830 Remove com.sun.nio.file import. (#5838)

* Issue #5830 Remove native classes from PathWatcher.

Removed use of com.sun.nio.file.SensitivityWatchEventModifier, no longer needed.
This has the desirable side-effect of getting rid of com.sun.nio.file package imports.

Signed-off-by: Jan Bartel <janb@webtide.com>
This commit is contained in:
Jan Bartel 2021-01-12 11:30:04 +01:00 committed by GitHub
parent 6e1cd862e4
commit 1a7c3deb09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 66 deletions

View File

@ -20,7 +20,6 @@ package org.eclipse.jetty.util;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.file.ClosedWatchServiceException; import java.nio.file.ClosedWatchServiceException;
import java.nio.file.FileSystem; import java.nio.file.FileSystem;
import java.nio.file.FileSystems; import java.nio.file.FileSystems;
@ -31,11 +30,9 @@ import java.nio.file.WatchEvent;
import java.nio.file.WatchKey; import java.nio.file.WatchKey;
import java.nio.file.WatchService; import java.nio.file.WatchService;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.EventListener; import java.util.EventListener;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@ -739,8 +736,6 @@ public class PathWatcher extends AbstractLifeCycle implements Runnable
private static final WatchEvent.Kind<?>[] WATCH_DIR_KINDS = {ENTRY_CREATE, ENTRY_DELETE}; private static final WatchEvent.Kind<?>[] WATCH_DIR_KINDS = {ENTRY_CREATE, ENTRY_DELETE};
private WatchService watchService; private WatchService watchService;
private WatchEvent.Modifier[] watchModifiers;
private boolean nativeWatchService;
private final List<Config> configs = new ArrayList<>(); private final List<Config> configs = new ArrayList<>();
private final Map<WatchKey, Config> keys = new ConcurrentHashMap<>(); private final Map<WatchKey, Config> keys = new ConcurrentHashMap<>();
@ -874,7 +869,7 @@ public class PathWatcher extends AbstractLifeCycle implements Runnable
protected void doStart() throws Exception protected void doStart() throws Exception
{ {
//create a new watchservice //create a new watchservice
createWatchService(); this.watchService = FileSystems.getDefault().newWatchService();
//ensure setting of quiet time is appropriate now we have a watcher //ensure setting of quiet time is appropriate now we have a watcher
setUpdateQuietTime(getUpdateQuietTimeMillis(), TimeUnit.MILLISECONDS); setUpdateQuietTime(getUpdateQuietTimeMillis(), TimeUnit.MILLISECONDS);
@ -927,45 +922,6 @@ public class PathWatcher extends AbstractLifeCycle implements Runnable
listeners.clear(); listeners.clear();
} }
/**
* Create a fresh WatchService and determine if it is a
* native implementation or not.
*/
private void createWatchService() throws IOException
{
//create a watch service
this.watchService = FileSystems.getDefault().newWatchService();
WatchEvent.Modifier[] modifiers = null;
boolean nativeService = true;
// Try to determine native behavior
// See http://stackoverflow.com/questions/9588737/is-java-7-watchservice-slow-for-anyone-else
try
{
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Class<?> pollingWatchServiceClass = Class.forName("sun.nio.fs.PollingWatchService", false, cl);
if (pollingWatchServiceClass.isAssignableFrom(this.watchService.getClass()))
{
nativeService = false;
LOG.info("Using Non-Native Java {}", pollingWatchServiceClass.getName());
Class<?> c = Class.forName("com.sun.nio.file.SensitivityWatchEventModifier");
Field f = c.getField("HIGH");
modifiers = new WatchEvent.Modifier[]
{
(WatchEvent.Modifier)f.get(c)
};
}
}
catch (Throwable t)
{
// Unknown JVM environment, assuming native.
LOG.ignore(t);
}
this.watchModifiers = modifiers;
this.nativeWatchService = nativeService;
}
/** /**
* Check to see if the watcher is in a state where it should generate * Check to see if the watcher is in a state where it should generate
* watch events to the listeners. Used to determine if watcher should generate * watch events to the listeners. Used to determine if watcher should generate
@ -1068,25 +1024,16 @@ public class PathWatcher extends AbstractLifeCycle implements Runnable
protected void register(Path path, Config config) throws IOException protected void register(Path path, Config config) throws IOException
{ {
if (LOG.isDebugEnabled()) if (LOG.isDebugEnabled())
LOG.debug("Registering watch on {} {}", path, watchModifiers == null ? null : Arrays.asList(watchModifiers)); LOG.debug("Registering watch on {}", path);
register(path, config, WATCH_EVENT_KINDS); register(path, config, WATCH_EVENT_KINDS);
} }
private void register(Path path, Config config, WatchEvent.Kind<?>[] kinds) throws IOException private void register(Path path, Config config, WatchEvent.Kind<?>[] kinds) throws IOException
{ {
if (watchModifiers != null) // Native Watcher
{ WatchKey key = path.register(watchService, kinds);
// Java Watcher keys.put(key, config);
WatchKey key = path.register(watchService, kinds, watchModifiers);
keys.put(key, config);
}
else
{
// Native Watcher
WatchKey key = path.register(watchService, kinds);
keys.put(key, config);
}
} }
/** /**
@ -1403,14 +1350,6 @@ public class PathWatcher extends AbstractLifeCycle implements Runnable
{ {
long desiredMillis = unit.toMillis(duration); long desiredMillis = unit.toMillis(duration);
if (watchService != null && !this.nativeWatchService && (desiredMillis < 5000))
{
LOG.warn("Quiet Time is too low for non-native WatchService [{}]: {} < 5000 ms (defaulting to 5000 ms)", watchService.getClass().getName(), desiredMillis);
this.updateQuietTimeDuration = 5000;
this.updateQuietTimeUnit = TimeUnit.MILLISECONDS;
return;
}
if (IS_WINDOWS && (desiredMillis < 1000)) if (IS_WINDOWS && (desiredMillis < 1000))
{ {
LOG.warn("Quiet Time is too low for Microsoft Windows: {} < 1000 ms (defaulting to 1000 ms)", desiredMillis); LOG.warn("Quiet Time is too low for Microsoft Windows: {} < 1000 ms (defaulting to 1000 ms)", desiredMillis);