Skip to content

Commit

Permalink
Inserting Maven logics
Browse files Browse the repository at this point in the history
  • Loading branch information
fvilla committed Dec 19, 2024
1 parent 774833a commit c1b346f
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public enum ResourceTransport {

INSTANCE;

private Map<String, List<Schema>> importSchemata = new HashMap<>();
private Map<String, List<Schema>> exportSchemata = new HashMap<>();
private final Map<String, List<Schema>> importSchemata = new HashMap<>();
private final Map<String, List<Schema>> 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;


/**
Expand Down Expand Up @@ -343,28 +343,28 @@ private List<Schema> findSchemata(Map<String, List<Schema>> 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"));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ private Object[] getParameters(ComponentRegistry.FunctionDescriptor descriptor,
* @return
*/
private Object[] matchParametersFreeform(Class<?>[] parameterTypes, ServiceCall call, Scope scope) {
List<Object> payload = call.getParameters().getUnnamedArguments();
List<Object> payload = new ArrayList<>(call.getParameters().getUnnamedArguments());
payload.add(call);
payload.add(scope);
if (!call.getParameters().isEmpty()) {
payload.add(call.getParameters());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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;
}

Expand All @@ -36,6 +38,31 @@ public static String importComponentDirect() {
description = "Non-standard Maven repository", optional = true)
})
public static String importComponentMaven(Parameters<String> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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<File> ret = new AtomicReference<>();
result.artifacts().peek(fetchedArtifact -> ret.set(fetchedArtifact.path().toFile()));
return ret.get();
}
return null;
}

Expand Down

0 comments on commit c1b346f

Please sign in to comment.