Reorganize compiler for more flexibility.
[jagi.git] / src / jagi / fs / JavaHandler.java
index 111264f..6579097 100644 (file)
@@ -9,7 +9,7 @@ import java.nio.file.*;
 
 public class JavaHandler implements Function<Map<Object, Object>, Map<Object, Object>> {
     private static final Logger log = Logger.getLogger("jagi-fs");
-    private final Map<ClassLoader, Function<Map<Object, Object>, Map<Object, Object>>> handlers = new WeakHashMap<>();
+    private final Map<Compiler.Module, Function<Map<Object, Object>, Map<Object, Object>>> handlers = new WeakHashMap<>();
 
     public static class HandlerException extends RuntimeException {
        public final Path file;
@@ -31,7 +31,7 @@ public class JavaHandler implements Function<Map<Object, Object>, Map<Object, Ob
     private static Function<Map<Object, Object>, Map<Object, Object>> makehandler(Compiler.Module mod) {
        Class<?> main;
        try {
-           main = mod.code().loadClass("Main");
+           main = mod.code.loadClass("Main");
        } catch(ClassNotFoundException e) {
            throw(new HandlerException(mod.file, "no Main class"));
        }
@@ -65,30 +65,30 @@ public class JavaHandler implements Function<Map<Object, Object>, Map<Object, Ob
        throw(new HandlerException(mod.file, "no wmain and not directly applicable"));
     }
 
-    private Function<Map<Object, Object>, Map<Object, Object>> gethandler(Compiler.Module mod) {
-       ClassLoader code = mod.code();
+    private Function<Map<Object, Object>, Map<Object, Object>> gethandler(Compiler.File file) {
+       Compiler.Module mod = file.mod();
        synchronized(handlers) {
-           Function<Map<Object, Object>, Map<Object, Object>> ret = handlers.get(code);
+           Function<Map<Object, Object>, Map<Object, Object>> ret = handlers.get(mod);
            if(ret == null)
-               handlers.put(code, ret = makehandler(mod));
+               handlers.put(mod, ret = makehandler(mod));
            return(ret);
        }
     }
 
     public Map<Object, Object> apply(Map<Object, Object> req) {
-       Compiler.Module mod = Compiler.get().module(Paths.get((String)req.get("SCRIPT_FILENAME")));
+       Compiler.File file = Compiler.get().file(Paths.get((String)req.get("SCRIPT_FILENAME")));
        try {
-           mod.update();
+           file.update();
        } catch(Compiler.CompilationException e) {
-           log.warning(String.format("Could not compile %s:\n%s", mod.file, e.messages()));
+           log.warning(String.format("Could not compile %s:\n%s", file.name, e.messages()));
            return(Utils.simpleerror(500, "Internal Error", "Could not load JAGI handler"));
        } catch(Exception e) {
-           log.log(Level.WARNING, String.format("Error occurred when loading %s", mod.file), e);
+           log.log(Level.WARNING, String.format("Error occurred when loading %s", file.name), e);
            return(Utils.simpleerror(500, "Internal Error", "Could not load JAGI handler"));
        }
        Function<Map<Object, Object>, Map<Object, Object>> handler;
        try {
-           handler = gethandler(mod);
+           handler = gethandler(file);
        } catch(HandlerException e) {
            Throwable cause = e.getCause();
            if(cause != null)