Skip to content
Snippets Groups Projects
Commit aa3fd16c authored by Joel Cavat's avatar Joel Cavat
Browse files

Use better abstraction for code

parent a37dac39
No related branches found
No related tags found
No related merge requests found
*.class
.idea
target
*.pyc
*.iml
\.DS_Store
doc/
......
......@@ -31,8 +31,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
......
import io.javalin.Javalin;
import org.json.JSONObject;
import java.io.*;
import java.util.Map;
import java.util.stream.Collectors;
import static java.lang.System.out;
interface ExecutableCode {
Map<String, Serializable> execute();
}
interface DockerExecutableCode extends ExecutableCode {
String command();
Map<String, String> filesToExecute();
default void writeFile(String filename, String content) {
File file = new File(filename);
try (PrintWriter writer = new PrintWriter( new FileWriter(file, true) )) {
writer.write(content);
} catch (IOException e) {
e.printStackTrace();
}
}
default void writeFiles(Map<String, String> files) {
files.forEach( this::writeFile );
}
default void deleteFiles(Map<String, String> files) {
files.keySet().forEach( filename -> new File(filename).delete() );
}
default Map<String, Serializable> executeFiles() {
Process cmdProc;
long elapsed = 0;
try {
long start = System.currentTimeMillis();
cmdProc = Runtime.getRuntime().exec( command() );
cmdProc.waitFor();
elapsed = System.currentTimeMillis() - start;
} catch (IOException | InterruptedException e) {
e.printStackTrace();
return Map.of("error", e.toString(), "exit", 1, "elapsed", elapsed);
}
String in = new BufferedReader( new InputStreamReader( cmdProc.getInputStream() )).lines().collect(Collectors.joining());
String err = new BufferedReader( new InputStreamReader( cmdProc.getErrorStream() )).lines().collect(Collectors.joining());
return Map.of("exit", err.isEmpty(), "output", in, "error", err, "time", elapsed);
}
default Map<String, Serializable> execute() {
writeFiles( filesToExecute() );
Map<String, Serializable> res = executeFiles();
deleteFiles( filesToExecute() );
return res;
}
}
class JavaDockerCode implements DockerExecutableCode {
private Map<String, String> filesWithContent;
public JavaDockerCode(Map<String, String> filesWithContent) {
this.filesWithContent = filesWithContent;
}
public String command() {
return "docker run --rm --mount type=bind,source=" + System.getProperty("user.dir") + "/share_docker_file,dst=/env/ freakency/java:1.0 ./java_test.sh";
}
public Map<String, String> filesToExecute() {
return this.filesWithContent;
}
}
class PythonDockerCode implements DockerExecutableCode {
private Map<String, String> filesWithContent;
public PythonDockerCode(Map<String, String> filesWithContent) {
this.filesWithContent = filesWithContent;
}
public String command() {
return "docker run --rm --mount type=bind,source=" + System.getProperty("user.dir") + "/share_docker_file,dst=/env/ freakency/python:3.0 python assert.py";
}
public Map<String, String> filesToExecute() {
return this.filesWithContent;
}
}
public class App {
public static void main(String[] args) {
Javalin app = Javalin.create().enableCorsForAllOrigins().start(6999);
CompilationService cpl = new DockerCompilation();
// CompilationService compilationService = new DockerCompilation();
app.post("/", ctx -> {
ctx.json(cpl.executeKata(new JSONObject(ctx.body())));
JSONObject res = new JSONObject( ctx.body() );
String language = res.getString("language");
String userCode = res.getString("stream");
String assertion = res.getString("assert");
ExecutableCode ec = language.equals("java") ?
new JavaDockerCode(Map.of("share_docker_file/Kata.java", userCode, "share_docker_file/Main.java", assertion)) :
new PythonDockerCode(Map.of("share_docker_file/sample.py", userCode, "share_docker_file/assert.py", assertion));
out.println(language);
out.println(userCode);
out.println(assertion);
ctx.json(ec.execute());
});
}
}
import org.json.JSONObject;
import java.io.Serializable;
import java.util.Map;
public interface CompilationService {
Map<String, Serializable> executeKata(JSONObject input);
Map<String, Serializable> executeKata(String language, String userCode, String assertion);
}
......@@ -26,16 +26,18 @@ public class DockerCompilation implements CompilationService {
this.filenameDic.put("java", java);
}
public Map<String, Serializable> executeKata(JSONObject input) {
public Map<String, Serializable> executeKata(String language, String userCode, String assertion) {
String line, output = "", error = "";
String line;
String output = "";
String error = "";
Map<String, String> language = this.filenameDic.get(input.get("language").toString());
Map<String, String> languages = this.filenameDic.get(language);
try (PrintWriter filenameWriter = new PrintWriter(language.get("filename"), "UTF-8"); PrintWriter filenameTestWriter = new PrintWriter(language.get("test"), "UTF-8")) {
filenameWriter.println(input.get("stream"));
filenameTestWriter.println(input.get("assert"));
try (PrintWriter filenameWriter = new PrintWriter(languages.get("filename"), "UTF-8"); PrintWriter filenameTestWriter = new PrintWriter(languages.get("test"), "UTF-8")) {
filenameWriter.println(userCode);
filenameTestWriter.println(assertion);
} catch (FileNotFoundException | UnsupportedEncodingException e) {
e.printStackTrace();
}
......@@ -45,7 +47,7 @@ public class DockerCompilation implements CompilationService {
try {
start = System.currentTimeMillis();
cmdProc = Runtime.getRuntime().exec(language.get("cmd"));
cmdProc = Runtime.getRuntime().exec(languages.get("cmd"));
cmdProc.waitFor();
elapsed = System.currentTimeMillis() - start;
......@@ -72,19 +74,19 @@ public class DockerCompilation implements CompilationService {
Map<String, Serializable> json = new HashMap<>();
//exit value of the ran program
if (!error.equals(""))
json.put("exit", 1);
else
if (error.equals(""))
json.put("exit", 0);
else
json.put("exit", 1);
json.put("output", output);
json.put("error", error);
json.put("time", elapsed);
// For delete purpose, create a File object also targeting sample.py
File file = new File(language.get("filename"));
File file = new File(languages.get("filename"));
file.delete();
file = new File(language.get("test"));
file = new File(languages.get("test"));
file.delete();
return json;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment