Skip to content

Commit

Permalink
Fixing PathWatcher and Test
Browse files Browse the repository at this point in the history
Using a trigger file (and event type) in test cases to know
when the watching & capture is truely finished (instead of
using sleep waits)
  • Loading branch information
joakime committed Apr 30, 2015
1 parent c04896c commit a6b3302
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 76 deletions.
120 changes: 76 additions & 44 deletions jetty-util/src/main/java/org/eclipse/jetty/util/PathWatcher.java
Expand Up @@ -62,26 +62,6 @@
*/
public class PathWatcher extends AbstractLifeCycle implements Runnable
{
/**
* Set to true to enable super noisy debug logging
*/
private static final boolean NOISY = false;

private static final boolean IS_WINDOWS;

static
{
String os = System.getProperty("os.name");
if (os == null)
{
IS_WINDOWS = false;
}
else
{
IS_WINDOWS = os.toLowerCase(Locale.ENGLISH).contains("windows");
}
}

public static class Config
{
private static final String PATTERN_SEP;
Expand All @@ -95,7 +75,7 @@ public static class Config
}
PATTERN_SEP = sep;
}

protected final Path dir;
protected int recurseDepth = 0; // 0 means no sub-directories are scanned
protected List<PathMatcher> includes;
Expand Down Expand Up @@ -141,18 +121,18 @@ public void addExclude(final String syntaxAndPattern)
/**
* Add a <code>glob:</code> syntax pattern exclude reference in a directory relative, os neutral, pattern.
* <p>
*
*
* <pre>
* On Linux:
* Config config = new Config(Path("/home/user/example"));
* config.addExcludeGlobRelative("*.war") => "glob:/home/user/example/*.war"
*
*
* On Windows
* Config config = new Config(Path("D:/code/examples"));
* config.addExcludeGlobRelative("*.war") => "glob:D:\\code\\examples\\*.war"
*
*
* </pre>
*
*
* @param pattern
* the pattern, in unixy format, relative to config.dir
*/
Expand Down Expand Up @@ -224,18 +204,18 @@ public void addInclude(String syntaxAndPattern)
/**
* Add a <code>glob:</code> syntax pattern reference in a directory relative, os neutral, pattern.
* <p>
*
*
* <pre>
* On Linux:
* Config config = new Config(Path("/home/user/example"));
* config.addIncludeGlobRelative("*.war") => "glob:/home/user/example/*.war"
*
*
* On Windows
* Config config = new Config(Path("D:/code/examples"));
* config.addIncludeGlobRelative("*.war") => "glob:D:\\code\\examples\\*.war"
*
*
* </pre>
*
*
* @param pattern
* the pattern, in unixy format, relative to config.dir
*/
Expand Down Expand Up @@ -288,12 +268,18 @@ private boolean hasMatch(Path path, List<PathMatcher> matchers)
{
if (matcher.matches(path))
{
if(NOISY) LOG.debug("Matched TRUE on {}",path);
if (NOISY_LOG.isDebugEnabled())
{
NOISY_LOG.debug("Matched TRUE on {}",path);
}
return true;
}
}

if(NOISY) LOG.debug("Matched FALSE on {}",path);
if (NOISY_LOG.isDebugEnabled())
{
NOISY_LOG.debug("Matched FALSE on {}",path);
}
return false;
}

Expand Down Expand Up @@ -353,7 +339,8 @@ public void setRecurseDepth(int depth)
}

/**
* Determine if the provided child directory should be recursed into based on the configured {@link #setRecurseDepth(int)}
* Determine if the provided child directory should be recursed into based on the configured
* {@link #setRecurseDepth(int)}
*
* @param child
* the child directory to test against
Expand All @@ -376,11 +363,14 @@ private String toGlobPattern(Path path, String subPattern)
StringBuilder s = new StringBuilder();
s.append("glob:");

boolean needDelim = false;

// Add root (aka "C:\" for Windows)
if (path.getRoot() != null)
{
for (char c : path.getRoot().toString().toCharArray())
{
if (c == '\\')
if (c != '\\')
{
s.append(PATTERN_SEP);
}
Expand All @@ -390,22 +380,39 @@ private String toGlobPattern(Path path, String subPattern)
}
}
}
else
{
needDelim = true;
}

// Add the individual path segments
for (Path segment : path)
{
if (needDelim)
{
s.append(PATTERN_SEP);
}
s.append(segment);
s.append(PATTERN_SEP);
needDelim = true;
}

for (char c : subPattern.toCharArray())
// Add the sub pattern (if specified)
if ((subPattern != null) && (subPattern.length() > 0))
{
if (c == '/')
if (needDelim)
{
s.append(PATTERN_SEP);
}
else
for (char c : subPattern.toCharArray())
{
s.append(c);
if (c == '/')
{
s.append(PATTERN_SEP);
}
else
{
s.append(c);
}
}
}

Expand Down Expand Up @@ -553,8 +560,8 @@ public int hashCode()
{
final int prime = 31;
int result = 1;
result = (prime * result) + ((path == null)?0:path.hashCode());
result = (prime * result) + ((type == null)?0:type.hashCode());
result = (prime * result) + ((path == null) ? 0 : path.hashCode());
result = (prime * result) + ((type == null) ? 0 : type.hashCode());
return result;
}

Expand All @@ -578,7 +585,26 @@ public static enum PathWatchEventType
UNKNOWN;
}

private static final boolean IS_WINDOWS;

static
{
String os = System.getProperty("os.name");
if (os == null)
{
IS_WINDOWS = false;
}
else
{
IS_WINDOWS = os.toLowerCase(Locale.ENGLISH).contains("windows");
}
}

private static final Logger LOG = Log.getLogger(PathWatcher.class);
/**
* super noisy debug logging
*/
private static final Logger NOISY_LOG = Log.getLogger(PathWatcher.class.getName() + ".Noisy");

@SuppressWarnings("unchecked")
protected static <T> WatchEvent<T> cast(WatchEvent<?> event)
Expand Down Expand Up @@ -813,13 +839,19 @@ public void run()
// Process new events
if (pendingUpdateEvents.isEmpty())
{
if(NOISY) LOG.debug("Waiting for take()");
if (NOISY_LOG.isDebugEnabled())
{
NOISY_LOG.debug("Waiting for take()");
}
// wait for any event
key = watcher.take();
}
else
{
if(NOISY) LOG.debug("Waiting for poll({}, {})",updateQuietTimeDuration,updateQuietTimeUnit);
if (NOISY_LOG.isDebugEnabled())
{
NOISY_LOG.debug("Waiting for poll({}, {})",updateQuietTimeDuration,updateQuietTimeUnit);
}
key = watcher.poll(updateQuietTimeDuration,updateQuietTimeUnit);
if (key == null)
{
Expand Down Expand Up @@ -947,8 +979,8 @@ else if (config.matches(child))
public void setUpdateQuietTime(long duration, TimeUnit unit)
{
long desiredMillis = unit.toMillis(duration);
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);
this.updateQuietTimeDuration = 1000;
Expand Down

0 comments on commit a6b3302

Please sign in to comment.