Permalink
Browse files

APPNG-2213 make sure urlrewrite.xml is being watched

  • Loading branch information...
madness-inc committed Oct 22, 2018
1 parent 07b5930 commit a259b9563ab818c8a00d28570c2f4775415e36e4
@@ -16,6 +16,7 @@
package org.appng.core.controller;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.StandardWatchEventKinds;
@@ -64,6 +65,7 @@
private WatchService watcher;
private boolean needsToBeWatched = false;
private Map<String, List<String>> forwardMap;
protected Long forwardsUpdatedAt = null;
private String wwwDir;
@@ -99,15 +101,19 @@ void init(Ehcache cache, String wwwDir, File configFile, String ruleSourceSuffix
this.configFile = configFile;
this.ruleSourceSuffix = ruleSourceSuffix;
readUrlRewrites(configFile);
watch(configFile.getParentFile().toPath());
for (String docDir : documentDirs) {
Path path = new File(wwwDir, docDir).toPath();
LOG.info("watching {}", path.toString());
path.register(watcher, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
watch(new File(wwwDir, docDir).toPath());
}
}
private void watch(Path path) throws IOException {
LOG.info("watching {}", path.toString());
path.register(watcher, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
}
public void run() {
LOG.info("start watching...");
for (;;) {
@@ -124,21 +130,23 @@ public void run() {
}
Path eventPath = (Path) key.watchable();
File absoluteFile = new File(eventPath.toFile(), ((Path) event.context()).toString());
LOG.debug("received event {} for {}", event.kind(), absoluteFile);
if (absoluteFile.equals(configFile)) {
readUrlRewrites(absoluteFile);
} else {
String absolutePath = FilenameUtils.normalize(absoluteFile.getPath(), true);
String relativePathName = absolutePath.substring(wwwDir.length());
if (relativePathName.endsWith(jspExtension)) {
relativePathName = relativePathName.substring(0,
relativePathName.length() - jspExtension.length());
}
removeFromCache(relativePathName);
if (forwardMap.containsKey(relativePathName)) {
forwardMap.get(relativePathName).forEach(path -> removeFromCache(path));
}
LOG.debug("processed event {} for {} ins {}ms", event.kind(), relativePathName,
System.currentTimeMillis() - start);
}
LOG.debug("received event {} for {}", event.kind(), absoluteFile);
String absolutePath = FilenameUtils.normalize(absoluteFile.getPath(), true);
String relativePathName = absolutePath.substring(wwwDir.length());
if (relativePathName.endsWith(jspExtension)) {
relativePathName = relativePathName.substring(0, relativePathName.length() - jspExtension.length());
}
removeFromCache(relativePathName);
if (forwardMap.containsKey(relativePathName)) {
forwardMap.get(relativePathName).forEach(path -> removeFromCache(path));
}
LOG.debug("processed event {} for {} ins {}ms", event.kind(), relativePathName,
System.currentTimeMillis() - start);
}
boolean valid = key.reset();
if (!valid) {
@@ -191,6 +199,7 @@ private void readUrlRewrites(File configFile) {
}
LOG.info("{} has been read, {} forward rules have been processed", configFile.getAbsolutePath(),
forwardRules.getLength());
forwardsUpdatedAt = System.currentTimeMillis();
} catch (Exception e) {
LOG.error(String.format("error reading %s", configFile.getAbsolutePath()), e);
}
@@ -63,19 +63,22 @@ public void test() throws Exception {
Assert.assertEquals(4, ehcache.getSize());
repositoryWatcher.init(ehcache, rootDir, new File(urlrewrite), RepositoryWatcher.DEFAULT_RULE_SUFFIX,
Arrays.asList("de"));
Long forwardsUpdatedAt = repositoryWatcher.forwardsUpdatedAt;
ThreadFactory tf = new ThreadFactoryBuilder().setNameFormat("repositoryWatcher").setDaemon(true).build();
ExecutorService executor = Executors.newSingleThreadExecutor(tf);
executor.execute(repositoryWatcher);
FileUtils.touch(new File(rootDir, fehlerJsp));
FileUtils.touch(new File(rootDir, testJsp));
while (ehcache.getSize() != 0) {
FileUtils.touch(new File(urlrewrite));
while (ehcache.getSize() != 0 || forwardsUpdatedAt == repositoryWatcher.forwardsUpdatedAt) {
Thread.sleep(100);
}
Assert.assertNull(ehcache.get(keyFehlerJsp));
Assert.assertNull(ehcache.get(keyTestJsp));
Assert.assertNull(ehcache.get("GET/de/error"));
Assert.assertNull(ehcache.get("GET/de/fault"));
Assert.assertEquals(0, ehcache.getSize());
Assert.assertTrue(repositoryWatcher.forwardsUpdatedAt > forwardsUpdatedAt);
}
}

0 comments on commit a259b95

Please sign in to comment.