-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sdk factory setup, classloading still not correct, however prism cont…
…ext initializes
- Loading branch information
Showing
12 changed files
with
588 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
midpoint-sdk-api/src/main/java/com/evolveum/midpoint/sdk/api/SdkComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
88 changes: 88 additions & 0 deletions
88
midpoint-sdk-api/src/main/java/com/evolveum/midpoint/sdk/api/SdkContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
midpoint-sdk-api/src/main/java/com/evolveum/midpoint/sdk/api/SdkContextFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
12 changes: 12 additions & 0 deletions
12
midpoint-sdk-api/src/main/java/com/evolveum/midpoint/sdk/api/SdkException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
midpoint-sdk-api/src/main/java/com/evolveum/midpoint/sdk/api/SdkFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
midpoint-sdk-api/src/main/java/com/evolveum/midpoint/sdk/api/SdkInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
4 changes: 4 additions & 0 deletions
4
midpoint-sdk-api/src/main/java/com/evolveum/midpoint/sdk/api/client/DeleteOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.evolveum.midpoint.sdk.api.client; | ||
|
||
public enum DeleteOptions { | ||
} |
9 changes: 9 additions & 0 deletions
9
midpoint-sdk-api/src/main/java/com/evolveum/midpoint/sdk/api/client/MessageListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.