Update generator to record the entity to fields mapping

Fixes: #15677
This commit is contained in:
Hynek Mlnařík 2022-11-24 16:42:21 +01:00 committed by Michal Hajas
parent 901c85f6c0
commit 208affe000
67 changed files with 164 additions and 126 deletions

View file

@ -11,17 +11,10 @@
<name>Keycloak Model Java Annotations and Processor</name>
<description/>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.release>11</maven.compiler.release>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>

View file

@ -32,6 +32,7 @@ import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.tools.Diagnostic;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
@ -278,6 +279,10 @@ public abstract class AbstractGenerateEntityImplementationsProcessor extends Abs
return res;
}
protected void generatedAnnotation(final PrintWriter pw) {
pw.println("@javax.annotation.processing.Generated(\"" + getClass().getName() + "\")");
}
protected static class NameFirstComparator implements Comparator<String> {
protected static final Comparator<String> ID_INSTANCE = new NameFirstComparator("id").thenComparing(Comparator.naturalOrder());
protected static final Comparator<String> GET_ID_INSTANCE = new NameFirstComparator("getId").thenComparing(Comparator.naturalOrder());

View file

@ -56,7 +56,7 @@ import javax.lang.model.type.TypeKind;
* @author hmlnarik
*/
@SupportedAnnotationTypes("org.keycloak.models.map.annotations.GenerateEntityImplementations")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
@SupportedSourceVersion(SourceVersion.RELEASE_11)
public class GenerateEntityImplementationsProcessor extends AbstractGenerateEntityImplementationsProcessor {
private static final Collection<String> autogenerated = new TreeSet<>();
@ -74,20 +74,24 @@ public class GenerateEntityImplementationsProcessor extends AbstractGenerateEnti
protected void afterAnnotationProcessing() {
if (! autogenerated.isEmpty()) {
try {
JavaFileObject file = processingEnv.getFiler().createSourceFile("org.keycloak.models.map.common.AutogeneratedCloners");
JavaFileObject file = processingEnv.getFiler().createSourceFile("org.keycloak.models.map.common.AutogeneratedClasses");
try (PrintWriter pw = new PrintWriterNoJavaLang(file.openWriter())) {
pw.println("package org.keycloak.models.map.common;");
pw.println("import " + FQN_DEEP_CLONER + ".Cloner;");
pw.println("import " + FQN_DEEP_CLONER + ".DelegateCreator;");
pw.println("import java.util.function.Function;");
pw.println("import " + FQN_DEEP_CLONER + ".EntityFieldDelegateCreator;");
pw.println("// DO NOT CHANGE THIS CLASS, IT IS GENERATED AUTOMATICALLY BY " + GenerateEntityImplementationsProcessor.class.getSimpleName());
pw.println("public final class AutogeneratedCloners {");
generatedAnnotation(pw);
pw.println("public final class AutogeneratedClasses {");
pw.println(" public static final java.util.Map<Class<?>, Cloner<?>> CLONERS_WITH_ID = new java.util.HashMap<>();");
pw.println(" public static final java.util.Map<Class<?>, Cloner<?>> CLONERS_WITHOUT_ID = new java.util.HashMap<>();");
pw.println(" public static final java.util.Map<Class<?>, DelegateCreator<?>> DELEGATE_CREATORS = new java.util.HashMap<>();");
pw.println(" public static final java.util.Map<Class<?>, EntityFieldDelegateCreator<?>> ENTITY_FIELD_DELEGATE_CREATORS = new java.util.HashMap<>();");
pw.println(" public static final java.util.Map<Class<?>, Object> EMPTY_INSTANCES = new java.util.HashMap<>();");
pw.println(" public static final java.util.Map<Class<?>, EntityField<?>[]> ENTITY_FIELDS = new java.util.HashMap<>();");
pw.println(" public static final java.util.Map<Class<?>, Function<DeepCloner, ?>> CONSTRUCTORS_DC = new java.util.HashMap<>();");
pw.println(" static {");
autogenerated.forEach(pw::println);
pw.println(" }");
@ -140,6 +144,7 @@ public class GenerateEntityImplementationsProcessor extends AbstractGenerateEnti
pw.println("package " + packageName + ";");
}
generatedAnnotation(pw);
pw.println("public enum " + mapSimpleFieldsClassName + " implements " + FQN_ENTITY_FIELD + "<" + className + "> {");
methodsPerAttribute.keySet().stream()
.sorted(NameFirstComparator.ID_INSTANCE)
@ -149,6 +154,8 @@ public class GenerateEntityImplementationsProcessor extends AbstractGenerateEnti
pw.println(" },");
});
pw.println("}");
autogenerated.add(" ENTITY_FIELDS.put(" + className + ".class, " + mapFieldsClassName + ".values());");
}
}
@ -281,29 +288,13 @@ public class GenerateEntityImplementationsProcessor extends AbstractGenerateEnti
pw.println("import java.util.Objects;");
pw.println("import " + FQN_DEEP_CLONER + ";");
pw.println("// DO NOT CHANGE THIS CLASS, IT IS GENERATED AUTOMATICALLY BY " + GenerateEntityImplementationsProcessor.class.getSimpleName());
generatedAnnotation(pw);
pw.println("public class " + mapSimpleClassName + (an.inherits().isEmpty() ? "" : " extends " + an.inherits()) + " implements " + className + " {");
// Constructors
allParentMembers.stream()
.filter(ExecutableElement.class::isInstance)
.map(ExecutableElement.class::cast)
.filter((ExecutableElement ee) -> ee.getKind() == ElementKind.CONSTRUCTOR)
.forEach((ExecutableElement ee) -> {
// Create constructor and initialize cloner to DUMB_CLONER if necessary
if (usingGeneratedCloner) {
pw.println(" /**");
pw.println(" * @deprecated This constructor uses a {@link DeepCloner#DUMB_CLONER} that does not clone anything. Use {@link #" + mapSimpleClassName + "(DeepCloner)} variant instead");
pw.println(" */");
}
pw.println(" "
+ ee.getModifiers().stream().map(Object::toString).collect(Collectors.joining(" "))
+ " " + mapSimpleClassName + "(" + methodParameters(ee.getParameters()) + ") {"
);
pw.println(" super(" + ee.getParameters() + ");");
if (usingGeneratedCloner) pw.println(" this.cloner = DeepCloner.DUMB_CLONER;");
pw.println(" }");
});
pw.println(" "
+ "private "
+ mapSimpleClassName + "() { this(DeepCloner.DUMB_CLONER); } // Nullary constructor only for Jackson deserialization"
);
pw.println(" "
+ "public "
+ mapSimpleClassName + "(DeepCloner cloner) { super(); " + (!usingGeneratedCloner ? "" : "this.cloner = cloner;") + "}"
@ -409,6 +400,7 @@ public class GenerateEntityImplementationsProcessor extends AbstractGenerateEnti
pw.println(" }");
autogenerated.add(" EMPTY_INSTANCES.put(" + className + ".class, " + mapImplClassName + ".Empty.INSTANCE);");
autogenerated.add(" CONSTRUCTORS_DC.put(" + className + ".class, " + mapImplClassName + "::new);");
pw.println("}");
}
@ -527,6 +519,7 @@ public class GenerateEntityImplementationsProcessor extends AbstractGenerateEnti
pw.println("package " + packageName + ";");
}
generatedAnnotation(pw);
pw.println("public class " + mapSimpleClassName + (an.inherits().isEmpty() ? "" : " extends " + an.inherits()) + " implements " + className + ", " + FQN_HAS_ENTITY_FIELD_DELEGATE + "<" + className + ">" + " {");
pw.println(" private final " + FQN_ENTITY_FIELD_DELEGATE + "<" + className + "> entityFieldDelegate;");
pw.println(" public " + mapSimpleClassName + "(" + FQN_ENTITY_FIELD_DELEGATE + "<" + className + "> entityFieldDelegate) {");
@ -642,11 +635,15 @@ public class GenerateEntityImplementationsProcessor extends AbstractGenerateEnti
pw.println("package " + packageName + ";");
}
pw.println("public class " + mapSimpleClassName + " implements " + className + " {");
generatedAnnotation(pw);
pw.println("public class " + mapSimpleClassName + " implements " + className + ", org.keycloak.models.map.common.delegate.HasDelegateProvider<" + className + "> {");
pw.println(" private final org.keycloak.models.map.common.delegate.DelegateProvider<" + className + "> delegateProvider;");
pw.println(" public " + mapSimpleClassName + "(org.keycloak.models.map.common.delegate.DelegateProvider<" + className + "> delegateProvider) {");
pw.println(" this.delegateProvider = delegateProvider;");
pw.println(" }");
pw.println(" public org.keycloak.models.map.common.delegate.DelegateProvider<" + className + "> getDelegateProvider() {");
pw.println(" return this.delegateProvider;");
pw.println(" }");
getAllAbstractMethods(e)
.forEach(ee -> {
@ -710,6 +707,7 @@ public class GenerateEntityImplementationsProcessor extends AbstractGenerateEnti
}
pw.println("import " + FQN_DEEP_CLONER + ";");
pw.println("// DO NOT CHANGE THIS CLASS, IT IS GENERATED AUTOMATICALLY BY " + GenerateEntityImplementationsProcessor.class.getSimpleName());
generatedAnnotation(pw);
pw.println("public class " + clonerSimpleClassName + " {");
if (methodsPerAttribute.containsKey(ID_FIELD_NAME)) {

View file

@ -76,6 +76,7 @@ public class GenerateHotRodEntityImplementationsProcessor extends AbstractGenera
pw.println("package org.keycloak.models.map.storage.hotRod.common;");
pw.println("// DO NOT CHANGE THIS CLASS, IT IS GENERATED AUTOMATICALLY BY " + GenerateHotRodEntityImplementationsProcessor.class.getSimpleName());
generatedAnnotation(pw);
pw.println("public final class AutogeneratedHotRodDescriptors {");
pw.println(" public static final java.util.Map<Class<?>, HotRodEntityDescriptor<?,?>> ENTITY_DESCRIPTOR_MAP = new java.util.HashMap<>();");
pw.println(" static {");
@ -156,6 +157,7 @@ public class GenerateHotRodEntityImplementationsProcessor extends AbstractGenera
pw.println("import java.util.stream.Collectors;");
pw.println();
pw.println("// DO NOT CHANGE THIS CLASS, IT IS GENERATED AUTOMATICALLY BY " + GenerateHotRodEntityImplementationsProcessor.class.getSimpleName());
generatedAnnotation(pw);
pw.println("public class " + hotRodSimpleClassName
+ " extends "
+ parentClassElement.getQualifiedName().toString() + (parentClassHasGeneric ? "<" + e.getQualifiedName().toString() + ">" : "")
@ -624,6 +626,7 @@ public class GenerateHotRodEntityImplementationsProcessor extends AbstractGenera
pw.println("import org.infinispan.protostream.GeneratedSchema;");
pw.println("import java.util.function.Function;");
generatedAnnotation(pw);
pw.println("public class " + descriptorSimpleClassName
+ " implements "
+ "org.keycloak.models.map.storage.hotRod.common.HotRodEntityDescriptor<" + className + ", " + className + "Delegate" + ">"

View file

@ -27,7 +27,7 @@ public class JpaAuthenticationSessionMetadata extends MapAuthenticationSessionEn
}
public JpaAuthenticationSessionMetadata() {
super();
super(DeepCloner.DUMB_CLONER);
}
private Integer entityVersion;

View file

@ -27,7 +27,7 @@ public class JpaRootAuthenticationSessionMetadata extends MapRootAuthenticationS
}
public JpaRootAuthenticationSessionMetadata() {
super();
super(DeepCloner.DUMB_CLONER);
}
private Integer entityVersion;

View file

@ -27,7 +27,7 @@ public class JpaPermissionMetadata extends MapPermissionTicketEntityImpl impleme
}
public JpaPermissionMetadata() {
super();
super(DeepCloner.DUMB_CLONER);
}
private Integer entityVersion;

View file

@ -27,7 +27,7 @@ public class JpaPolicyMetadata extends MapPolicyEntityImpl implements Serializab
}
public JpaPolicyMetadata() {
super();
super(DeepCloner.DUMB_CLONER);
}
private Integer entityVersion;

