Skip to content

Commit

Permalink
feat: add engine graalium
Browse files Browse the repository at this point in the history
  • Loading branch information
vmutafov committed Jul 12, 2022
1 parent 18799ba commit 4b080ef
Show file tree
Hide file tree
Showing 61 changed files with 2,535 additions and 241 deletions.
6 changes: 6 additions & 0 deletions groups/engine-all/pom.xml
Expand Up @@ -46,6 +46,7 @@
<artifactId>dirigible-engine-javascript-graalvm</artifactId>
<version>7.0.0-SNAPSHOT</version>
</dependency>

<!--
<dependency>
<groupId>org.eclipse.dirigible</groupId>
Expand Down Expand Up @@ -93,6 +94,11 @@
<artifactId>dirigible-engine-odata</artifactId>
<version>7.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.eclipse.dirigible</groupId>
<artifactId>engine-graalium-execution-dirigible-web</artifactId>
<version>7.0.0-SNAPSHOT</version>
</dependency>
</dependencies>

<properties>
Expand Down
Expand Up @@ -31,10 +31,6 @@
*/
public abstract class AbstractExceptionHandler<T extends Throwable> implements ExceptionMapper<T> {

/** The response. */
@Context
private HttpServletResponse response;

/** The Constant GSON. */
private static final Gson GSON = new Gson();

Expand Down
45 changes: 45 additions & 0 deletions modules/engines/engine-graalium/execution-dirigible-web/pom.xml
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>engine-graalium</artifactId>
<groupId>org.eclipse.dirigible</groupId>
<version>7.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>engine-graalium-execution-dirigible-web</artifactId>
<name>Engine - Graalium - Execution - Dirigible - Web</name>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<license.skip>true</license.skip>
</properties>

<dependencies>
<dependency>
<groupId>org.eclipse.dirigible</groupId>
<artifactId>dirigible-api-facade-http</artifactId>
<version>7.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.eclipse.dirigible</groupId>
<artifactId>engine-graalium-execution-dirigible</artifactId>
<version>7.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.github.chrisvest</groupId>
<artifactId>stormpot</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.dirigible</groupId>
<artifactId>dirigible-engine-javascript</artifactId>
<version>7.0.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
@@ -0,0 +1,33 @@
package org.eclipse.dirigible.graalium.web.dirigible;

import org.eclipse.dirigible.engine.js.service.JSWebHandler;
import org.eclipse.dirigible.graalium.core.dirigible.DirigibleJSCodeRunner;
import org.eclipse.dirigible.graalium.core.dirigible.modules.DirigibleSourceProvider;
import org.eclipse.dirigible.api.v3.http.HttpRequestFacade;

import java.io.IOException;
import java.nio.file.Path;

public class GraaliumJSWebHandler implements JSWebHandler {

private final DirigibleSourceProvider dirigibleSourceProvider = new DirigibleSourceProvider();

@Override
public void handleJSRequest(String projectName, String projectFilePath, String projectFilePathParam) {
try {
if (HttpRequestFacade.isValid()) {
HttpRequestFacade.setAttribute(HttpRequestFacade.ATTRIBUTE_REST_RESOURCE_PATH, projectFilePathParam);
}

String maybeJSCode = dirigibleSourceProvider.getSource(projectName, projectFilePath);
if (maybeJSCode == null) {
throw new IOException("JS source for project name '" + projectName + "' and file name '" + projectFilePath + " could not be found");
}

Path jsCodePath = dirigibleSourceProvider.getAbsoluteSourcePath(projectName, projectFilePath);
new DirigibleJSCodeRunner().run(jsCodePath);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
@@ -0,0 +1,134 @@
package org.eclipse.dirigible.graalium.web.dirigible;

import javax.ws.rs.*;
import javax.ws.rs.core.Response;

@Path("/js")
public class GraaliumJSWebService {
private static final String HTTP_PATH_MATCHER = "/{projectName}/{projectFilePath:.*\\.js|.*\\.mjs}";
private static final String HTTP_PATH_WITH_PARAM_MATCHER = "/{projectName}/{projectFilePath:.*\\.js|.*\\.mjs}/{projectFilePathParam}";
private final GraaliumJSWebHandler requestHandler = new GraaliumJSWebHandler();

@GET
@Path(HTTP_PATH_MATCHER)
public Response get(
@PathParam("projectName") String projectName,
@PathParam("projectFilePath") String projectFilePath
) {
return executeJavaScript(projectName, projectFilePath);
}

@GET
@Path(HTTP_PATH_WITH_PARAM_MATCHER)
public Response get(
@PathParam("projectName") String projectName,
@PathParam("projectFilePath") String projectFilePath,
@PathParam("projectFilePathParam") String projectFilePathParam
) {
return executeJavaScript(projectName, projectFilePath, projectFilePathParam);
}

@POST
@Path(HTTP_PATH_MATCHER)
public Response post(
@PathParam("projectName") String projectName,
@PathParam("projectFilePath") String projectFilePath
) {
return executeJavaScript(projectName, projectFilePath);
}

@POST
@Path(HTTP_PATH_WITH_PARAM_MATCHER)
public Response post(
@PathParam("projectName") String projectName,
@PathParam("projectFilePath") String projectFilePath,
@PathParam("projectFilePathParam") String projectFilePathParam
) {
return executeJavaScript(projectName, projectFilePath, projectFilePathParam);
}

@PUT
@Path(HTTP_PATH_MATCHER)
public Response put(
@PathParam("projectName") String projectName,
@PathParam("projectFilePath") String projectFilePath
) {
return executeJavaScript(projectName, projectFilePath);
}

@PUT
@Path(HTTP_PATH_WITH_PARAM_MATCHER)
public Response put(
@PathParam("projectName") String projectName,
@PathParam("projectFilePath") String projectFilePath,
@PathParam("projectFilePathParam") String projectFilePathParam
) {
return executeJavaScript(projectName, projectFilePath, projectFilePathParam);
}

@PATCH
@Path(HTTP_PATH_MATCHER)
public Response patch(
@PathParam("projectName") String projectName,
@PathParam("projectFilePath") String projectFilePath
) {
return executeJavaScript(projectName, projectFilePath);
}

@PATCH
@Path(HTTP_PATH_WITH_PARAM_MATCHER)
public Response patch(
@PathParam("projectName") String projectName,
@PathParam("projectFilePath") String projectFilePath,
@PathParam("projectFilePathParam") String projectFilePathParam
) {
return executeJavaScript(projectName, projectFilePath, projectFilePathParam);
}

@DELETE
@Path(HTTP_PATH_MATCHER)
public Response delete(
@PathParam("projectName") String projectName,
@PathParam("projectFilePath") String projectFilePath
) {
return executeJavaScript(projectName, projectFilePath);
}

@DELETE
@Path(HTTP_PATH_WITH_PARAM_MATCHER)
public Response delete(
@PathParam("projectName") String projectName,
@PathParam("projectFilePath") String projectFilePath,
@PathParam("projectFilePathParam") String projectFilePathParam
) {
return executeJavaScript(projectName, projectFilePath, projectFilePathParam);
}

@HEAD
@Path(HTTP_PATH_MATCHER)
public Response head(
@PathParam("projectName") String projectName,
@PathParam("projectFilePath") String projectFilePath
) {
return executeJavaScript(projectName, projectFilePath);
}

@HEAD
@Path(HTTP_PATH_WITH_PARAM_MATCHER)
public Response head(
@PathParam("projectName") String projectName,
@PathParam("projectFilePath") String projectFilePath,
@PathParam("projectFilePathParam") String projectFilePathParam
) {
return executeJavaScript(projectName, projectFilePath, projectFilePathParam);
}

private Response executeJavaScript(String projectName, String projectFilePath) {
return executeJavaScript(projectName, projectFilePath, "");
}

private Response executeJavaScript(String projectName, String projectFilePath, String projectFilePathParam) {
requestHandler.handleJSRequest(projectName, projectFilePath, projectFilePathParam);
return Response.ok().build();
}
}
47 changes: 47 additions & 0 deletions modules/engines/engine-graalium/execution-dirigible/pom.xml
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>engine-graalium</artifactId>
<groupId>org.eclipse.dirigible</groupId>
<version>7.0.0-SNAPSHOT</version>
</parent>

<artifactId>engine-graalium-execution-dirigible</artifactId>
<name>Engine - Graalium - Execution - Dirigible</name>

<properties>
<license.skip>true</license.skip>
</properties>

<dependencies>
<dependency>
<groupId>org.eclipse.dirigible</groupId>
<artifactId>dirigible-commons-api</artifactId>
<version>7.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.eclipse.dirigible</groupId>
<artifactId>dirigible-repository-api</artifactId>
<version>7.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.dirigible</groupId>
<artifactId>engine-graalium-execution</artifactId>
<version>7.0.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
@@ -0,0 +1,61 @@
package org.eclipse.dirigible.graalium.core.dirigible;

import org.eclipse.dirigible.commons.config.Configuration;
import org.eclipse.dirigible.commons.config.StaticObjects;
import org.eclipse.dirigible.repository.api.IRepository;
import org.eclipse.dirigible.repository.api.IRepositoryStructure;
import org.eclipse.dirigible.graalium.core.dirigible.globals.DirigibleContextGlobalObject;
import org.eclipse.dirigible.graalium.core.dirigible.globals.DirigibleEngineTypeGlobalObject;
import org.eclipse.dirigible.graalium.core.dirigible.modules.DirigibleModuleResolver;
import org.eclipse.dirigible.graalium.core.dirigible.polyfills.RequirePolyfill;
import org.eclipse.dirigible.graalium.core.javascript.GraalJSCodeRunner;
import org.eclipse.dirigible.graalium.core.javascript.JSCodeRunner;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;

import java.nio.file.Path;
import java.util.HashMap;

public class DirigibleJSCodeRunner implements JSCodeRunner<Source, Value> {

private final JSCodeRunner<Source, Value> codeRunner;

public DirigibleJSCodeRunner() {
var workingDirectoryPath = getDirigibleWorkingDirectory();
var cachePath = workingDirectoryPath.resolve("caches");
var coreModulesESMProxiesCachePath = cachePath.resolve("core-modules-proxies-cache");

codeRunner = GraalJSCodeRunner.newBuilder(workingDirectoryPath, cachePath)
.addJSPolyfill(new RequirePolyfill())
.addGlobalObject(new DirigibleContextGlobalObject(new HashMap<>()))
.addGlobalObject(new DirigibleEngineTypeGlobalObject())
.addModuleResolver(new DirigibleModuleResolver(coreModulesESMProxiesCachePath))
.waitForDebugger(DirigibleJSCodeRunner::shouldEnableDebug)
.build();
}

private static boolean shouldEnableDebug() {
return Configuration.get("DIRIGIBLE_GRAALIUM_ENABLE_DEBUG", Boolean.FALSE.toString()).equals(Boolean.TRUE.toString());
}

private Path getDirigibleWorkingDirectory() {
var repository = (IRepository) StaticObjects.get(StaticObjects.REPOSITORY);
String publicRegistryPath = repository.getInternalResourcePath(IRepositoryStructure.PATH_REGISTRY_PUBLIC);
return Path.of(publicRegistryPath);
}

@Override
public Value run(Path codeFilePath) {
return codeRunner.run(codeFilePath);
}

@Override
public Value run(Source codeSource) {
return codeRunner.run(codeSource);
}

@Override
public void close() {
codeRunner.close();
}
}
@@ -0,0 +1,25 @@
package org.eclipse.dirigible.graalium.core.dirigible.globals;

import org.eclipse.dirigible.graalium.core.graal.globals.JSGlobalObject;

import java.util.HashMap;
import java.util.Map;

public class DirigibleContextGlobalObject implements JSGlobalObject {

private final Map<Object, Object> dirigibleContextValue;

public DirigibleContextGlobalObject(Map<Object, Object> dirigibleContextValue) {
this.dirigibleContextValue = dirigibleContextValue != null ? dirigibleContextValue : new HashMap<>();
}

@Override
public String getName() {
return "__context";
}

@Override
public Object getValue() {
return dirigibleContextValue;
}
}
@@ -0,0 +1,15 @@
package org.eclipse.dirigible.graalium.core.dirigible.globals;

import org.eclipse.dirigible.graalium.core.graal.globals.JSGlobalObject;

public class DirigibleEngineTypeGlobalObject implements JSGlobalObject {
@Override
public String getName() {
return "__engine";
}

@Override
public Object getValue() {
return "graalium";
}
}

0 comments on commit 4b080ef

Please sign in to comment.