Skip to content

Commit

Permalink
initial import sdk with schema visible, not working since plugin init…
Browse files Browse the repository at this point in the history
…ialization needs schema classes which are not available during sdk doesn't load something
  • Loading branch information
1azyman committed Nov 27, 2023
1 parent 41ec31a commit 5a7ab1b
Show file tree
Hide file tree
Showing 26 changed files with 466 additions and 269 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.evolveum.midpoint.sdk.api;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface SdkComponent {

Class<?> type();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.evolveum.midpoint.sdk.api;

import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.sdk.api.lang.AuthorizationActionProvider;
import com.evolveum.midpoint.sdk.api.lang.ExpressionVariablesProvider;
import com.evolveum.midpoint.sdk.api.lang.TaskHandlerProvider;

public class SdkContext {

private PrismContext prismContext;

private TaskHandlerProvider taskHandlerProvider;

private AuthorizationActionProvider authorizationActionProvider;

private ExpressionVariablesProvider expressionVariablesProvider;

public PrismContext prismContext() {
return prismContext;
}

public TaskHandlerProvider taskHandlerProvider() {
return taskHandlerProvider;
}

public AuthorizationActionProvider authorizationActionProvider() {
return authorizationActionProvider;
}

public ExpressionVariablesProvider expressionVariablesProvider() {
return expressionVariablesProvider;
}

public static class Builder {

private PrismContext prismContext;

private TaskHandlerProvider taskHandlerProvider;

private AuthorizationActionProvider authorizationActionProvider;

private ExpressionVariablesProvider expressionVariablesProvider;

public Builder prismContext(PrismContext prismContext) {
this.prismContext = prismContext;
return this;
}

public Builder taskHandlerProvider(TaskHandlerProvider taskHandlerProvider) {
this.taskHandlerProvider = taskHandlerProvider;
return this;
}

public Builder authorizationActionProvider(AuthorizationActionProvider authorizationActionProvider) {
this.authorizationActionProvider = authorizationActionProvider;
return this;
}

public Builder expressionVariablesProvider(ExpressionVariablesProvider expressionVariablesProvider) {
this.expressionVariablesProvider = expressionVariablesProvider;
return this;
}

public SdkContext build() {
SdkContext sdkContext = new SdkContext();
sdkContext.prismContext = prismContext;
sdkContext.taskHandlerProvider = taskHandlerProvider;
sdkContext.authorizationActionProvider = authorizationActionProvider;
sdkContext.expressionVariablesProvider = expressionVariablesProvider;
return sdkContext;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.evolveum.midpoint.sdk.api;

public interface SdkContextFactory {

SdkContext creatContext() throws SdkException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.evolveum.midpoint.sdk.api;

public class SdkException extends Exception {

public SdkException(String message) {
super(message);
}

public SdkException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.evolveum.midpoint.sdk.api;

import com.google.common.collect.ImmutableList;
import io.github.classgraph.ClassGraph;
import io.github.classgraph.ScanResult;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.loader.LaunchedURLClassLoader;
import org.springframework.boot.loader.archive.Archive;
import org.springframework.boot.loader.archive.JarFileArchive;

import java.io.File;
import java.lang.annotation.Annotation;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

public class SdkFactory {

private static final Logger LOG = LoggerFactory.getLogger(SdkFactory.class);

private File sdkFile;

public SdkFactory sdkFile(File sdkFile) {
this.sdkFile = sdkFile;
return this;
}

public SdkContext build() throws SdkException {
Validate.notNull(sdkFile, "SDK jar file must be set");

if (!sdkFile.exists() || !sdkFile.canRead() || !sdkFile.isFile()) {
throw new IllegalArgumentException("SDK jar file does not exist or is not readable");
}

try {
Archive archive = new JarFileArchive(sdkFile);

List<URL> urls = new ArrayList<>();
urls.add(archive.getUrl());

Iterator<Archive> iterator = archive.getNestedArchives(null, (entry) -> {
if (entry.isDirectory()) {
return entry.getName().equals("BOOT-INF/classes/");
}
return entry.getName().startsWith("BOOT-INF/lib/");
});

ImmutableList.copyOf(iterator).forEach(a -> {
try {
urls.add(a.getUrl());
} catch (MalformedURLException e) {
e.printStackTrace(); // todo fix
}
});

LaunchedURLClassLoader classLoader = new LaunchedURLClassLoader(
false, archive, urls.toArray(new URL[0]), Thread.currentThread().getContextClassLoader());

@SuppressWarnings("unchecked")
Class<? extends SdkContextFactory> factoryClass = (Class<? extends SdkContextFactory>)
scanClasses(classLoader, SdkComponent.class, "com.evolveum.midpoint.sdk")
.stream()
.filter(c -> Objects.equals(c.getAnnotation(SdkComponent.class).type(), SdkContextFactory.class))
.filter(c -> SdkContextFactory.class.isAssignableFrom(c))
.findFirst()
.orElse(null);

if (factoryClass == null) {
throw new SdkException("Couldn't find SDK context factory class annotated with @SdkComponent");
}

SdkContextFactory factory = factoryClass.getConstructor().newInstance();
return factory.creatContext();
} catch (Exception ex) {
throw new SdkException("Couldn't build SDK context", ex);
}
}

Collection<Class<?>> scanClasses(
ClassLoader classLoader, Class<? extends Annotation> annotationClass, String... packageNames) {

LOG.debug("Scanning classes for: {} with package scope: {}", annotationClass, packageNames);

try (ScanResult scanResult = new ClassGraph()
.overrideClassLoaders(classLoader)
.acceptPackages(packageNames)
.enableClassInfo()
.enableAnnotationInfo()
.scan()) {

List<Class<?>> classes = scanResult
.getClassesWithAnnotation(annotationClass)
.loadClasses();

LOG.debug("Found {} classes with annotation {}", classes.size(), annotationClass.getName());

return classes;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.evolveum.midpoint.sdk.api;

public interface SdkInfo {

String getVersion();

String getName();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.evolveum.midpoint.sdk.api.lang;

import javax.xml.namespace.QName;
import java.util.Comparator;
import java.util.Objects;

public class Action implements Comparable<Action> {

private QName name;

private String source;

public Action(QName name, String source) {
this.name = name;
this.source = source;
}

public QName name() {
return name;
}

public String source() {
return source;
}

@Override
public int compareTo(Action o) {
String q1 = name != null ? name.getLocalPart() : null;
String q2 = o != null && o.name != null ? o.name.getLocalPart() : null;

return Comparator.<String>naturalOrder().compare(q1, q2);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Action action = (Action) o;
return Objects.equals(name, action.name) && Objects.equals(source, action.source);
}

@Override
public int hashCode() {
return Objects.hash(name, source);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.evolveum.midpoint.sdk.api.lang;

import java.util.Set;

public interface AuthorizationActionProvider {

Set<Action> getActions();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.evolveum.midpoint.sdk.api.lang;

import java.util.Map;

public interface ExpressionVariablesProvider {

// todo parameters describing the context of the expression evaluation
// eg. type of the object being evaluated, type of the expression, place (template, policy rule, assingment target search, ...)
Map<String, Variable> getVariables();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.evolveum.midpoint.sdk.api.lang;

import java.util.Set;

public interface TaskHandlerProvider {

Set<String> getTaskHandlerUris();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.evolveum.midpoint.sdk.api.lang;

import java.util.Objects;

public class Variable<T, I extends T> {

private String name;

private Class<T> type;

private Class<I> implementation;

public Variable(String name, Class<T> type, Class<I> implementation) {
this.name = name;
this.type = type;
this.implementation = implementation;
}

public String name() {
return name;
}

public Class<T> type() {
return type;
}

public Class<I> implementation() {
return implementation;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Variable<?, ?> variable = (Variable<?, ?>) o;
return Objects.equals(name, variable.name) && Objects.equals(type, variable.type) && Objects.equals(implementation, variable.implementation);
}

@Override
public int hashCode() {
return Objects.hash(name, type, implementation);
}
}
15 changes: 12 additions & 3 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ dependencyResolutionManagement {
version("jaxb-runtime", "2.3.2")
version("jcommander", "1.81")
version("logback", "1.2.3")
version("midpoint", "4.8")
version("midpoint", "4.9-SNAPSHOT")
version("okhttp", "4.10.0")
version("openkeepass", "0.8.1")
version("spring", "5.3.8")
version("spring-boot", "3.1.5")
version("stax", "1.2.0")
version("testng", "6.14.3")
version("slf4j", "1.7.32")
Expand All @@ -43,18 +44,24 @@ dependencyResolutionManagement {
library("jaxb-runtime", "org.glassfish.jaxb", "jaxb-runtime").versionRef("jaxb-runtime")
library("jcommander", "com.beust", "jcommander").versionRef("jcommander")
library("logback-classic", "ch.qos.logback", "logback-classic").versionRef("logback")
library("prism-utils", "com.evolveum.commons", "util").versionRef("midpoint")
library("prism-api", "com.evolveum.prism", "prism-api").versionRef("midpoint")
library("prism-impl", "com.evolveum.prism", "prism-impl").versionRef("midpoint")
library("midpoint-common", "com.evolveum.midpoint.infra", "common").versionRef("midpoint")
library("midpoint-localization", "com.evolveum.midpoint", "midpoint-localization").versionRef("midpoint")
library("midpoint-model-api", "com.evolveum.midpoint.model", "model-api").versionRef("midpoint")
library("midpoint-model-common", "com.evolveum.midpoint.model", "model-common").versionRef("midpoint")
library("midpoint-model-impl", "com.evolveum.midpoint.model", "model-impl").versionRef("midpoint")
library("midpoint-notifications-api", "com.evolveum.midpoint.model", "notifications-api").versionRef("midpoint")
library("midpoint-schema", "com.evolveum.midpoint.infra", "schema").versionRef("midpoint")
library("notifications-api", "com.evolveum.midpoint.model", "notifications-api").versionRef("midpoint")
library("midpoint-security-api", "com.evolveum.midpoint.repo", "security-api").versionRef("midpoint")

library("okhttp-logging", "com.squareup.okhttp3", "logging-interceptor").versionRef("okhttp")
library("okhttp3", "com.squareup.okhttp3", "okhttp").versionRef("okhttp")
library("openkeepass", "de.slackspace", "openkeepass").versionRef("openkeepass")
library("security-api", "com.evolveum.midpoint.repo", "security-api").versionRef("midpoint")

library("spring-core", "org.springframework", "spring-core").versionRef("spring")
library("spring-boot-loader", "org.springframework.boot", "spring-boot-loader").versionRef("spring-boot")
library("stax", "stax", "stax").versionRef("stax")
library("slf4j-api", "org.slf4j", "slf4j-api").versionRef("slf4j")
library("velocity", "org.apache.velocity", "velocity-engine-core").versionRef("velocity")
Expand All @@ -78,5 +85,7 @@ dependencyResolutionManagement {

include("midpoint-client")
include("midscribe")
include("midpoint-sdk-api")
include("midpoint-sdk-impl")
include("studio-idea-plugin")
include("studio-gradle-plugin")

0 comments on commit 5a7ab1b

Please sign in to comment.