View file

@ -27,7 +27,7 @@ public class JpaResourceMetadata extends MapResourceEntityImpl implements Serial
}
public JpaResourceMetadata() {
super();
super(DeepCloner.DUMB_CLONER);
}
private Integer entityVersion;

View file

@ -27,7 +27,7 @@ public class JpaResourceServerMetadata extends MapResourceServerEntityImpl imple
}
public JpaResourceServerMetadata() {
super();
super(DeepCloner.DUMB_CLONER);
}
private Integer entityVersion;

View file

@ -27,7 +27,7 @@ public class JpaScopeMetadata extends MapScopeEntityImpl implements Serializable
}
public JpaScopeMetadata() {
super();
super(DeepCloner.DUMB_CLONER);
}
private Integer entityVersion;

View file

@ -27,7 +27,7 @@ public class JpaClientMetadata extends MapClientEntityImpl implements Serializab
}
public JpaClientMetadata() {
super();
super(DeepCloner.DUMB_CLONER);
}
private Integer entityVersion;

View file

@ -27,7 +27,7 @@ public class JpaClientScopeMetadata extends MapClientScopeEntityImpl implements
}
public JpaClientScopeMetadata() {
super();
super(DeepCloner.DUMB_CLONER);
}
private Integer entityVersion;

View file

@ -33,7 +33,7 @@ public class JpaAdminEventMetadata extends MapAdminEventEntityImpl implements Se
}
public JpaAdminEventMetadata() {
super();
super(DeepCloner.DUMB_CLONER);
}
private Integer entityVersion;

