Skip to content

Commit

Permalink
Support resolving external libraries (#4061)
Browse files Browse the repository at this point in the history
* ABAP - support inline statements

Signed-off-by: Iliyan Velichkov <velichkov.iliyan@gmail.com>

* add comment

Signed-off-by: Iliyan Velichkov <velichkov.iliyan@gmail.com>

* support resolving external libraries

Signed-off-by: Iliyan Velichkov <velichkov.iliyan@gmail.com>

* roolback DatabaseResultSetHelper.java

Signed-off-by: Iliyan Velichkov <velichkov.iliyan@gmail.com>

---------

Signed-off-by: Iliyan Velichkov <velichkov.iliyan@gmail.com>
  • Loading branch information
iliyan-velichkov authored Jun 20, 2024
1 parent b2a0801 commit d9d13ff
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,24 @@
package org.eclipse.dirigible.components.base.spring;

import org.eclipse.dirigible.components.base.tenant.TenantContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
* The Class BeanProvider.
*/
@Component
public class BeanProvider implements ApplicationContextAware {

private static final Logger LOGGER = LoggerFactory.getLogger(BeanProvider.class);

/** The context. */
private static ApplicationContext context;

Expand Down Expand Up @@ -74,4 +81,15 @@ public static <T> T getBean(Class<T> clazz) {
return context.getBean(clazz);
}

public static <T> Optional<T> getOptionalBean(Class<T> clazz) {
if (!isInitialzed()) {
throw new IllegalStateException("Spring is not initialized yet.");
}
try {
return Optional.of(context.getBean(clazz));
} catch (NoSuchBeanDefinitionException ex) {
LOGGER.debug("Missing bean for [{}]", clazz, ex);
return Optional.empty();
}
}
}
7 changes: 6 additions & 1 deletion modules/engines/engine-graalium/execution-core/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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>

Expand All @@ -18,6 +18,11 @@
<groupId>org.eclipse.dirigible</groupId>
<artifactId>dirigible-repository-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.dirigible</groupId>
<artifactId>dirigible-components-core-base</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.dirigible</groupId>
<artifactId>dirigible-engine-graalium-execution</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,32 @@
*/
package org.eclipse.dirigible.graalium.core;

import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.function.Consumer;
import java.util.regex.Pattern;
import org.eclipse.dirigible.commons.config.Configuration;
import org.eclipse.dirigible.commons.config.StaticObjects;
import org.eclipse.dirigible.components.base.spring.BeanProvider;
import org.eclipse.dirigible.graalium.core.globals.DirigibleContextGlobalObject;
import org.eclipse.dirigible.graalium.core.globals.DirigibleEngineTypeGlobalObject;
import org.eclipse.dirigible.graalium.core.javascript.GraalJSCodeRunner;
import org.eclipse.dirigible.graalium.core.javascript.GraalJSInterceptor;
import org.eclipse.dirigible.graalium.core.javascript.modules.Module;
import org.eclipse.dirigible.graalium.core.javascript.modules.ModuleType;
import org.eclipse.dirigible.graalium.core.javascript.modules.java.JavaModuleResolver;
import org.eclipse.dirigible.graalium.core.modules.DirigibleEsmModuleResolver;
import org.eclipse.dirigible.graalium.core.modules.DirigibleGlobalModuleResolver;
import org.eclipse.dirigible.graalium.core.modules.DirigibleModuleResolver;
import org.eclipse.dirigible.graalium.core.modules.DirigibleSourceProvider;
import org.eclipse.dirigible.graalium.core.modules.*;
import org.eclipse.dirigible.graalium.core.polyfills.RequirePolyfill;
import org.eclipse.dirigible.repository.api.IRepository;
import org.eclipse.dirigible.repository.api.IRepositoryStructure;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;

import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.function.Consumer;
import java.util.regex.Pattern;

/**
* The Class DirigibleJavascriptCodeRunner.
*/
Expand All @@ -48,19 +48,14 @@ public class DirigibleJavascriptCodeRunner implements CodeRunner<Source, Value>

/** TypeScript files extension */
private static final String TS_EXT = ".ts";

/** The code runner. */
private final GraalJSCodeRunner codeRunner;

private final JavascriptSourceProvider sourceProvider;

/** The Constant DIRIGIBLE_JAVASCRIPT_HOOKS_PROVIDERS. */
private static final ServiceLoader<DirigibleJavascriptHooksProvider> DIRIGIBLE_JAVASCRIPT_HOOKS_PROVIDERS =
ServiceLoader.load(DirigibleJavascriptHooksProvider.class);

/** The Constant DIRIGIBLE_JAVASCRIPT_INTERCEPTORS. */
private static final ServiceLoader<GraalJSInterceptor> DIRIGIBLE_JAVASCRIPT_INTERCEPTORS = ServiceLoader.load(GraalJSInterceptor.class);

/** The code runner. */
private final GraalJSCodeRunner codeRunner;
private final JavascriptSourceProvider sourceProvider;
/** The interceptor. */
private final GraalJSInterceptor interceptor;

Expand All @@ -71,15 +66,6 @@ public DirigibleJavascriptCodeRunner() {
this(new HashMap<>(), false);
}

/**
* Instantiates a new dirigible javascript code runner.
*
* @param shouldEnableDebug the debug
*/
public DirigibleJavascriptCodeRunner(boolean shouldEnableDebug) {
this(new HashMap<>(), shouldEnableDebug);
}

/**
* Instantiates a new dirigible javascript code runner.
*
Expand Down Expand Up @@ -127,42 +113,55 @@ private GraalJSCodeRunner createCodeRunner(Path workingDirectoryPath, Map<Object
Path coreModulesESMProxiesCachePath = cachePath.resolve("core-modules-proxies-cache");
Path javaModulesESMProxiesCachePath = cachePath.resolve("java-modules-proxies-cache");

return GraalJSCodeRunner.newBuilder(workingDirectoryPath, cachePath)
.addJSPolyfill(new RequirePolyfill())
.addGlobalObject(new DirigibleContextGlobalObject(context))
.addGlobalObject(new DirigibleEngineTypeGlobalObject())
.addModuleResolver(new JavaModuleResolver(javaModulesESMProxiesCachePath))
.addModuleResolver(new DirigibleModuleResolver(coreModulesESMProxiesCachePath, sourceProvider))
.addModuleResolver(new DirigibleEsmModuleResolver(sourceProvider))
.addModuleResolver(new DirigibleGlobalModuleResolver(sourceProvider))
.waitForDebugger(shouldEnableDebug && DirigibleJavascriptCodeRunner.shouldEnableDebug())
.addOnBeforeContextCreatedListener(onBeforeContextCreatedListener)
.addOnAfterContextCreatedListener(onAfterContextCreatedListener)
.setOnRealPathNotFound(p -> sourceProvider.unpackedToFileSystem(p, workingDirectoryPath.relativize(p)))
.setInterceptor(interceptor)
.build();
Optional<ExternalModuleResolver> externalModuleResolver = BeanProvider.getOptionalBean(ExternalModuleResolver.class);
GraalJSCodeRunner.Builder builder = GraalJSCodeRunner.newBuilder(workingDirectoryPath, cachePath)
.addJSPolyfill(new RequirePolyfill())
.addGlobalObject(new DirigibleContextGlobalObject(context))
.addGlobalObject(new DirigibleEngineTypeGlobalObject());
if (externalModuleResolver.isPresent()) {
builder.addModuleResolver(externalModuleResolver.get());
}

return builder.addModuleResolver(new JavaModuleResolver(javaModulesESMProxiesCachePath))
.addModuleResolver(new DirigibleModuleResolver(coreModulesESMProxiesCachePath, sourceProvider))
.addModuleResolver(new DirigibleEsmModuleResolver(sourceProvider))
.addModuleResolver(new DirigibleGlobalModuleResolver(sourceProvider))
.waitForDebugger(shouldEnableDebug && DirigibleJavascriptCodeRunner.shouldEnableDebug())
.addOnBeforeContextCreatedListener(onBeforeContextCreatedListener)
.addOnAfterContextCreatedListener(onAfterContextCreatedListener)
.setOnRealPathNotFound(p -> sourceProvider.unpackedToFileSystem(p, workingDirectoryPath.relativize(p)))
.setInterceptor(interceptor)
.build();
}

/**
* Prepare the Source to be run
* Should enable debug.
*
* @param codeFilePath the code file path to use
* @return the source
* @return true, if successful
*/
@Override
public Source prepareSource(Path codeFilePath) {
return codeRunner.prepareSource(codeFilePath);
private static boolean shouldEnableDebug() {
return Configuration.get("DIRIGIBLE_GRAALIUM_ENABLE_DEBUG", Boolean.FALSE.toString())
.equals(Boolean.TRUE.toString());
}

/**
* Run the given source.
* Gets the dirigible working directory.
*
* @param codeSource the code source
* @return the value
* @return the dirigible working directory
*/
@Override
public Value run(Source codeSource) {
return codeRunner.run(codeSource);
private Path getDirigibleWorkingDirectory() {
IRepository repository = (IRepository) StaticObjects.get(StaticObjects.REPOSITORY);
String publicRegistryPath = repository.getInternalResourcePath(IRepositoryStructure.PATH_REGISTRY_PUBLIC);
return Path.of(publicRegistryPath);
}

/**
* Instantiates a new dirigible javascript code runner.
*
* @param shouldEnableDebug the debug
*/
public DirigibleJavascriptCodeRunner(boolean shouldEnableDebug) {
this(new HashMap<>(), shouldEnableDebug);
}

public Module run(String codeFilePath) {
Expand All @@ -181,6 +180,32 @@ public Module run(Path codeFilePath) {
return new Module(module, moduleType);
}

/**
* Prepare the Source to be run
*
* @param codeFilePath the code file path to use
* @return the source
*/
@Override
public Source prepareSource(Path codeFilePath) {
return codeRunner.prepareSource(codeFilePath);
}

/**
* Run the given source.
*
* @param codeSource the code source
* @return the value
*/
@Override
public Value run(Source codeSource) {
return codeRunner.run(codeSource);
}

private static String transformTypeScriptHandlerPathIfNecessary(String handlerPath) {
return handlerPath.endsWith(TS_EXT) ? handlerPath.replaceAll(Pattern.quote(TS_EXT), JS_EXT) : handlerPath;
}

public Value runMethod(Module codeModule, String methodName, Object... args) {
return switch (codeModule.moduleType()) {
case CJS -> runCjsMethod(codeModule.module(), methodName, args);
Expand Down Expand Up @@ -211,27 +236,6 @@ public GraalJSCodeRunner getCodeRunner() {
return codeRunner;
}

/**
* Should enable debug.
*
* @return true, if successful
*/
private static boolean shouldEnableDebug() {
return Configuration.get("DIRIGIBLE_GRAALIUM_ENABLE_DEBUG", Boolean.FALSE.toString())
.equals(Boolean.TRUE.toString());
}

/**
* Gets the dirigible working directory.
*
* @return the dirigible working directory
*/
private Path getDirigibleWorkingDirectory() {
IRepository repository = (IRepository) StaticObjects.get(StaticObjects.REPOSITORY);
String publicRegistryPath = repository.getInternalResourcePath(IRepositoryStructure.PATH_REGISTRY_PUBLIC);
return Path.of(publicRegistryPath);
}

public JavascriptSourceProvider getSourceProvider() {
return sourceProvider;
}
Expand All @@ -245,10 +249,6 @@ public GraalJSInterceptor getGraalJSInterceptor() {
return interceptor;
}

private static String transformTypeScriptHandlerPathIfNecessary(String handlerPath) {
return handlerPath.endsWith(TS_EXT) ? handlerPath.replaceAll(Pattern.quote(TS_EXT), JS_EXT) : handlerPath;
}

/**
* Close.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,28 @@
*/
package org.eclipse.dirigible.graalium.core.modules;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

import org.eclipse.dirigible.commons.config.StaticObjects;
import org.eclipse.dirigible.graalium.core.JavascriptSourceProvider;
import org.eclipse.dirigible.graalium.core.javascript.CalledFromJS;
import org.eclipse.dirigible.repository.api.IRepository;
import org.eclipse.dirigible.repository.api.IRepositoryStructure;
import org.eclipse.dirigible.repository.api.IResource;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

/**
* The Class DirigibleSourceProvider.
*/
@Component
@CalledFromJS
public class DirigibleSourceProvider implements JavascriptSourceProvider {

/**
* Gets the repository.
*
* @return the repository
*/
static IRepository getRepository() {
return (IRepository) StaticObjects.get(StaticObjects.REPOSITORY);
}

/**
* Gets the absolute source path.
*
Expand All @@ -50,23 +43,17 @@ public Path getAbsoluteSourcePath(String projectName, String projectFileName) {
String projectFilePath = Path.of(projectName, projectFileName)
.toString();
String internalRepositoryRelativeSourcePath = getInternalRepositoryRelativeSourcePath(projectFilePath);
String absoluteSourcePathString = getRepository().getInternalResourcePath(internalRepositoryRelativeSourcePath.toString());
String absoluteSourcePathString = getRepository().getInternalResourcePath(internalRepositoryRelativeSourcePath);
return Path.of(absoluteSourcePathString);
}

/**
* Gets the absolute project path.
* Gets the repository.
*
* @param projectName the project name
* @return the absolute project path
* @return the repository
*/
@Override
public Path getAbsoluteProjectPath(String projectName) {
String projectFilePath = Path.of(projectName)
.toString();
String internalRepositoryRelativeSourcePath = getInternalRepositoryRelativeSourcePath(projectFilePath);
String absoluteSourcePathString = getRepository().getInternalResourcePath(internalRepositoryRelativeSourcePath.toString());
return Path.of(absoluteSourcePathString);
static IRepository getRepository() {
return (IRepository) StaticObjects.get(StaticObjects.REPOSITORY);
}

/**
Expand All @@ -80,6 +67,21 @@ protected String getInternalRepositoryRelativeSourcePath(String projectFilePath)
.toString();
}

/**
* Gets the absolute project path.
*
* @param projectName the project name
* @return the absolute project path
*/
@Override
public Path getAbsoluteProjectPath(String projectName) {
String projectFilePath = Path.of(projectName)
.toString();
String internalRepositoryRelativeSourcePath = getInternalRepositoryRelativeSourcePath(projectFilePath);
String absoluteSourcePathString = getRepository().getInternalResourcePath(internalRepositoryRelativeSourcePath);
return Path.of(absoluteSourcePathString);
}

/**
* Gets the source.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.eclipse.dirigible.graalium.core.modules;

import org.eclipse.dirigible.graalium.core.javascript.modules.ModuleResolver;

public interface ExternalModuleResolver extends ModuleResolver {

}

0 comments on commit d9d13ff

Please sign in to comment.