diff --git a/native-cli/build.gradle b/native-cli/build.gradle index 736dc1e..5449ae5 100644 --- a/native-cli/build.gradle +++ b/native-cli/build.gradle @@ -28,7 +28,7 @@ dependencies { compile group: 'org.graalvm.sdk', name: 'graal-sdk', version: '1.0.0-rc12' compile group: 'org.mule.weave', name: 'core-modules', version: weaveVersion compile group: 'org.mule.weave', name: 'yaml-module', version: weaveVersion -// compile group: 'org.mule.weave', name: 'dependency-manager', version: weaveVersion + compile group: 'org.mule.weave', name: 'dependency-manager', version: weaveVersion // compile(group: 'org.mule.weave', name: 'raml-module', version: ioVersion){ // exclude group: 'org.slf4j' // exclude group: 'org.topbraid.shacl' diff --git a/native-cli/src/main/scala/org/mule/weave/dwnative/NativeRuntime.scala b/native-cli/src/main/scala/org/mule/weave/dwnative/NativeRuntime.scala index 23b8577..0e12edc 100644 --- a/native-cli/src/main/scala/org/mule/weave/dwnative/NativeRuntime.scala +++ b/native-cli/src/main/scala/org/mule/weave/dwnative/NativeRuntime.scala @@ -3,6 +3,8 @@ package org.mule.weave.dwnative import io.netty.util.internal.PlatformDependent import org.mule.weave.dwnative.cli.Console import org.mule.weave.dwnative.initializer.NativeSystemModuleComponents +import org.mule.weave.v2.deps.Artifact +import org.mule.weave.v2.deps.ResourceDependencyAnnotationProcessor import org.mule.weave.v2.exception.InvalidLocationException import org.mule.weave.v2.interpreted.CustomRuntimeModuleNodeCompiler import org.mule.weave.v2.interpreted.RuntimeModuleNodeCompiler @@ -45,14 +47,34 @@ import java.io.OutputStream import java.io.PrintWriter import java.io.StringWriter import java.nio.charset.StandardCharsets +import java.util.concurrent.ExecutorService +import scala.concurrent.Await +import scala.concurrent.Future +import scala.concurrent.duration.Duration -class NativeRuntime(libDir: File, path: Array[File], console: Console) { +class NativeRuntime(resourcesCacheDir: File, executor: ExecutorService, libDir: File, path: Array[File], console: Console) { private val pathBasedResourceResolver: PathBasedResourceResolver = PathBasedResourceResolver(path ++ Option(libDir.listFiles()).getOrElse(new Array[File](0))) + + private val resourceDependencyAnnotationProcessor = ResourceDependencyAnnotationProcessor( + new File(resourcesCacheDir, "resources"), + (id: String, kind: String, artifact: Future[Seq[Artifact]]) => { + pathBasedResourceResolver.addContent(new LazyContentResolver(() => { + new CompositeContentResolver(Await.result(artifact, Duration.Inf) + .map((artifact) => { + ContentResolver(artifact.file) + })) + }) + ) + }, executor + ) + + private val annotationProcessors: Seq[(String, AnnotationProcessor)] = Seq( + (ResourceDependencyAnnotationProcessor.ANNOTATION_NAME.name, resourceDependencyAnnotationProcessor) + ) private val weaveScriptingEngine: DataWeaveScriptingEngine = { setupEnv() - val annotationProcessors: Seq[(String, AnnotationProcessor)] = Seq() DataWeaveScriptingEngine(new NativeModuleComponentFactory(() => pathBasedResourceResolver, systemFirst = true), ParserConfiguration(parsingAnnotationProcessors = annotationProcessors)) } diff --git a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/RunWeaveCommand.scala b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/RunWeaveCommand.scala index 61fbffd..32c2246 100644 --- a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/RunWeaveCommand.scala +++ b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/RunWeaveCommand.scala @@ -6,6 +6,7 @@ import org.mule.weave.dwnative.cli.Console import org.mule.weave.dwnative.cli.FileWatcher import org.mule.weave.dwnative.utils.DataWeaveUtils import org.mule.weave.dwnative.utils.DataWeaveUtils._ +import org.mule.weave.v2.io.FileHelper.deleteDirectory import org.mule.weave.v2.model.EvaluationContext import org.mule.weave.v2.model.values.StringValue import org.mule.weave.v2.module.DataFormatManager @@ -19,6 +20,7 @@ import java.io.FileOutputStream import java.io.OutputStream import java.io.PrintWriter import java.io.StringWriter +import java.util.concurrent.Executors import scala.util.Try class RunWeaveCommand(config: WeaveRunnerConfig, console: Console) extends WeaveCommand { @@ -32,8 +34,12 @@ class RunWeaveCommand(config: WeaveRunnerConfig, console: Console) extends Weave def exec(): Int = { val path: Array[File] = config.path.map(new File(_)) - val nativeRuntime: NativeRuntime = new NativeRuntime(weaveUtils.getLibPathHome(), path, console) - + val cacheDirectory: File = weaveUtils.getCacheHome() + if (config.cleanCache) { + deleteDirectory(cacheDirectory) + cacheDirectory.mkdirs() + } + val nativeRuntime: NativeRuntime = new NativeRuntime(cacheDirectory, Executors.newCachedThreadPool(), weaveUtils.getLibPathHome(), path, console) if (config.watch) { console.clear() val fileWatcher = FileWatcher(config.filesToWatch) diff --git a/native-cli/src/main/scala/org/mule/weave/dwnative/utils/DataWeaveUtils.scala b/native-cli/src/main/scala/org/mule/weave/dwnative/utils/DataWeaveUtils.scala index 53eb85c..e840198 100644 --- a/native-cli/src/main/scala/org/mule/weave/dwnative/utils/DataWeaveUtils.scala +++ b/native-cli/src/main/scala/org/mule/weave/dwnative/utils/DataWeaveUtils.scala @@ -16,6 +16,19 @@ object DataWeaveUtils { class DataWeaveUtils(console: Console) { + def getCacheHome(): File = { + val weavehome = console.envVar("DW_CACHE_PATH") + if (weavehome.nonEmpty) { + val home = new File(weavehome.get) + if (!home.exists()) { + console.error(s" Weave Cache Home Directory `${weavehome}` declared on environment variable `DW_CACHE_PATH` does not exists.") + } + home + } else { + new File(getDWHome(), "cache") + } + } + /** * Returns the DW home directory if exists it can be overwritten with env variable DW_HOME *