View file

@ -33,7 +33,7 @@ public class JpaAuthEventMetadata extends MapAuthEventEntityImpl implements Seri
}
public JpaAuthEventMetadata() {
super();
super(DeepCloner.DUMB_CLONER);
}
private Integer entityVersion;

View file

@ -27,7 +27,7 @@ public class JpaGroupMetadata extends MapGroupEntityImpl implements Serializable
}
public JpaGroupMetadata() {
super();
super(DeepCloner.DUMB_CLONER);
}
private Integer entityVersion;

View file

@ -33,7 +33,7 @@ public class JpaUserLoginFailureMetadata extends MapUserLoginFailureEntityImpl i
}
public JpaUserLoginFailureMetadata() {
super();
super(DeepCloner.DUMB_CLONER);
}
private Integer entityVersion;

View file

@ -33,7 +33,7 @@ public class JpaComponentMetadata extends MapComponentEntityImpl implements Seri
}
public JpaComponentMetadata() {
super();
super(DeepCloner.DUMB_CLONER);
}
private Integer entityVersion;

View file

@ -29,7 +29,7 @@ import org.keycloak.models.map.realm.MapRealmEntityImpl;
public class JpaRealmMetadata extends MapRealmEntityImpl implements Serializable {
public JpaRealmMetadata() {
super();
super(DeepCloner.DUMB_CLONER);
}
public JpaRealmMetadata(final DeepCloner cloner) {

View file

@ -27,7 +27,7 @@ public class JpaRoleMetadata extends MapRoleEntityImpl implements Serializable {
}
public JpaRoleMetadata() {
super();
super(DeepCloner.DUMB_CLONER);
}
private Integer entityVersion;

View file

@ -29,7 +29,7 @@ import org.keycloak.models.map.singleUseObject.MapSingleUseObjectEntityImpl;
public class JpaSingleUseObjectMetadata extends MapSingleUseObjectEntityImpl implements Serializable {
public JpaSingleUseObjectMetadata() {
super();
super(DeepCloner.DUMB_CLONER);
}
public JpaSingleUseObjectMetadata(final DeepCloner cloner) {

View file

@ -29,7 +29,7 @@ import org.keycloak.models.map.user.MapUserConsentEntityImpl;
public class JpaUserConsentMetadata extends MapUserConsentEntityImpl implements Serializable {
public JpaUserConsentMetadata() {
super();
super(DeepCloner.DUMB_CLONER);
}
public JpaUserConsentMetadata(final DeepCloner cloner) {

View file

@ -29,7 +29,7 @@ import org.keycloak.models.map.user.MapUserFederatedIdentityEntityImpl;
public class JpaUserFederatedIdentityMetadata extends MapUserFederatedIdentityEntityImpl implements Serializable {
public JpaUserFederatedIdentityMetadata() {
super();
super(DeepCloner.DUMB_CLONER);
}
public JpaUserFederatedIdentityMetadata(final DeepCloner cloner) {

View file

@ -29,7 +29,7 @@ import org.keycloak.models.map.user.MapUserEntityImpl;
public class JpaUserMetadata extends MapUserEntityImpl implements Serializable {
public JpaUserMetadata() {
super();
super(DeepCloner.DUMB_CLONER);
}
public JpaUserMetadata(final DeepCloner cloner) {

View file

@ -27,7 +27,7 @@ public class JpaClientSessionMetadata extends MapAuthenticatedClientSessionEntit
}
public JpaClientSessionMetadata() {
super();
super(DeepCloner.DUMB_CLONER);
}
private Integer entityVersion;

View file

@ -27,7 +27,7 @@ public class JpaUserSessionMetadata extends MapUserSessionEntityImpl implements
}
public JpaUserSessionMetadata() {
super();
super(DeepCloner.DUMB_CLONER);
}
private Integer entityVersion;

View file

@ -11,11 +11,18 @@
<name>Keycloak Model Map</name>
<description/>
<properties>
<maven.compiler.release>11</maven.compiler.release>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1-jboss-2</version>
<configuration>
<annotationProcessorPaths>
<path>

View file

@ -23,6 +23,7 @@ import org.keycloak.common.util.Time;
import org.keycloak.models.ClientModel;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.models.map.common.DeepCloner;
import org.keycloak.models.map.common.TimeAdapter;
import org.keycloak.models.utils.SessionExpiration;
import org.keycloak.sessions.AuthenticationSessionModel;
@ -105,7 +106,7 @@ public class MapRootAuthenticationSessionAdapter extends AbstractRootAuthenticat
}
}
MapAuthenticationSessionEntity authSessionEntity = new MapAuthenticationSessionEntityImpl();
MapAuthenticationSessionEntity authSessionEntity = DeepCloner.DUMB_CLONER.newInstance(MapAuthenticationSessionEntity.class);
authSessionEntity.setClientUUID(client.getId());
long timestamp = Time.currentTimeMillis();

View file

@ -17,12 +17,12 @@
package org.keycloak.models.map.authSession;
import org.jboss.logging.Logger;
import org.keycloak.cluster.ClusterProvider;
import org.keycloak.common.util.Time;
import org.keycloak.models.ClientModel;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.ModelDuplicateException;
import org.keycloak.models.RealmModel;
import org.keycloak.models.map.common.DeepCloner;
import org.keycloak.models.map.common.TimeAdapter;
import org.keycloak.models.map.storage.MapKeycloakTransaction;
import org.keycloak.models.map.storage.MapStorage;
@ -96,7 +96,7 @@ public class MapRootAuthenticationSessionProvider implements AuthenticationSessi
LOG.tracef("createRootAuthenticationSession(%s)%s", realm.getName(), getShortStackTrace());
// create map authentication session entity
MapRootAuthenticationSessionEntity entity = new MapRootAuthenticationSessionEntityImpl();
MapRootAuthenticationSessionEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapRootAuthenticationSessionEntity.class);
entity.setId(id);
entity.setRealmId(realm.getId());
long timestamp = Time.currentTimeMillis();

View file

@ -34,7 +34,7 @@ import org.keycloak.models.ModelDuplicateException;
import org.keycloak.models.RealmModel;
import org.keycloak.models.map.authorization.adapter.MapPermissionTicketAdapter;
import org.keycloak.models.map.authorization.entity.MapPermissionTicketEntity;
import org.keycloak.models.map.authorization.entity.MapPermissionTicketEntityImpl;
import org.keycloak.models.map.common.DeepCloner;
import org.keycloak.models.map.storage.MapKeycloakTransaction;
import org.keycloak.models.map.storage.MapStorage;
import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator;
@ -115,7 +115,7 @@ public class MapPermissionTicketStore implements PermissionTicketStore {
+ ", Resource: " + resource + ", owner: " + owner + ", scopeId: " + scope + " already exists.");
}
MapPermissionTicketEntity entity = new MapPermissionTicketEntityImpl();
MapPermissionTicketEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapPermissionTicketEntity.class);
entity.setResourceId(resource.getId());
entity.setRequester(requester);
entity.setCreatedTimestamp(Time.currentTimeMillis());

View file

@ -30,7 +30,7 @@ import org.keycloak.models.ModelDuplicateException;
import org.keycloak.models.RealmModel;
import org.keycloak.models.map.authorization.adapter.MapPolicyAdapter;
import org.keycloak.models.map.authorization.entity.MapPolicyEntity;
import org.keycloak.models.map.authorization.entity.MapPolicyEntityImpl;
import org.keycloak.models.map.common.DeepCloner;
import org.keycloak.models.map.storage.MapKeycloakTransaction;
import org.keycloak.models.map.storage.MapStorage;
import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator;
@ -89,7 +89,7 @@ public class MapPolicyStore implements PolicyStore {
}
String uid = representation.getId();
MapPolicyEntity entity = new MapPolicyEntityImpl();
MapPolicyEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapPolicyEntity.class);
entity.setId(uid);
entity.setType(representation.getType());
entity.setName(representation.getName());

View file

@ -29,7 +29,7 @@ import org.keycloak.models.ModelException;
import org.keycloak.models.RealmModel;
import org.keycloak.models.map.authorization.adapter.MapResourceServerAdapter;
import org.keycloak.models.map.authorization.entity.MapResourceServerEntity;
import org.keycloak.models.map.authorization.entity.MapResourceServerEntityImpl;
import org.keycloak.models.map.common.DeepCloner;
import org.keycloak.models.map.storage.MapKeycloakTransaction;
import org.keycloak.models.map.storage.MapStorage;
import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator;
@ -76,7 +76,7 @@ public class MapResourceServerStore implements ResourceServerStore {
throw new ModelDuplicateException("Resource server assiciated with client : " + client.getClientId() + " already exists.");
}
MapResourceServerEntity entity = new MapResourceServerEntityImpl();
MapResourceServerEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapResourceServerEntity.class);
entity.setClientId(clientId);
entity.setRealmId(client.getRealm().getId());

View file

@ -29,7 +29,7 @@ import org.keycloak.models.ModelDuplicateException;
import org.keycloak.models.RealmModel;
import org.keycloak.models.map.authorization.adapter.MapResourceAdapter;
import org.keycloak.models.map.authorization.entity.MapResourceEntity;
import org.keycloak.models.map.authorization.entity.MapResourceEntityImpl;
import org.keycloak.models.map.common.DeepCloner;
import org.keycloak.models.map.storage.MapKeycloakTransaction;
import org.keycloak.models.map.storage.MapStorage;
import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator;
@ -89,7 +89,7 @@ public class MapResourceStore implements ResourceStore {
throw new ModelDuplicateException("Resource with name '" + name + "' for " + resourceServer.getId() + " already exists for request owner " + owner);
}
MapResourceEntity entity = new MapResourceEntityImpl();
MapResourceEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapResourceEntity.class);
entity.setId(id);
entity.setName(name);
entity.setResourceServerId(resourceServer.getId());

