Skip to content

Commit

Permalink
Issue #5830 Remove com.sun.nio.file import. (#5838)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
janbartel authored Jan 12, 2021
1 parent 6e1cd86 commit 1a7c3de
Showing 1 changed file with 5 additions and 66 deletions.
71 changes: 5 additions & 66 deletions jetty-util/src/main/java/org/eclipse/jetty/util/PathWatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.file.ClosedWatchServiceException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
Expand All @@ -31,11 +30,9 @@
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EventListener;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -739,8 +736,6 @@ protected static <T> WatchEvent<T> cast(WatchEvent<?> event)
private static final WatchEvent.Kind<?>[] WATCH_DIR_KINDS = {ENTRY_CREATE, ENTRY_DELETE};

private WatchService watchService;
private WatchEvent.Modifier[] watchModifiers;
private boolean nativeWatchService;

private final List<Config> configs = new ArrayList<>();
private final Map<WatchKey, Config> keys = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -874,7 +869,7 @@ private void appendConfigId(StringBuilder s)
protected void doStart() throws Exception
{
//create a new watchservice
createWatchService();
this.watchService = FileSystems.getDefault().newWatchService();

//ensure setting of quiet time is appropriate now we have a watcher
setUpdateQuietTime(getUpdateQuietTimeMillis(), TimeUnit.MILLISECONDS);
Expand Down Expand Up @@ -927,45 +922,6 @@ public void reset()
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
* watch events to the listeners. Used to determine if watcher should generate
Expand Down Expand Up @@ -1068,25 +1024,16 @@ private void registerDir(Path path, Config config) throws IOException
protected void register(Path path, Config config) throws IOException
{
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);
}

private void register(Path path, Config config, WatchEvent.Kind<?>[] kinds) throws IOException
{
if (watchModifiers != null)
{
// Java Watcher
WatchKey key = path.register(watchService, kinds, watchModifiers);
keys.put(key, config);
}
else
{
// Native Watcher
WatchKey key = path.register(watchService, kinds);
keys.put(key, config);
}
// Native Watcher
WatchKey key = path.register(watchService, kinds);
keys.put(key, config);
}

/**
Expand Down Expand Up @@ -1403,14 +1350,6 @@ public void setUpdateQuietTime(long duration, TimeUnit unit)
{
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))
{
LOG.warn("Quiet Time is too low for Microsoft Windows: {} < 1000 ms (defaulting to 1000 ms)", desiredMillis);
Expand Down

0 comments on commit 1a7c3de

Please sign in to comment.