From c1b346fe0a5bd2c8b288bddb070abd1634a0fb20 Mon Sep 17 00:00:00 2001 From: Ferdinando Villa Date: Thu, 19 Dec 2024 12:29:29 +0100 Subject: [PATCH] Inserting Maven logics --- .../services/resources/ResourceTransport.java | 56 +++++++++---------- .../runtime/language/LanguageService.java | 4 +- .../runtime/libraries/ComponentIOLibrary.java | 29 +++++++++- .../klab/utilities/Utils.java | 23 +++++++- 4 files changed, 81 insertions(+), 31 deletions(-) diff --git a/klab.core.api/src/main/java/org/integratedmodelling/klab/api/services/resources/ResourceTransport.java b/klab.core.api/src/main/java/org/integratedmodelling/klab/api/services/resources/ResourceTransport.java index 7c11f9e35..587bd3d32 100644 --- a/klab.core.api/src/main/java/org/integratedmodelling/klab/api/services/resources/ResourceTransport.java +++ b/klab.core.api/src/main/java/org/integratedmodelling/klab/api/services/resources/ResourceTransport.java @@ -22,13 +22,13 @@ public enum ResourceTransport { INSTANCE; - private Map> importSchemata = new HashMap<>(); - private Map> exportSchemata = new HashMap<>(); + private final Map> importSchemata = new HashMap<>(); + private final Map> exportSchemata = new HashMap<>(); - public Schema COMPONENT_MAVEN; - public Schema COMPONENT_JAR; - public Schema PROJECT_ZIP; - public Schema PROJECT_GIT; +// public Schema COMPONENT_MAVEN; +// public Schema COMPONENT_JAR; +// public Schema PROJECT_ZIP; +// public Schema PROJECT_GIT; /** @@ -343,28 +343,28 @@ private List findSchemata(Map> schemata, String sch */ ResourceTransport() { - addImport("component", - COMPONENT_MAVEN = Schema.create("component.maven", - Schema.Type.PROPERTIES, KlabAsset.KnowledgeClass.COMPONENT, "Register a component " + - "available on Maven " + "using " + "the component's Maven coordinates").with( - "groupId", Artifact.Type.TEXT, false).with("adapterId", Artifact.Type.TEXT, - false).with("version", Artifact.Type.TEXT, false), - COMPONENT_JAR = Schema.create("component.jar", - Schema.Type.STREAM, KlabAsset.KnowledgeClass.COMPONENT, "Register a component by " + - "directly " + - "submitting a jar file").mediaType("application/java-archive").fileExtensions("jar")); - - addImport("project.git", - PROJECT_GIT = Schema.create("project.git", Schema.Type.PROPERTIES, - KlabAsset.KnowledgeClass.PROJECT, "Register a k.LAB project by submitting the URL " + - "of a Git " - + "repository and optional credentials").with("url", Artifact.Type.TEXT, - false).with("username", Artifact.Type.TEXT, true).with("password", - Artifact.Type.TEXT, true).with("token", Artifact.Type.TEXT, true), - PROJECT_ZIP = Schema.create("project.zip", - Schema.Type.STREAM, - KlabAsset.KnowledgeClass.PROJECT, "Register a k.LAB by directly submitting a zip " + - "archive").mediaType("application/zip", "application/x-zip-compressed").fileExtensions("zip")); +// addImport("component", +// COMPONENT_MAVEN = Schema.create("component.maven", +// Schema.Type.PROPERTIES, KlabAsset.KnowledgeClass.COMPONENT, "Register a component " + +// "available on Maven " + "using " + "the component's Maven coordinates").with( +// "groupId", Artifact.Type.TEXT, false).with("adapterId", Artifact.Type.TEXT, +// false).with("version", Artifact.Type.TEXT, false), +// COMPONENT_JAR = Schema.create("component.jar", +// Schema.Type.STREAM, KlabAsset.KnowledgeClass.COMPONENT, "Register a component by " + +// "directly " + +// "submitting a jar file").mediaType("application/java-archive").fileExtensions("jar")); +// +// addImport("project.git", +// PROJECT_GIT = Schema.create("project.git", Schema.Type.PROPERTIES, +// KlabAsset.KnowledgeClass.PROJECT, "Register a k.LAB project by submitting the URL " + +// "of a Git " +// + "repository and optional credentials").with("url", Artifact.Type.TEXT, +// false).with("username", Artifact.Type.TEXT, true).with("password", +// Artifact.Type.TEXT, true).with("token", Artifact.Type.TEXT, true), +// PROJECT_ZIP = Schema.create("project.zip", +// Schema.Type.STREAM, +// KlabAsset.KnowledgeClass.PROJECT, "Register a k.LAB by directly submitting a zip " + +// "archive").mediaType("application/zip", "application/x-zip-compressed").fileExtensions("zip")); } diff --git a/klab.core.services/src/main/java/org/integratedmodelling/klab/runtime/language/LanguageService.java b/klab.core.services/src/main/java/org/integratedmodelling/klab/runtime/language/LanguageService.java index c499d1658..5e356baac 100644 --- a/klab.core.services/src/main/java/org/integratedmodelling/klab/runtime/language/LanguageService.java +++ b/klab.core.services/src/main/java/org/integratedmodelling/klab/runtime/language/LanguageService.java @@ -139,7 +139,9 @@ private Object[] getParameters(ComponentRegistry.FunctionDescriptor descriptor, * @return */ private Object[] matchParametersFreeform(Class[] parameterTypes, ServiceCall call, Scope scope) { - List payload = call.getParameters().getUnnamedArguments(); + List payload = new ArrayList<>(call.getParameters().getUnnamedArguments()); + payload.add(call); + payload.add(scope); if (!call.getParameters().isEmpty()) { payload.add(call.getParameters()); } diff --git a/klab.core.services/src/main/java/org/integratedmodelling/klab/runtime/libraries/ComponentIOLibrary.java b/klab.core.services/src/main/java/org/integratedmodelling/klab/runtime/libraries/ComponentIOLibrary.java index b841b9674..68beafea1 100644 --- a/klab.core.services/src/main/java/org/integratedmodelling/klab/runtime/libraries/ComponentIOLibrary.java +++ b/klab.core.services/src/main/java/org/integratedmodelling/klab/runtime/libraries/ComponentIOLibrary.java @@ -9,7 +9,9 @@ import org.integratedmodelling.klab.api.services.resources.adapters.Importer; import org.integratedmodelling.klab.api.services.runtime.extension.KlabFunction; import org.integratedmodelling.klab.api.services.runtime.extension.Library; +import org.integratedmodelling.klab.utilities.Utils; +import java.io.File; import java.io.InputStream; @Library(name = "component", description = "Importers for components shared by all services", version = @@ -19,7 +21,7 @@ public class ComponentIOLibrary { @Importer(schema = "jar", knowledgeClass = KlabAsset.KnowledgeClass.COMPONENT, description = "Import a component by directly uploading a jar file", mediaType = "application/java-archive", fileExtensions = {"jar"}) - public static String importComponentDirect() { + public static String importComponentDirect(File file) { return null; } @@ -36,6 +38,31 @@ public static String importComponentDirect() { description = "Non-standard Maven repository", optional = true) }) public static String importComponentMaven(Parameters properties) { + + if (Utils.Maven.needsUpdate(properties.get("groupId", String.class), + properties.get("artifactId", String.class), + properties.get("version", String.class))) { + + var file = Utils.Maven.synchronizeArtifact(properties.get("groupId", String.class), + properties.get("artifactId", String.class), + properties.get("version", String.class), true); + + if (file != null && file.exists()) { + + /* + Unload any existing component + */ + + /* + Update catalog + */ + + /* + Load component + */ + } + + } return null; } diff --git a/klab.core.services/src/main/java/org/integratedmodelling/klab/utilities/Utils.java b/klab.core.services/src/main/java/org/integratedmodelling/klab/utilities/Utils.java index 0c0c965f3..b8f146d40 100644 --- a/klab.core.services/src/main/java/org/integratedmodelling/klab/utilities/Utils.java +++ b/klab.core.services/src/main/java/org/integratedmodelling/klab/utilities/Utils.java @@ -40,6 +40,7 @@ import java.io.IOException; import java.io.InputStream; import java.util.*; +import java.util.concurrent.atomic.AtomicReference; public class Utils extends org.integratedmodelling.common.utils.Utils { @@ -87,11 +88,31 @@ public static class Maven { */ public static boolean needsUpdate(String mavenGroupId, String mavenArtifactId, String version) { + if (version.contains("SNAPSHOT")) { + // TODO if version exists in repo, check hash + return true; + } + + var request = new MavenFetchRequest(mavenGroupId + ":" + mavenArtifactId + ":" + version); + var result = mavenFetcher.fetchArtifacts(request); + if (result.artifacts().findAny().isPresent()) { + return false; + } + + // TODO check if version exists in repo + return false; } - public File retrieveArtifact(String mavenGroupId, String mavenArtifactId, Version version, + public static File synchronizeArtifact(String mavenGroupId, String mavenArtifactId, String version, boolean verifySignature) { + var request = new MavenFetchRequest(mavenGroupId + ":" + mavenArtifactId + ":" + version); + var result = mavenFetcher.fetchArtifacts(request); + if (result.artifacts().findAny().isPresent()) { + AtomicReference ret = new AtomicReference<>(); + result.artifacts().peek(fetchedArtifact -> ret.set(fetchedArtifact.path().toFile())); + return ret.get(); + } return null; }