日期:2014-05-16 浏览次数:20700 次
public class JSEngine {
// Declaration of engine, compliable and invocable ...
static {
// Initialize of engine, compliable and invocable ...
}
public static void excuteFiles(String dir) throws ScriptException,
IOException {
for (File file : new File(dir).listFiles()) {
excuteFile(file);
}
}
public static void excuteFile(File file) throws ScriptException,
IOException {
FileReader reader = new FileReader(file);
compliable.compile(reader).eval();
reader.close();
}
// Defination of invoke ...
}
package purejs;
import static java.nio.file.StandardWatchEventKinds.*;
import java.io.*;
import java.nio.file.*;
public class FileChangeMonitor {
public interface FileChangeListener {
public void fileChanged(File file);
}
public static void monit(final String dir,
final FileChangeListener listener) {
new Thread() {
public void run() {
Path path = new File(dir).toPath();
WatchService watcher;
try {
watcher = FileSystems.getDefault().newWatchService();
path.register(watcher, ENTRY_CREATE, ENTRY_MODIFY);
} catch (IOException e) {
e.printStackTrace();
return;
}
while (true) {
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException _) {
return;
}
processKey(path, key, listener);
}
};
}.start();
}
private static void processKey(Path path, WatchKey key,
FileChangeListener listener) {
for (WatchEvent<?> event : key.pollEvents()) {
if (event.kind() == OVERFLOW) {
continue;
}
Path fileName = (Path) event.context();
File file = path.resolve(fileName).toFile();
listener.fileChanged(file);
}
key.reset();
}
}
public class JSServer {
public static void main(String[] args) throws Exception {
loadScripts();
startServer();
}
private static void loadScripts() throws ScriptException, IOException {
JSEngine.excuteFiles("scripts");
FileChangeMonitor.monit("scripts", new FileChangeListener() {