Skip to content

Commit

Permalink
sdk factory setup, classloading still not correct, however prism cont…
Browse files Browse the repository at this point in the history
…ext initializes
  • Loading branch information
1azyman committed Nov 22, 2023
1 parent 4b6e1f5 commit a9690c8
Show file tree
Hide file tree
Showing 12 changed files with 588 additions and 0 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,88 @@
package com.evolveum.midpoint.sdk.api;

import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.sdk.api.client.MidpointClient;
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 MidpointClient client;

private PrismContext prismContext;

private TaskHandlerProvider taskHandlerProvider;

private AuthorizationActionProvider authorizationActionProvider;

private ExpressionVariablesProvider expressionVariablesProvider;

public MidpointClient client() {
return client;
}

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 MidpointClient client;

private PrismContext prismContext;

private TaskHandlerProvider taskHandlerProvider;

private AuthorizationActionProvider authorizationActionProvider;

private ExpressionVariablesProvider expressionVariablesProvider;

public Builder client(MidpointClient client) {
this.client = client;
return this;
}

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.client = client;
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,4 @@
package com.evolveum.midpoint.sdk.api.client;

public enum DeleteOptions {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.evolveum.midpoint.sdk.api.client;

/**
* Created by Viliam Repan (lazyman).
*/
public interface MessageListener {

void handleMessage(String message);
}

0 comments on commit a9690c8

Please sign in to comment.