View file

@ -28,7 +28,7 @@ import org.keycloak.models.ModelDuplicateException;
import org.keycloak.models.RealmModel;
import org.keycloak.models.map.authorization.adapter.MapScopeAdapter;
import org.keycloak.models.map.authorization.entity.MapScopeEntity;
import org.keycloak.models.map.authorization.entity.MapScopeEntityImpl;
import org.keycloak.models.map.common.DeepCloner;
import org.keycloak.models.map.storage.MapKeycloakTransaction;
import org.keycloak.models.map.storage.MapStorage;
import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator;
@ -85,7 +85,7 @@ public class MapScopeStore implements ScopeStore {
throw new ModelDuplicateException("Scope with name '" + name + "' for " + resourceServer.getId() + " already exists");
}
MapScopeEntity entity = new MapScopeEntityImpl();
MapScopeEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapScopeEntity.class);
entity.setId(id);
entity.setName(name);
entity.setResourceServerId(resourceServer.getId());

View file

@ -39,6 +39,7 @@ import org.keycloak.models.KeycloakSession;
import org.keycloak.models.ModelDuplicateException;
import org.keycloak.models.RealmModel;
import org.keycloak.models.RoleModel;
import org.keycloak.models.map.common.DeepCloner;
import org.keycloak.models.map.common.TimeAdapter;
import org.keycloak.models.map.storage.MapKeycloakTransaction;
import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator;
@ -154,7 +155,7 @@ public class MapClientProvider implements ClientProvider {
throw new ModelDuplicateException("Client with same clientId in realm " + realm.getName() + " exists: " + clientId);
}
MapClientEntity entity = new MapClientEntityImpl();
MapClientEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapClientEntity.class);
entity.setId(id);
entity.setRealmId(realm.getId());
entity.setClientId(clientId);

View file

