Refactor annotation element retrieval
Signed-off-by: Simon Vacek <simonvacky@email.cz>
This commit is contained in:
parent
ef54d20be1
commit
5a6ac58107
8 changed files with 31 additions and 67 deletions
|
@ -53,12 +53,6 @@
|
||||||
<groupId>com.microsoft.sqlserver</groupId>
|
<groupId>com.microsoft.sqlserver</groupId>
|
||||||
<artifactId>mssql-jdbc</artifactId>
|
<artifactId>mssql-jdbc</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- Temporary dependency until we figure out how we want to support OAuth -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.nimbusds</groupId>
|
|
||||||
<artifactId>oauth2-oidc-sdk</artifactId>
|
|
||||||
<version>11.13</version>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -25,10 +25,10 @@ public class InstanceContext<T, A extends Annotation> {
|
||||||
this.supplier = supplier;
|
this.supplier = supplier;
|
||||||
this.annotation = annotation;
|
this.annotation = annotation;
|
||||||
this.requestedValueType = requestedValueType;
|
this.requestedValueType = requestedValueType;
|
||||||
this.config = supplier.getConfig(annotation);
|
this.config = (Class<?>) supplier.getAnnotationElementValue(annotation, SupplierHelpers.CONFIG);
|
||||||
this.lifeCycle = supplier.getLifeCycle(annotation);
|
this.lifeCycle = (LifeCycle) supplier.getAnnotationElementValue(annotation, SupplierHelpers.LIFECYCLE);
|
||||||
this.ref = supplier.getRef(annotation);
|
this.ref = (String) supplier.getAnnotationElementValue(annotation, SupplierHelpers.REF);
|
||||||
this.realmRef = supplier.getRealmRef(annotation);
|
this.realmRef = (String) supplier.getAnnotationElementValue(annotation, SupplierHelpers.REALM_REF);
|
||||||
}
|
}
|
||||||
|
|
||||||
public InstanceContext(Registry registry, Supplier<T, A> supplier, Class<? extends T> requestedValueType, String ref, Class<?> config) {
|
public InstanceContext(Registry registry, Supplier<T, A> supplier, Class<? extends T> requestedValueType, String ref, Class<?> config) {
|
||||||
|
|
|
@ -2,7 +2,6 @@ package org.keycloak.test.framework.injection;
|
||||||
|
|
||||||
import org.jboss.logging.Logger;
|
import org.jboss.logging.Logger;
|
||||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||||
import org.keycloak.test.framework.annotations.InjectRealm;
|
|
||||||
import org.keycloak.test.framework.config.Config;
|
import org.keycloak.test.framework.config.Config;
|
||||||
import org.keycloak.test.framework.realm.DefaultRealmConfig;
|
import org.keycloak.test.framework.realm.DefaultRealmConfig;
|
||||||
import org.keycloak.test.framework.realm.ManagedRealm;
|
import org.keycloak.test.framework.realm.ManagedRealm;
|
||||||
|
@ -62,7 +61,7 @@ public class Registry {
|
||||||
|
|
||||||
private <T> T getDeployedDependency(Class<T> typeClass, InstanceContext dependent) {
|
private <T> T getDeployedDependency(Class<T> typeClass, InstanceContext dependent) {
|
||||||
InstanceContext dependency;
|
InstanceContext dependency;
|
||||||
if(!dependent.getRealmRef().equals("")) {
|
if(typeClass.equals(ManagedRealm.class) && !dependent.getRealmRef().equals("")) {
|
||||||
dependency = getDeployedInstance(typeClass, dependent.getRealmRef());
|
dependency = getDeployedInstance(typeClass, dependent.getRealmRef());
|
||||||
} else {
|
} else {
|
||||||
dependency = getDeployedInstance(typeClass);
|
dependency = getDeployedInstance(typeClass);
|
||||||
|
@ -85,7 +84,7 @@ public class Registry {
|
||||||
private <T> T getRequestedDependency(Class<T> typeClass, InstanceContext dependent) {
|
private <T> T getRequestedDependency(Class<T> typeClass, InstanceContext dependent) {
|
||||||
InstanceContext dependency;
|
InstanceContext dependency;
|
||||||
RequestedInstance requestedDependency;
|
RequestedInstance requestedDependency;
|
||||||
if(!dependent.getRealmRef().equals("")) {
|
if(typeClass.equals(ManagedRealm.class) && !dependent.getRealmRef().equals("")) {
|
||||||
requestedDependency = getRequestedInstance(typeClass, dependent.getRealmRef());
|
requestedDependency = getRequestedInstance(typeClass, dependent.getRealmRef());
|
||||||
} else {
|
} else {
|
||||||
requestedDependency = getRequestedInstance(typeClass);
|
requestedDependency = getRequestedInstance(typeClass);
|
||||||
|
@ -256,7 +255,7 @@ public class Registry {
|
||||||
Supplier supplier = i.getSupplier();
|
Supplier supplier = i.getSupplier();
|
||||||
if (supplier.getAnnotationClass().equals(a.annotationType())
|
if (supplier.getAnnotationClass().equals(a.annotationType())
|
||||||
&& valueType.isAssignableFrom(i.getValue().getClass())
|
&& valueType.isAssignableFrom(i.getValue().getClass())
|
||||||
&& supplier.getRef(a).equals(i.getRef()) ) {
|
&& supplier.getAnnotationElementValue(a, SupplierHelpers.REF).equals(i.getRef()) ) {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,15 +9,13 @@ public class RequestedInstance<T, A extends Annotation> {
|
||||||
private final Class<? extends T> valueType;
|
private final Class<? extends T> valueType;
|
||||||
private final LifeCycle lifeCycle;
|
private final LifeCycle lifeCycle;
|
||||||
private final String ref;
|
private final String ref;
|
||||||
private final String realmRef;
|
|
||||||
|
|
||||||
public RequestedInstance(Supplier<T, A> supplier, A annotation, Class<? extends T> valueType) {
|
public RequestedInstance(Supplier<T, A> supplier, A annotation, Class<? extends T> valueType) {
|
||||||
this.supplier = supplier;
|
this.supplier = supplier;
|
||||||
this.annotation = annotation;
|
this.annotation = annotation;
|
||||||
this.valueType = valueType;
|
this.valueType = valueType;
|
||||||
this.lifeCycle = supplier.getLifeCycle(annotation);
|
this.lifeCycle = (LifeCycle) supplier.getAnnotationElementValue(annotation, SupplierHelpers.LIFECYCLE);
|
||||||
this.ref = supplier.getRef(annotation);
|
this.ref = (String) supplier.getAnnotationElementValue(annotation, SupplierHelpers.REF);
|
||||||
this.realmRef = supplier.getRealmRef(annotation);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Supplier<T, A> getSupplier() {
|
public Supplier<T, A> getSupplier() {
|
||||||
|
@ -40,7 +38,4 @@ public class RequestedInstance<T, A extends Annotation> {
|
||||||
return ref;
|
return ref;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getRealmRef() {
|
|
||||||
return realmRef;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,61 +13,33 @@ public interface Supplier<T, S extends Annotation> {
|
||||||
|
|
||||||
T getValue(InstanceContext<T, S> instanceContext);
|
T getValue(InstanceContext<T, S> instanceContext);
|
||||||
|
|
||||||
default Class<?> getConfig(S annotation) {
|
default Object getAnnotationElementValue(S annotation, String annotationAttribute) {
|
||||||
if (annotation != null) {
|
if (annotation != null) {
|
||||||
Optional<Method> config = Arrays.stream(annotation.annotationType().getMethods()).filter(m -> m.getName().equals("config")).findFirst();
|
Optional<Method> annotationMethod = Arrays.stream(annotation.annotationType().getMethods()).filter(m -> m.getName().equals(annotationAttribute)).findFirst();
|
||||||
if (config.isPresent()) {
|
if (annotationMethod.isPresent()) {
|
||||||
try {
|
try {
|
||||||
return (Class<?>) config.get().invoke(annotation);
|
return annotationMethod.get().invoke(annotation);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return getAnnotationElementValue(annotationAttribute);
|
||||||
|
}
|
||||||
|
|
||||||
|
default Object getAnnotationElementValue(String annotationAttribute) {
|
||||||
|
switch (annotationAttribute) {
|
||||||
|
case SupplierHelpers.LIFECYCLE -> {
|
||||||
|
return this.getDefaultLifecycle();
|
||||||
|
}
|
||||||
|
case SupplierHelpers.REF, SupplierHelpers.REALM_REF -> {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
default -> {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
default LifeCycle getLifeCycle(S annotation) {
|
|
||||||
if (annotation != null) {
|
|
||||||
Optional<Method> lifecycle = Arrays.stream(annotation.annotationType().getMethods()).filter(m -> m.getName().equals("lifecycle")).findFirst();
|
|
||||||
if (lifecycle.isPresent()) {
|
|
||||||
try {
|
|
||||||
return (LifeCycle) lifecycle.get().invoke(annotation);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return getDefaultLifecycle();
|
|
||||||
}
|
|
||||||
|
|
||||||
default String getRef(S annotation) {
|
|
||||||
if (annotation != null) {
|
|
||||||
Optional<Method> ref = Arrays.stream(annotation.annotationType().getMethods()).filter(m -> m.getName().equals("ref")).findFirst();
|
|
||||||
if (ref.isPresent()) {
|
|
||||||
try {
|
|
||||||
return (String) ref.get().invoke(annotation);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
default String getRealmRef(S annotation) {
|
|
||||||
if (annotation != null) {
|
|
||||||
Optional<Method> realmRef = Arrays.stream(annotation.annotationType().getMethods()).filter(m -> m.getName().equals("realmRef")).findFirst();
|
|
||||||
if (realmRef.isPresent()) {
|
|
||||||
try {
|
|
||||||
return (String) realmRef.get().invoke(annotation);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
default LifeCycle getDefaultLifecycle() {
|
default LifeCycle getDefaultLifecycle() {
|
||||||
return LifeCycle.CLASS;
|
return LifeCycle.CLASS;
|
||||||
|
|
|
@ -4,6 +4,11 @@ import java.lang.reflect.Constructor;
|
||||||
|
|
||||||
public class SupplierHelpers {
|
public class SupplierHelpers {
|
||||||
|
|
||||||
|
public static final String CONFIG = "config";
|
||||||
|
public static final String LIFECYCLE = "lifecycle";
|
||||||
|
public static final String REF = "ref";
|
||||||
|
public static final String REALM_REF = "realmRef";
|
||||||
|
|
||||||
public static <T> T getInstance(Class<T> clazz) {
|
public static <T> T getInstance(Class<T> clazz) {
|
||||||
try {
|
try {
|
||||||
Constructor<T> declaredConstructor = clazz.getDeclaredConstructor();
|
Constructor<T> declaredConstructor = clazz.getDeclaredConstructor();
|
||||||
|
@ -13,5 +18,4 @@ public class SupplierHelpers {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,7 @@ public class OAuthClientSupplier implements Supplier<OAuthClient, InjectOAuthCli
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LifeCycle getLifeCycle(InjectOAuthClient annotation) {
|
public LifeCycle getDefaultLifecycle() {
|
||||||
return LifeCycle.GLOBAL;
|
return LifeCycle.GLOBAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@ public abstract class AbstractWebDriverSupplier implements Supplier<WebDriver, I
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LifeCycle getLifeCycle(InjectWebDriver annotation) {
|
public LifeCycle getDefaultLifecycle() {
|
||||||
return LifeCycle.GLOBAL;
|
return LifeCycle.GLOBAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue