Skip to content

Commit

Permalink
Feature jdk8 grammer exec (#22)
Browse files Browse the repository at this point in the history
* init

* update

* rm some log
  • Loading branch information
sunwu51 authored Apr 29, 2024
1 parent 8f487f6 commit 097a7f2
Show file tree
Hide file tree
Showing 15 changed files with 518 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ build/

### VS Code ###
.vscode/
dependency-reduced-pom.xml
26 changes: 26 additions & 0 deletions dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,32 @@
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>junit-jupiter-api</artifactId>
<groupId>org.junit.jupiter</groupId>
</exclusion>
<exclusion>
<artifactId>junit-jupiter-params</artifactId>
<groupId>org.junit.jupiter</groupId>
</exclusion>
<exclusion>
<artifactId>junit-jupiter-engine</artifactId>
<groupId>org.junit.jupiter</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>1.14.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
Expand Down
13 changes: 12 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,18 @@
<artifactId>jackson-databind</artifactId>
<version>2.13.5</version>
</dependency>
<dependency>
<!-- https://mvnrepository.com/artifact/net.openhft/compiler -->
<dependency>
<groupId>net.openhft</groupId>
<artifactId>compiler</artifactId>
<version>2.25ea2</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>joor-java-8</artifactId>
<version>0.9.14</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
Expand Down
61 changes: 49 additions & 12 deletions src/main/java/w/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
import java.io.*;
import java.lang.instrument.Instrumentation;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import javassist.*;

Expand All @@ -24,10 +20,6 @@ public class App {
private static final int DEFAULT_WEBSOCKET_PORT = 18000;

public static void agentmain(String arg, Instrumentation instrumentation) throws Exception {
if (Global.instrumentation != null) {
Global.debug("Already attached before");
return;
}
if (arg != null && arg.length() > 0) {
String[] items = arg.split("&");
for (String item : items) {
Expand All @@ -49,17 +41,62 @@ public static void agentmain(String arg, Instrumentation instrumentation) throws
startHttpd(DEFAULT_HTTP_PORT);
startWebsocketd(DEFAULT_WEBSOCKET_PORT);

// 3 init execInstance
// 3 start a new compiler server
startCompiler();

// 4 init execInstance
initExecInstance();

// 4 task to clean closed ws and related enhancer
// 5 task to clean closed ws and related enhancer
schedule();
}
//
// public static void premain(String arg, Instrumentation instrumentation) throws Exception {
// agentmain(arg, instrumentation);
// }

public static void startCompiler() {

Global.unpackUberJar(Global.getClassLoader());
String javaHome = System.getProperty("java.home");
String javaBin = javaHome + "/bin/java";
String classpath = "";
for (String cp : Global.getClassPaths()) {
if (!classpath.isEmpty()) {
classpath += File.pathSeparator;
}
classpath += cp;
}
classpath += File.pathSeparator + javaHome + "/../lib/tools.jar" + File.pathSeparator + System.getProperty("java.class.path");
String className = "w.Compiler";
List<String> command = new ArrayList<>();
command.add(javaBin);
command.add("-cp");
command.add(classpath);
command.add("-Duser.language=en");
command.add("-Dfile.encoding=UTF-8");
command.add(className);
Global.info(String.format("%s -cp %s -Duser.language=en -Dfile.encoding=UTF-8 %s", javaBin, classpath, className));
try {
//new jvm process
ProcessBuilder builder = new ProcessBuilder(command);
Process process = builder.inheritIO().start();
Global.info("compiler server started");
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
Global.info("JVM Shutdown Hook: Shutting down child process.");
process.destroy();
try {
process.waitFor();
} catch (InterruptedException e) {
Global.error("Shutdown hook interrupted while waiting for child process to end.");
Thread.currentThread().interrupt();
}
}));
} catch (Exception e) {
Global.error("compiler server start error", e);
}
}

private static void startHttpd(int port) throws IOException {
if (port > 8100) {
System.err.println("Httpd start failed " + port);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/w/Attach.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static void main(String[] args) throws Exception {
return;
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
} else {
Global.error(jdkVersion + " is not supported");
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/w/Compiler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package w;

import fi.iki.elonen.NanoHTTPD;
import w.core.InMemoryJavaCompiler;
import w.util.PrintUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

import static fi.iki.elonen.NanoHTTPD.Response.Status.BAD_REQUEST;

public class Compiler {
public static final int port = 13019;

public static void main(String[] args) throws IOException {
try {
Class.forName("com.sun.tools.javac.processing.JavacProcessingEnvironment");
} catch (ClassNotFoundException e) {
System.err.println("Please use jdk instead of jre to start a compiler server");
System.exit(-1);
}
NanoHTTPD nanoHTTPD = new NanoHTTPD(port) {
@Override
public Response serve(String uri, Method method,
Map<String, String> header, Map<String, String> parameters,
Map<String, String> files) {
if (method == Method.POST) {
switch (uri) {
case "/compile":
String className = parameters.get("className");

String content = parameters.get("content");
content = new String(Base64.getDecoder().decode(parameters.get("content")), StandardCharsets.UTF_8);
try {
StringBuilder errorMessage = new StringBuilder();
byte[] bytecode = InMemoryJavaCompiler.compile(className, content, new InMemoryJavaCompiler.InMemoryDiagnosticListener(errorMessage));
Map<String, Object> _m = new HashMap<>();
if (bytecode != null) {
_m.put("code", 0);
_m.put("data", Base64.getEncoder().encodeToString(bytecode) );
String json = PrintUtils.getObjectMapper().writeValueAsString(_m);
return newFixedLengthResponse(json);
} else {
_m.put("code", 1);
_m.put("data", errorMessage.toString());
String json = PrintUtils.getObjectMapper().writeValueAsString(_m);
return newFixedLengthResponse(json);
}
} catch (Exception e) {
return newFixedLengthResponse(BAD_REQUEST, "", "NOT SUPPORT");
}
}
}

return newFixedLengthResponse(BAD_REQUEST, "", "NOT SUPPORT");
}
};
nanoHTTPD.start(5000, false);
}
}


Loading

0 comments on commit 097a7f2

Please sign in to comment.