@ -22,6 +22,7 @@ import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.keycloak.models.ProtocolMapperModel;
import org.keycloak.models.map.common.DeepCloner;
public class MapProtocolMapperUtils {
@ -38,7 +39,7 @@ public class MapProtocolMapperUtils {
}
public static MapProtocolMapperEntity fromModel(ProtocolMapperModel model) {
MapProtocolMapperEntity res = new MapProtocolMapperEntityImpl();
MapProtocolMapperEntity res = DeepCloner.DUMB_CLONER.newInstance(MapProtocolMapperEntity.class);
res.setId(model.getId());
res.setName(model.getName());
res.setProtocolMapper(model.getProtocolMapper());

View file

@ -29,6 +29,7 @@ import org.keycloak.models.ClientScopeProvider;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.ModelDuplicateException;
import org.keycloak.models.RealmModel;
import org.keycloak.models.map.common.DeepCloner;
import org.keycloak.models.map.storage.MapKeycloakTransaction;
import org.keycloak.models.map.storage.MapStorage;
import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator;
@ -93,7 +94,7 @@ public class MapClientScopeProvider implements ClientScopeProvider {
LOG.tracef("addClientScope(%s, %s, %s)%s", realm, id, name, getShortStackTrace());
MapClientScopeEntity entity = new MapClientScopeEntityImpl();
MapClientScopeEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapClientScopeEntity.class);
entity.setId(id);
entity.setRealmId(realm.getId());
entity.setName(KeycloakModelUtils.convertClientScopeName(name));

View file

@ -30,7 +30,6 @@ import java.util.Stack;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.jboss.logging.Logger;
/**
@ -111,11 +110,11 @@ public class DeepCloner {
* Builder for the {@code DeepCloner} helper class.
*/
public static class Builder {
private final Map<Class<?>, Function<DeepCloner, ?>> constructors = new HashMap<>();
private final Map<Class<?>, Cloner<?>> clonersWithId = new HashMap<>(org.keycloak.models.map.common.AutogeneratedCloners.CLONERS_WITH_ID);
private final Map<Class<?>, Cloner<?>> clonersWithoutId = new HashMap<>(org.keycloak.models.map.common.AutogeneratedCloners.CLONERS_WITHOUT_ID);
private final Map<Class<?>, DelegateCreator<?>> delegateCreators = new HashMap<>(org.keycloak.models.map.common.AutogeneratedCloners.DELEGATE_CREATORS);
private final Map<Class<?>, EntityFieldDelegateCreator<?>> entityFieldDelegateCreators = new HashMap<>(org.keycloak.models.map.common.AutogeneratedCloners.ENTITY_FIELD_DELEGATE_CREATORS);
private final Map<Class<?>, Function<DeepCloner, ?>> constructors = new HashMap<>(org.keycloak.models.map.common.AutogeneratedClasses.CONSTRUCTORS_DC);
private final Map<Class<?>, Cloner<?>> clonersWithId = new HashMap<>(org.keycloak.models.map.common.AutogeneratedClasses.CLONERS_WITH_ID);
private final Map<Class<?>, Cloner<?>> clonersWithoutId = new HashMap<>(org.keycloak.models.map.common.AutogeneratedClasses.CLONERS_WITHOUT_ID);
private final Map<Class<?>, DelegateCreator<?>> delegateCreators = new HashMap<>(org.keycloak.models.map.common.AutogeneratedClasses.DELEGATE_CREATORS);
private final Map<Class<?>, EntityFieldDelegateCreator<?>> entityFieldDelegateCreators = new HashMap<>(org.keycloak.models.map.common.AutogeneratedClasses.ENTITY_FIELD_DELEGATE_CREATORS);
private Cloner<?> genericCloner = (from, to) -> { throw new IllegalStateException("Cloner not found for class " + (from == null ? "<null>" : from.getClass())); };
/**
@ -256,7 +255,7 @@ public class DeepCloner {
private final Map<Class<?>, DelegateCreator<?>> delegateCreators;
private final Map<Class<?>, EntityFieldDelegateCreator<?>> entityFieldDelegateCreators;
private final Cloner<?> genericCloner;
private final Map<Class<?>, Object> emptyInstances = new HashMap<>(AutogeneratedCloners.EMPTY_INSTANCES);
private final Map<Class<?>, Object> emptyInstances = new HashMap<>(AutogeneratedClasses.EMPTY_INSTANCES);
private DeepCloner(Map<Class<?>, Function<DeepCloner, ?>> constructors,
Map<Class<?>, DelegateCreator<?>> delegateCreators,
@ -319,7 +318,7 @@ public class DeepCloner {
}
@SuppressWarnings("unchecked")
public <V> V entityFieldDelegate(V delegate, EntityFieldDelegate<V> delegateProvider) {
public <V extends AbstractEntity> V entityFieldDelegate(V delegate, EntityFieldDelegate<V> delegateProvider) {
return entityFieldDelegate((Class<V>) delegate.getClass(), delegateProvider);
}
@ -358,8 +357,8 @@ public class DeepCloner {
Function<DeepCloner, V> c = (Function<DeepCloner, V>) getFromClassRespectingHierarchy(this.constructors, clazz);
if (c == null) {
try {
res = clazz.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
res = clazz.getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException ex) {
res = null;
}
} else {

View file

@ -0,0 +1,21 @@
/*
* Copyright 2022 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.keycloak.models.map.common.delegate;
public interface HasDelegateProvider<E> {
DelegateProvider<E> getDelegateProvider();
}

View file

@ -21,6 +21,7 @@ import org.keycloak.events.Event;
import org.keycloak.events.admin.AdminEvent;
import org.keycloak.events.admin.AuthDetails;
import org.keycloak.models.map.common.DeepCloner;
import java.util.Collections;
import java.util.Map;
@ -62,7 +63,7 @@ public class EventUtils {
}
public static MapAdminEventEntity modelToEntity(AdminEvent adminEvent, boolean includeRepresentation) {
MapAdminEventEntity mapAdminEvent = new MapAdminEventEntityImpl();
MapAdminEventEntity mapAdminEvent = DeepCloner.DUMB_CLONER.newInstance(MapAdminEventEntity.class);
mapAdminEvent.setId(adminEvent.getId());
mapAdminEvent.setTimestamp(adminEvent.getTime());
mapAdminEvent.setRealmId(adminEvent.getRealmId());
@ -79,7 +80,7 @@ public class EventUtils {
}
public static MapAuthEventEntity modelToEntity(Event event) {
MapAuthEventEntity eventEntity = new MapAuthEventEntityImpl();
MapAuthEventEntity eventEntity = DeepCloner.DUMB_CLONER.newInstance(MapAuthEventEntity.class);
eventEntity.setId(event.getId());
eventEntity.setTimestamp(event.getTime());
eventEntity.setType(event.getType());

View file

@ -26,6 +26,7 @@ import org.keycloak.models.KeycloakSession;
import org.keycloak.models.ModelDuplicateException;
import org.keycloak.models.RealmModel;
import org.keycloak.models.RoleModel;
import org.keycloak.models.map.common.DeepCloner;
import org.keycloak.models.map.storage.MapKeycloakTransaction;
import org.keycloak.models.map.storage.MapStorage;
@ -221,7 +222,7 @@ public class MapGroupProvider implements GroupProvider {
throw new ModelDuplicateException("Group with name '" + name + "' in realm " + realm.getName() + " already exists for requested parent" );
}
MapGroupEntity entity = new MapGroupEntityImpl();
MapGroupEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapGroupEntity.class);
entity.setId(id);
entity.setRealmId(realm.getId());
entity.setName(name);

View file

@ -21,6 +21,7 @@ import org.keycloak.models.KeycloakSession;
import org.keycloak.models.UserLoginFailureProvider;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserLoginFailureModel;
import org.keycloak.models.map.common.DeepCloner;
import org.keycloak.models.map.storage.MapKeycloakTransaction;
import org.keycloak.models.map.storage.MapStorage;
@ -78,7 +79,7 @@ public class MapUserLoginFailureProvider implements UserLoginFailureProvider {
MapUserLoginFailureEntity userLoginFailureEntity = userLoginFailureTx.read(withCriteria(mcb)).findFirst().orElse(null);
if (userLoginFailureEntity == null) {
userLoginFailureEntity = new MapUserLoginFailureEntityImpl();
userLoginFailureEntity = DeepCloner.DUMB_CLONER.newInstance(MapUserLoginFailureEntity.class);
userLoginFailureEntity.setRealmId(realm.getId());
userLoginFailureEntity.setUserId(userId);

View file

@ -31,6 +31,7 @@ import org.keycloak.models.RealmModel;
import org.keycloak.models.RealmModel.SearchableFields;
import org.keycloak.models.RealmProvider;
import org.keycloak.models.RoleModel;
import org.keycloak.models.map.common.DeepCloner;
import org.keycloak.models.map.storage.MapKeycloakTransaction;
import org.keycloak.models.map.storage.MapStorage;
import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator;
@ -77,7 +78,7 @@ public class MapRealmProvider implements RealmProvider {
LOG.tracef("createRealm(%s, %s)%s", id, name, getShortStackTrace());
MapRealmEntity entity = new MapRealmEntityImpl();
MapRealmEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapRealmEntity.class);
entity.setId(id);
entity.setName(name);

View file

@ -29,7 +29,7 @@ import org.keycloak.models.utils.KeycloakModelUtils;
public interface MapAuthenticationExecutionEntity extends UpdatableEntity, AbstractEntity {
static MapAuthenticationExecutionEntity fromModel(AuthenticationExecutionModel model) {
if (model == null) return null;
MapAuthenticationExecutionEntity entity = new MapAuthenticationExecutionEntityImpl();
MapAuthenticationExecutionEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapAuthenticationExecutionEntity.class);
String id = model.getId() == null ? KeycloakModelUtils.generateId() : model.getId();
entity.setId(id);
entity.setAuthenticator(model.getAuthenticator());

View file

@ -28,7 +28,7 @@ import org.keycloak.models.utils.KeycloakModelUtils;
@DeepCloner.Root
public interface MapAuthenticationFlowEntity extends UpdatableEntity, AbstractEntity {
static MapAuthenticationFlowEntity fromModel(AuthenticationFlowModel model) {
MapAuthenticationFlowEntity entity = new MapAuthenticationFlowEntityImpl();
MapAuthenticationFlowEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapAuthenticationFlowEntity.class);
String id = model.getId() == null ? KeycloakModelUtils.generateId() : model.getId();
entity.setId(id);
entity.setAlias(model.getAlias());

View file

@ -32,7 +32,7 @@ import java.util.Map;
public interface MapAuthenticatorConfigEntity extends UpdatableEntity, AbstractEntity {
static MapAuthenticatorConfigEntity fromModel(AuthenticatorConfigModel model) {
if (model == null) return null;
MapAuthenticatorConfigEntity entity = new MapAuthenticatorConfigEntityImpl();
MapAuthenticatorConfigEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapAuthenticatorConfigEntity.class);
String id = model.getId() == null ? KeycloakModelUtils.generateId() : model.getId();
entity.setId(id);
entity.setAlias(model.getAlias());

View file

@ -33,7 +33,7 @@ public interface MapClientInitialAccessEntity extends UpdatableEntity, AbstractE
static MapClientInitialAccessEntity createEntity(int expiration, int count) {
long currentTime = Time.currentTimeMillis();
MapClientInitialAccessEntity entity = new MapClientInitialAccessEntityImpl();
MapClientInitialAccessEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapClientInitialAccessEntity.class);
entity.setId(KeycloakModelUtils.generateId());
entity.setTimestamp(currentTime);
entity.setExpiration(expiration == 0 ? null : currentTime + TimeAdapter.fromSecondsToMilliseconds(expiration));

View file

@ -32,7 +32,7 @@ import java.util.Map;
@DeepCloner.Root
public interface MapComponentEntity extends UpdatableEntity, AbstractEntity {
static MapComponentEntity fromModel(ComponentModel model) {
MapComponentEntity entity = new MapComponentEntityImpl();
MapComponentEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapComponentEntity.class);
String id = model.getId() == null ? KeycloakModelUtils.generateId() : model.getId();
entity.setId(id);
entity.setName(model.getName());

View file

@ -32,7 +32,7 @@ import java.util.Map;
public interface MapIdentityProviderEntity extends UpdatableEntity, AbstractEntity {
static MapIdentityProviderEntity fromModel(IdentityProviderModel model) {
if (model == null) return null;
MapIdentityProviderEntity entity = new MapIdentityProviderEntityImpl();
MapIdentityProviderEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapIdentityProviderEntity.class);
String id = model.getInternalId() == null ? KeycloakModelUtils.generateId() : model.getInternalId();
entity.setId(id);
entity.setAlias(model.getAlias());

View file

@ -32,7 +32,7 @@ import java.util.Map;
public interface MapIdentityProviderMapperEntity extends UpdatableEntity, AbstractEntity {
static MapIdentityProviderMapperEntity fromModel(IdentityProviderMapperModel model) {
if (model == null) return null;
MapIdentityProviderMapperEntity entity = new MapIdentityProviderMapperEntityImpl();
MapIdentityProviderMapperEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapIdentityProviderMapperEntity.class);
String id = model.getId() == null ? KeycloakModelUtils.generateId() : model.getId();
entity.setId(id);
entity.setName(model.getName());

View file

@ -27,7 +27,7 @@ import org.keycloak.models.map.common.UpdatableEntity;
public interface MapOTPPolicyEntity extends UpdatableEntity {
static MapOTPPolicyEntity fromModel(OTPPolicy model) {
if (model == null) return null;
MapOTPPolicyEntity entity = new MapOTPPolicyEntityImpl();
MapOTPPolicyEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapOTPPolicyEntity.class);
entity.setOtpPolicyAlgorithm(model.getAlgorithm());
entity.setOtpPolicyDigits(model.getDigits());
entity.setOtpPolicyInitialCounter(model.getInitialCounter());

View file

@ -32,7 +32,7 @@ import java.util.Map;
public interface MapRequiredActionProviderEntity extends UpdatableEntity, AbstractEntity {
static MapRequiredActionProviderEntity fromModel(RequiredActionProviderModel model) {
if (model == null) return null;
MapRequiredActionProviderEntity entity = new MapRequiredActionProviderEntityImpl();
MapRequiredActionProviderEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapRequiredActionProviderEntity.class);
String id = model.getId() == null ? KeycloakModelUtils.generateId() : model.getId();
entity.setId(id);
entity.setAlias(model.getAlias());

View file

@ -27,7 +27,7 @@ import org.keycloak.models.map.common.UpdatableEntity;
public interface MapRequiredCredentialEntity extends UpdatableEntity {
static MapRequiredCredentialEntity fromModel(RequiredCredentialModel model) {
if (model == null) return null;
MapRequiredCredentialEntity entity = new MapRequiredCredentialEntityImpl();
MapRequiredCredentialEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapRequiredCredentialEntity.class);
entity.setFormLabel(model.getFormLabel());
entity.setType(model.getType());
entity.setInput(model.isInput());

View file

@ -32,7 +32,7 @@ import java.util.List;
public interface MapWebAuthnPolicyEntity extends UpdatableEntity {
static MapWebAuthnPolicyEntity fromModel(WebAuthnPolicy model) {
if (model == null) return null;
MapWebAuthnPolicyEntity entity = new MapWebAuthnPolicyEntityImpl();
MapWebAuthnPolicyEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapWebAuthnPolicyEntity.class);
entity.setRpEntityName(model.getRpEntityName());
entity.setSignatureAlgorithms(model.getSignatureAlgorithm());
entity.setRpId(model.getRpId());
@ -64,7 +64,7 @@ public interface MapWebAuthnPolicyEntity extends UpdatableEntity {
}
static MapWebAuthnPolicyEntity defaultWebAuthnPolicy() {
MapWebAuthnPolicyEntity entity = new MapWebAuthnPolicyEntityImpl();
MapWebAuthnPolicyEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapWebAuthnPolicyEntity.class);
entity.setRpEntityName(Constants.DEFAULT_WEBAUTHN_POLICY_RP_ENTITY_NAME);
entity.setSignatureAlgorithms(Arrays.asList(Constants.DEFAULT_WEBAUTHN_POLICY_SIGNATURE_ALGORITHMS.split(",")));
entity.setRpId("");

View file

@ -30,6 +30,7 @@ import org.keycloak.models.map.storage.MapKeycloakTransaction;
import org.keycloak.models.map.storage.MapStorage;
import org.keycloak.models.RoleModel.SearchableFields;
import org.keycloak.models.RoleProvider;
import org.keycloak.models.map.common.DeepCloner;
import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator;
import org.keycloak.models.map.storage.criteria.DefaultModelCriteria;
@ -65,7 +66,7 @@ public class MapRoleProvider implements RoleProvider {
LOG.tracef("addRealmRole(%s, %s, %s)%s", realm, id, name, getShortStackTrace());
MapRoleEntity entity = new MapRoleEntityImpl();
MapRoleEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapRoleEntity.class);
entity.setId(id);
entity.setRealmId(realm.getId());
entity.setName(name);
@ -122,7 +123,7 @@ public class MapRoleProvider implements RoleProvider {
LOG.tracef("addClientRole(%s, %s, %s)%s", client, id, name, getShortStackTrace());
MapRoleEntity entity = new MapRoleEntityImpl();
MapRoleEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapRoleEntity.class);
entity.setId(id);
entity.setRealmId(client.getRealm().getId());
entity.setName(name);

View file

@ -23,6 +23,7 @@ import org.keycloak.models.SingleUseObjectValueModel;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.ModelDuplicateException;
import org.keycloak.models.SingleUseObjectProvider;
import org.keycloak.models.map.common.DeepCloner;
import org.keycloak.models.map.common.TimeAdapter;
import org.keycloak.models.map.storage.MapKeycloakTransaction;
import org.keycloak.models.map.storage.MapStorage;
@ -63,7 +64,7 @@ public class MapSingleUseObjectProvider implements SingleUseObjectProvider {
throw new ModelDuplicateException("Single-use object entity exists: " + singleUseEntity.getObjectKey());
}
singleUseEntity = new MapSingleUseObjectEntityImpl();
singleUseEntity = DeepCloner.DUMB_CLONER.newInstance(MapSingleUseObjectEntity.class);
singleUseEntity.setObjectKey(key);
singleUseEntity.setExpiration(Time.currentTimeMillis() + TimeAdapter.fromSecondsToMilliseconds(lifespanSeconds));
singleUseEntity.setNotes(notes);
@ -121,7 +122,7 @@ public class MapSingleUseObjectProvider implements SingleUseObjectProvider {
if (singleUseEntity != null) {
return false;
} else {
singleUseEntity = new MapSingleUseObjectEntityImpl();
singleUseEntity = DeepCloner.DUMB_CLONER.newInstance(MapSingleUseObjectEntity.class);
singleUseEntity.setObjectKey(key);
singleUseEntity.setExpiration(Time.currentTimeMillis() + TimeAdapter.fromSecondsToMilliseconds(lifespanInSeconds));

View file

@ -38,7 +38,7 @@ public interface MapUserConsentEntity extends UpdatableEntity {
public static MapUserConsentEntity fromModel(UserConsentModel model) {
long currentTime = Time.currentTimeMillis();
MapUserConsentEntity consentEntity = new MapUserConsentEntityImpl();
MapUserConsentEntity consentEntity = DeepCloner.DUMB_CLONER.newInstance(MapUserConsentEntity.class);
consentEntity.setClientId(model.getClient().getId());
consentEntity.setCreatedDate(currentTime);
consentEntity.setLastUpdatedDate(currentTime);

View file

@ -30,7 +30,7 @@ import java.util.Comparator;
public interface MapUserCredentialEntity extends UpdatableEntity {
public static MapUserCredentialEntity fromModel(CredentialModel model) {
MapUserCredentialEntity credentialEntity = new MapUserCredentialEntityImpl();
MapUserCredentialEntity credentialEntity = DeepCloner.DUMB_CLONER.newInstance(MapUserCredentialEntity.class);
String id = model.getId() == null ? KeycloakModelUtils.generateId() : model.getId();
credentialEntity.setId(id);
credentialEntity.setCreatedDate(model.getCreatedDate());

View file

@ -28,7 +28,7 @@ public interface MapUserFederatedIdentityEntity extends UpdatableEntity {
public static MapUserFederatedIdentityEntity fromModel(FederatedIdentityModel model) {
if (model == null) return null;
MapUserFederatedIdentityEntity entity = new MapUserFederatedIdentityEntityImpl();
MapUserFederatedIdentityEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapUserFederatedIdentityEntity.class);
entity.setIdentityProvider(model.getIdentityProvider());
entity.setUserId(model.getUserId());
entity.setUserName(model.getUserName().toLowerCase());

View file

@ -46,6 +46,7 @@ import org.keycloak.models.UserConsentModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.UserModel.SearchableFields;
import org.keycloak.models.UserProvider;
import org.keycloak.models.map.common.DeepCloner;
import org.keycloak.models.map.common.TimeAdapter;
import org.keycloak.models.map.credential.MapUserCredentialManager;
import org.keycloak.models.map.storage.MapKeycloakTransactionWithAuth;
@ -343,7 +344,7 @@ public class MapUserProvider implements UserProvider {
throw new ModelDuplicateException("User exists: " + id);
}
MapUserEntity entity = new MapUserEntityImpl();
MapUserEntity entity = DeepCloner.DUMB_CLONER.newInstance(MapUserEntity.class);
entity.setId(id);
entity.setRealmId(realm.getId());
entity.setEmailConstraint(KeycloakModelUtils.generateId());

View file

@ -27,6 +27,7 @@ import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.UserSessionModel;
import org.keycloak.models.UserSessionProvider;
import org.keycloak.models.map.common.DeepCloner;
import org.keycloak.models.map.common.TimeAdapter;
import org.keycloak.models.map.storage.MapKeycloakTransaction;
import org.keycloak.models.map.storage.MapStorage;
@ -625,7 +626,7 @@ public class MapUserSessionProvider implements UserSessionProvider {
private MapUserSessionEntity createUserSessionEntityInstance(String id, String realmId, String userId, String loginUsername, String ipAddress,
String authMethod, boolean rememberMe, String brokerSessionId, String brokerUserId,
boolean offline) {
MapUserSessionEntityImpl userSessionEntity = new MapUserSessionEntityImpl();
MapUserSessionEntity userSessionEntity = DeepCloner.DUMB_CLONER.newInstance(MapUserSessionEntity.class);
userSessionEntity.setId(id);
userSessionEntity.setRealmId(realmId);
userSessionEntity.setUserId(userId);
@ -643,7 +644,7 @@ public class MapUserSessionProvider implements UserSessionProvider {
private MapAuthenticatedClientSessionEntity createAuthenticatedClientSessionEntityInstance(String id, String userSessionId, String realmId,
String clientId, boolean offline) {
MapAuthenticatedClientSessionEntityImpl clientSessionEntity = new MapAuthenticatedClientSessionEntityImpl();
MapAuthenticatedClientSessionEntity clientSessionEntity = DeepCloner.DUMB_CLONER.newInstance(MapAuthenticatedClientSessionEntity.class);
clientSessionEntity.setId(id == null ? KeycloakModelUtils.generateId() : id);
clientSessionEntity.setRealmId(realmId);
clientSessionEntity.setClientId(clientId);

View file

@ -86,7 +86,7 @@ public class MapClientEntityClonerTest {
newInstance.setId("my-id");
newInstance.setClientId("a-client-id");
newInstance.setAttribute("attr", Arrays.asList("aa", "bb", "cc"));
MapProtocolMapperEntity pmm = new MapProtocolMapperEntityImpl();
MapProtocolMapperEntity pmm = DeepCloner.DUMB_CLONER.newInstance(MapProtocolMapperEntity.class);
pmm.setId("pmm-id");
Map<String, String> config = new HashMap<>();
config.put("key1", "value1");
@ -121,11 +121,11 @@ public class MapClientEntityClonerTest {
@Test
public void testCloneToExistingInstanceDumb() {
MapClientEntity newInstance = new MapClientEntityImpl();
MapClientEntity newInstance = DeepCloner.DUMB_CLONER.newInstance(MapClientEntity.class);
newInstance.setId("my-id");
newInstance.setClientId("a-client-id");
newInstance.setAttribute("attr", Arrays.asList("aa", "bb", "cc"));
MapProtocolMapperEntity pmm = new MapProtocolMapperEntityImpl();
MapProtocolMapperEntity pmm = DeepCloner.DUMB_CLONER.newInstance(MapProtocolMapperEntity.class);
pmm.setId("pmm-id");
Map<String, String> config = new HashMap<>();
config.put("key1", "value1");

View file

@ -18,7 +18,6 @@ package org.keycloak.models.map.common.delegate;
import org.keycloak.models.map.client.MapClientEntity;
import org.keycloak.models.map.client.MapClientEntityFields;
import org.keycloak.models.map.client.MapClientEntityImpl;
import org.keycloak.models.map.common.DeepCloner;
import org.keycloak.models.map.storage.tree.NodeProperties;
import org.keycloak.models.map.storage.tree.TreeStorageNodeInstance;
@ -61,8 +60,8 @@ public class PerFieldDelegateProviderCacheTest {
@Before
public void initEntities() {
upperEnt = new MapClientEntityImpl();
lowerEnt = new MapClientEntityImpl();
upperEnt = DeepCloner.DUMB_CLONER.newInstance(MapClientEntity.class);
lowerEnt = DeepCloner.DUMB_CLONER.newInstance(MapClientEntity.class);
upperEnt.setProtocol("upper-protocol");
upperEnt.addRedirectUri("upper-redirectUri-1");

View file

@ -18,7 +18,6 @@ package org.keycloak.models.map.common.delegate;
import org.keycloak.models.map.client.MapClientEntity;
import org.keycloak.models.map.client.MapClientEntityFields;
import org.keycloak.models.map.client.MapClientEntityImpl;
import org.keycloak.models.map.common.DeepCloner;
import org.keycloak.models.map.storage.tree.NodeProperties;
import org.keycloak.models.map.storage.tree.TreeStorageNodeInstance;
@ -60,8 +59,8 @@ public class PerFieldDelegateProviderPrimarySourceTest {
@Before
public void initEntities() {
upperEnt = new MapClientEntityImpl();
lowerEnt = new MapClientEntityImpl();
upperEnt = DeepCloner.DUMB_CLONER.newInstance(MapClientEntity.class);
lowerEnt = DeepCloner.DUMB_CLONER.newInstance(MapClientEntity.class);
upperEnt.setProtocol("upper-protocol");
upperEnt.addRedirectUri("upper-redirectUri-1");

View file

@ -17,6 +17,7 @@
package org.keycloak.models.map.realm;
import org.keycloak.models.map.common.DeepCloner;
import org.junit.Test;
import java.util.Arrays;
@ -39,7 +40,7 @@ import static org.hamcrest.Matchers.hasKey;
public class RealmEntityUndefinedValuesTest {
public MapRealmEntity newMapRealmEntity() {
return new MapRealmEntityImpl();
return DeepCloner.DUMB_CLONER.newInstance(MapRealmEntity.class);
}
@Test

View file

@ -41,7 +41,7 @@ public class MapUserEntityImplCredentialsOrderTest {
user = new MapUserEntityImpl(CLONER);
for (int i = 1; i <= 5; i++) {
MapUserCredentialEntity credentialModel = new MapUserCredentialEntityImpl();
MapUserCredentialEntity credentialModel = DeepCloner.DUMB_CLONER.newInstance(MapUserCredentialEntity.class);
credentialModel.setId(Integer.toString(i));
user.addCredential(credentialModel);

View file

@ -26,6 +26,7 @@ import org.keycloak.models.RealmProvider;
import org.keycloak.models.map.client.MapClientEntity;
import org.keycloak.models.map.client.MapClientEntityImpl;
import org.keycloak.models.map.client.MapClientProviderFactory;
import org.keycloak.models.map.common.DeepCloner;
import org.keycloak.models.map.storage.MapStorage;
import org.keycloak.models.map.storage.MapStorageProvider;
import org.keycloak.models.map.storage.MapStorageProviderFactory;
@ -117,13 +118,13 @@ public class ConcurrentHashMapStorageTest extends KeycloakModelTest {
assertClientDoesNotExist(storage2, idMain, kcMain, kc2);
assertClientDoesNotExist(storage2, id1, kc1, kc2);
MapClientEntity clientMain = new MapClientEntityImpl();
MapClientEntity clientMain = new MapClientEntityImpl(DeepCloner.DUMB_CLONER);
clientMain.setId(idMain);
clientMain.setRealmId(realmId);
MapClientEntity client1 = new MapClientEntityImpl();
MapClientEntity client1 = new MapClientEntityImpl(DeepCloner.DUMB_CLONER);
client1.setId(id1);
client1.setRealmId(realmId);
MapClientEntity client2 = new MapClientEntityImpl();
MapClientEntity client2 = new MapClientEntityImpl(DeepCloner.DUMB_CLONER);
client2.setId(id2);
client2.setRealmId(realmId);