Skip to content
This repository has been archived by the owner on Nov 28, 2024. It is now read-only.

Commit

Permalink
reuse parseJDL context as static variable
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangsa committed Apr 24, 2023
1 parent 7405818 commit e1b99e8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 103 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Upstream JDL source code is located under `src/main/jdl-core` folder.
You can bundle `target/classes/io/zenwave360/jhipster/jdl/jdl-parser.js` using:

- `mvn generate-resources` from project root, or
- `npm generate-resources` from `src/main/jdl-core` folder
- `npm run generate-resources` from `src/main/jdl-core` folder

## Custom Extensions

Expand Down
61 changes: 20 additions & 41 deletions src/main/java/io/zenwave360/jhipster/jdl/JDLParser.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
package io.zenwave360.jhipster.jdl;

import static com.oracle.truffle.js.runtime.JSContextOptions.ESM_EVAL_RETURNS_EXPORTS_NAME;

import com.oracle.truffle.js.lang.JavaScriptLanguage;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Engine;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;

import javax.swing.plaf.synth.SynthDesktopIconUI;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -24,45 +16,20 @@

public class JDLParser {

private static Value parseJDL;

public static Map<String, Object> parseJDL(String JDL) throws IOException {
return parseJDL(JDL, Collections.emptyMap());
}

public static Map<String, Object> parseJDL(String JDL, Map<String, String> configuration) throws IOException {
System.setProperty("polyglot.engine.WarnInterpreterOnly", "false");
String jsCode = loadClassPathFile("io/zenwave360/jhipster/jdl/jdl-parser.js");
Source jdlParsedJsSource = Source.newBuilder(JavaScriptLanguage.ID, jsCode, "jdl-parser.js").build();
try (Context context = Context.create(JavaScriptLanguage.ID)) {
context.eval(jdlParsedJsSource);
Value parseJDL = context.getBindings(JavaScriptLanguage.ID).getMember("parseJDL");
Value returned = null;
try {
returned = parseJDL.execute(JDL, configuration);
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
return (Map<String, Object>) copy(returned.as(Map.class));
}
}

public static String jdlToMermaid(String JDL) throws IOException {
return jdlToMermaid(JDL, Collections.emptyMap());
}
public static String jdlToMermaid(String JDL, Map<String, String> configuration) throws IOException {
System.setProperty("polyglot.engine.WarnInterpreterOnly", "false");
String jsCode = loadClassPathFile("io/zenwave360/jhipster/jdl/jdl-parser.js");
Source jdlParsedJsSource = Source.newBuilder(JavaScriptLanguage.ID, jsCode, "jdl-parser.js").build();
try (Context context = Context.create(JavaScriptLanguage.ID)) {
context.eval(jdlParsedJsSource);
Value jdlToMermaid = context.getBindings(JavaScriptLanguage.ID).getMember("jdlToMermaid");
Value returned = null;
try {
returned = jdlToMermaid.execute(JDL, configuration);
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
return returned.asString();
Value returned = null;
try {
returned = getParseJDL().execute(JDL, configuration);
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
return (Map<String, Object>) copy(returned.as(Map.class));
}

private static Object copy(Object source) {
Expand All @@ -75,6 +42,18 @@ private static Object copy(Object source) {
return source;
}

private static Value getParseJDL() throws IOException {
if(parseJDL == null) {
System.setProperty("polyglot.engine.WarnInterpreterOnly", "false");
String jsCode = loadClassPathFile("io/zenwave360/jhipster/jdl/jdl-parser.js");
Source jdlParsedJsSource = Source.newBuilder(JavaScriptLanguage.ID, jsCode, "jdl-parser.js").build();
Context context = Context.create(JavaScriptLanguage.ID);
context.eval(jdlParsedJsSource);
parseJDL = context.getBindings(JavaScriptLanguage.ID).getMember("parseJDL");
}
return parseJDL;
}

private static String loadClassPathFile(String fileName) throws IOException {
try {
return new String(JDLParser.class.getClassLoader().getResourceAsStream(fileName).readAllBytes(), StandardCharsets.UTF_8);
Expand Down
5 changes: 4 additions & 1 deletion src/test/java/io/zenwave360/jhipster/jdl/JDLParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ public void testLoadAllJDLs() throws IOException {
for (String jdlFile : jdlFiles) {
String jdlString = loadClassPathFile(jdlFile);
try {
System.out.println("Parsing " + jdlFile + " ...");
System.out.print("Parsing " + jdlFile + " ...");
long start = System.currentTimeMillis();
Map<String, Object> parsedJDL = JDLParser.parseJDL(jdlString);
long end = System.currentTimeMillis();
System.out.println(" took " + (end - start) + " ms");
Assert.assertNotNull(parsedJDL);
} catch (Exception e) {
exceptions.put(jdlFile, e);
Expand Down
60 changes: 0 additions & 60 deletions src/test/java/io/zenwave360/jhipster/jdl/JDLToMermaidTest.java

This file was deleted.

0 comments on commit e1b99e8

Please sign in to comment.