diff --git a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/InfinispanAuthenticationSessionProvider.java b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/InfinispanAuthenticationSessionProvider.java index 051a377ae3..ca9f981876 100644 --- a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/InfinispanAuthenticationSessionProvider.java +++ b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/InfinispanAuthenticationSessionProvider.java @@ -76,8 +76,7 @@ public class InfinispanAuthenticationSessionProvider implements AuthenticationSe @Override public RootAuthenticationSessionModel createRootAuthenticationSession(RealmModel realm, String id) { - RootAuthenticationSessionEntity entity = new RootAuthenticationSessionEntity(); - entity.setId(id); + RootAuthenticationSessionEntity entity = new RootAuthenticationSessionEntity(id); entity.setRealmId(realm.getId()); entity.setTimestamp(Time.currentTime()); diff --git a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/InfinispanUserSessionProvider.java b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/InfinispanUserSessionProvider.java index 70504b5fb2..db20044ee4 100755 --- a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/InfinispanUserSessionProvider.java +++ b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/InfinispanUserSessionProvider.java @@ -236,8 +236,7 @@ public class InfinispanUserSessionProvider implements UserSessionProvider, Sessi id = keyGenerator.generateKeyString(session, sessionCache); } - UserSessionEntity entity = new UserSessionEntity(); - entity.setId(id); + UserSessionEntity entity = new UserSessionEntity(id); updateSessionEntity(entity, realm, user, loginUsername, ipAddress, authMethod, rememberMe, brokerSessionId, brokerUserId); SessionUpdateTask createSessionTask = Tasks.addIfAbsentSync(); @@ -1042,8 +1041,7 @@ public class InfinispanUserSessionProvider implements UserSessionProvider, Sessi private UserSessionEntity createUserSessionEntityInstance(UserSessionModel userSession) { - UserSessionEntity entity = new UserSessionEntity(); - entity.setId(userSession.getId()); + UserSessionEntity entity = new UserSessionEntity(userSession.getId()); entity.setRealmId(userSession.getRealm().getId()); entity.setAuthMethod(userSession.getAuthMethod()); diff --git a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/PersistentUserSessionProvider.java b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/PersistentUserSessionProvider.java index 7b568a4acd..7b2edefe9e 100755 --- a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/PersistentUserSessionProvider.java +++ b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/PersistentUserSessionProvider.java @@ -238,8 +238,7 @@ public class PersistentUserSessionProvider implements UserSessionProvider, Sessi id = keyGenerator.generateKeyString(session, sessionCache); } - UserSessionEntity entity = new UserSessionEntity(); - entity.setId(id); + UserSessionEntity entity = new UserSessionEntity(id); updateSessionEntity(entity, realm, user, loginUsername, ipAddress, authMethod, rememberMe, brokerSessionId, brokerUserId); SessionUpdateTask createSessionTask = Tasks.addIfAbsentSync(); @@ -852,8 +851,7 @@ public class PersistentUserSessionProvider implements UserSessionProvider, Sessi } private UserSessionEntity createUserSessionEntityInstance(UserSessionModel userSession) { - UserSessionEntity entity = new UserSessionEntity(); - entity.setId(userSession.getId()); + UserSessionEntity entity = new UserSessionEntity(userSession.getId()); entity.setRealmId(userSession.getRealm().getId()); entity.setAuthMethod(userSession.getAuthMethod()); diff --git a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/changes/JpaChangesPerformer.java b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/changes/JpaChangesPerformer.java index ad184dba3b..87612baf8e 100644 --- a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/changes/JpaChangesPerformer.java +++ b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/changes/JpaChangesPerformer.java @@ -550,7 +550,7 @@ public class JpaChangesPerformer implements SessionC } else { PersistentUserSessionAdapter userSessionModel = (PersistentUserSessionAdapter) userSessionPersister.loadUserSession(realm, entry.getKey().toString(), entity.isOffline()); if (userSessionModel != null) { - UserSessionEntity userSessionEntity = new UserSessionEntity() { + UserSessionEntity userSessionEntity = new UserSessionEntity(userSessionModel.getId()) { @Override public Map getNotes() { return new HashMap<>() { @@ -611,16 +611,6 @@ public class JpaChangesPerformer implements SessionC userSessionModel.setRealm(innerSession.realms().getRealm(realmId)); } - @Override - public String getId() { - return userSessionModel.getId(); - } - - @Override - public void setId(String id) { - throw new IllegalStateException("not supported"); - } - @Override public String getUser() { return userSessionModel.getUser().getId(); diff --git a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/entities/RootAuthenticationSessionEntity.java b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/entities/RootAuthenticationSessionEntity.java index dd9cbcc6bd..f6173082bf 100644 --- a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/entities/RootAuthenticationSessionEntity.java +++ b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/entities/RootAuthenticationSessionEntity.java @@ -33,11 +33,12 @@ import org.infinispan.commons.marshall.SerializeWith; @SerializeWith(RootAuthenticationSessionEntity.ExternalizerImpl.class) public class RootAuthenticationSessionEntity extends SessionEntity { - private String id; + private final String id; private int timestamp; private Map authenticationSessions = new ConcurrentHashMap<>(); - public RootAuthenticationSessionEntity() { + public RootAuthenticationSessionEntity(String id) { + this.id = id; } protected RootAuthenticationSessionEntity(String realmId, String id, int timestamp, Map authenticationSessions) { @@ -51,10 +52,6 @@ public class RootAuthenticationSessionEntity extends SessionEntity { return id; } - public void setId(String id) { - this.id = id; - } - public int getTimestamp() { return timestamp; } diff --git a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/entities/UserSessionEntity.java b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/entities/UserSessionEntity.java index 40b1e1024d..3b2ce2b787 100755 --- a/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/entities/UserSessionEntity.java +++ b/model/infinispan/src/main/java/org/keycloak/models/sessions/infinispan/entities/UserSessionEntity.java @@ -47,7 +47,7 @@ public class UserSessionEntity extends SessionEntity { // Metadata attribute, which contains the lastSessionRefresh available on remoteCache. Used in decide whether we need to write to remoteCache (DC) or not public static final String LAST_SESSION_REFRESH_REMOTE = "lsrr"; - private String id; + private final String id; private String user; @@ -68,12 +68,12 @@ public class UserSessionEntity extends SessionEntity { private UserSessionModel.State state; - public String getId() { - return id; + public UserSessionEntity(String id) { + this.id = id; } - public void setId(String id) { - this.id = id; + public String getId() { + return id; } private Map notes = new ConcurrentHashMap<>(); @@ -279,13 +279,13 @@ public class UserSessionEntity extends SessionEntity { } public UserSessionEntity readObjectVersion1(ObjectInput input) throws IOException, ClassNotFoundException { - UserSessionEntity sessionEntity = new UserSessionEntity(); - - sessionEntity.setAuthMethod(MarshallUtil.unmarshallString(input)); - sessionEntity.setBrokerSessionId(MarshallUtil.unmarshallString(input)); - sessionEntity.setBrokerUserId(MarshallUtil.unmarshallString(input)); - final String userSessionId = MarshallUtil.unmarshallString(input); - sessionEntity.setId(userSessionId); + String authMethod = MarshallUtil.unmarshallString(input); + String brokerSessionId = MarshallUtil.unmarshallString(input); + String brokerUserId = MarshallUtil.unmarshallString(input); + UserSessionEntity sessionEntity = new UserSessionEntity(MarshallUtil.unmarshallString(input)); + sessionEntity.setAuthMethod(authMethod); + sessionEntity.setBrokerSessionId(brokerSessionId); + sessionEntity.setBrokerUserId(brokerUserId); sessionEntity.setIpAddress(MarshallUtil.unmarshallString(input)); sessionEntity.setLoginUsername(MarshallUtil.unmarshallString(input)); sessionEntity.setRealmId(MarshallUtil.unmarshallString(input)); diff --git a/model/infinispan/src/test/java/org/keycloak/cluster/infinispan/ConcurrencyDistributedRemoveSessionTest.java b/model/infinispan/src/test/java/org/keycloak/cluster/infinispan/ConcurrencyDistributedRemoveSessionTest.java index bac63a0911..e853274142 100644 --- a/model/infinispan/src/test/java/org/keycloak/cluster/infinispan/ConcurrencyDistributedRemoveSessionTest.java +++ b/model/infinispan/src/test/java/org/keycloak/cluster/infinispan/ConcurrencyDistributedRemoveSessionTest.java @@ -127,8 +127,7 @@ public class ConcurrencyDistributedRemoveSessionTest { private static SessionEntityWrapper createSessionEntity(String sessionId) { // Create 100 initial sessions - UserSessionEntity session = new UserSessionEntity(); - session.setId(sessionId); + UserSessionEntity session = new UserSessionEntity(sessionId); session.setRealmId("foo"); session.setBrokerSessionId("!23123123"); session.setBrokerUserId(null); diff --git a/model/infinispan/src/test/java/org/keycloak/cluster/infinispan/ConcurrencyJDGCacheReplaceTest.java b/model/infinispan/src/test/java/org/keycloak/cluster/infinispan/ConcurrencyJDGCacheReplaceTest.java index c7f913ad29..16a468a0fb 100644 --- a/model/infinispan/src/test/java/org/keycloak/cluster/infinispan/ConcurrencyJDGCacheReplaceTest.java +++ b/model/infinispan/src/test/java/org/keycloak/cluster/infinispan/ConcurrencyJDGCacheReplaceTest.java @@ -81,8 +81,7 @@ public class ConcurrencyJDGCacheReplaceTest { Cache> cache2 = createManager(2).getCache(InfinispanConnectionProvider.USER_SESSION_CACHE_NAME); // Create initial item - UserSessionEntity session = new UserSessionEntity(); - session.setId("123"); + UserSessionEntity session = new UserSessionEntity("123"); session.setRealmId("foo"); session.setBrokerSessionId("!23123123"); session.setBrokerUserId(null); diff --git a/model/infinispan/src/test/java/org/keycloak/cluster/infinispan/ConcurrencyJDGOfflineBackupsTest.java b/model/infinispan/src/test/java/org/keycloak/cluster/infinispan/ConcurrencyJDGOfflineBackupsTest.java index 19fe4f7b12..f2094d331b 100644 --- a/model/infinispan/src/test/java/org/keycloak/cluster/infinispan/ConcurrencyJDGOfflineBackupsTest.java +++ b/model/infinispan/src/test/java/org/keycloak/cluster/infinispan/ConcurrencyJDGOfflineBackupsTest.java @@ -43,8 +43,7 @@ public class ConcurrencyJDGOfflineBackupsTest { try { // Create initial item - UserSessionEntity session = new UserSessionEntity(); - session.setId("123"); + UserSessionEntity session = new UserSessionEntity("123"); session.setRealmId("foo"); session.setBrokerSessionId("!23123123"); session.setBrokerUserId(null); diff --git a/model/infinispan/src/test/java/org/keycloak/cluster/infinispan/ConcurrencyJDGRemoveSessionTest.java b/model/infinispan/src/test/java/org/keycloak/cluster/infinispan/ConcurrencyJDGRemoveSessionTest.java index 70ccc2db7d..9606ba50f0 100644 --- a/model/infinispan/src/test/java/org/keycloak/cluster/infinispan/ConcurrencyJDGRemoveSessionTest.java +++ b/model/infinispan/src/test/java/org/keycloak/cluster/infinispan/ConcurrencyJDGRemoveSessionTest.java @@ -188,8 +188,7 @@ public class ConcurrencyJDGRemoveSessionTest { private static SessionEntityWrapper createSessionEntity(String sessionId) { // Create 100 initial sessions - UserSessionEntity session = new UserSessionEntity(); - session.setId(sessionId); + UserSessionEntity session = new UserSessionEntity(sessionId); session.setRealmId("foo"); session.setBrokerSessionId("!23123123"); session.setBrokerUserId(null); diff --git a/model/infinispan/src/test/java/org/keycloak/cluster/infinispan/RemoteCacheSessionsLoaderTest.java b/model/infinispan/src/test/java/org/keycloak/cluster/infinispan/RemoteCacheSessionsLoaderTest.java index b753144910..1f9f4823c3 100644 --- a/model/infinispan/src/test/java/org/keycloak/cluster/infinispan/RemoteCacheSessionsLoaderTest.java +++ b/model/infinispan/src/test/java/org/keycloak/cluster/infinispan/RemoteCacheSessionsLoaderTest.java @@ -63,8 +63,7 @@ public class RemoteCacheSessionsLoaderTest { for (int i=0 ; i createEntityInstance(String id) { // Create initial item - UserSessionEntity session = new UserSessionEntity(); - session.setId(id); + UserSessionEntity session = new UserSessionEntity(id); session.setRealmId("foo"); session.setBrokerSessionId("!23123123"); session.setBrokerUserId(null); diff --git a/model/infinispan/src/test/java/org/keycloak/models/sessions/infinispan/initializer/DistributedCacheWriteSkewTest.java b/model/infinispan/src/test/java/org/keycloak/models/sessions/infinispan/initializer/DistributedCacheWriteSkewTest.java index deebd88fa9..c8a86c172f 100644 --- a/model/infinispan/src/test/java/org/keycloak/models/sessions/infinispan/initializer/DistributedCacheWriteSkewTest.java +++ b/model/infinispan/src/test/java/org/keycloak/models/sessions/infinispan/initializer/DistributedCacheWriteSkewTest.java @@ -55,8 +55,7 @@ public class DistributedCacheWriteSkewTest { Cache cache2 = createManager("node2").getCache(InfinispanConnectionProvider.USER_SESSION_CACHE_NAME); // Create initial item - UserSessionEntity session = new UserSessionEntity(); - session.setId("123"); + UserSessionEntity session = new UserSessionEntity("123"); session.setRealmId("foo"); session.setBrokerSessionId("!23123123"); session.setBrokerUserId(null); diff --git a/testsuite/utils/src/main/java/org/keycloak/testsuite/util/cli/AbstractSessionCacheCommand.java b/testsuite/utils/src/main/java/org/keycloak/testsuite/util/cli/AbstractSessionCacheCommand.java index c77d7ba9f9..1b83c4308f 100644 --- a/testsuite/utils/src/main/java/org/keycloak/testsuite/util/cli/AbstractSessionCacheCommand.java +++ b/testsuite/utils/src/main/java/org/keycloak/testsuite/util/cli/AbstractSessionCacheCommand.java @@ -107,14 +107,11 @@ public abstract class AbstractSessionCacheCommand extends AbstractCommand { @Override protected void doRunCacheCommand(KeycloakSession session, Cache cache) { - UserSessionEntity userSession = new UserSessionEntity(); - String id = getArg(1); - - userSession.setId(id); + UserSessionEntity userSession = new UserSessionEntity(getArg(1)); userSession.setRealmId(getArg(2)); userSession.setLastSessionRefresh(Time.currentTime()); - cache.put(id, new SessionEntityWrapper(userSession)); + cache.put(userSession.getId(), new SessionEntityWrapper(userSession)); } @Override @@ -294,14 +291,11 @@ public abstract class AbstractSessionCacheCommand extends AbstractCommand { BatchTaskRunner.runInBatches(0, count, batchCount, session.getKeycloakSessionFactory(), (KeycloakSession batchSession, int firstInIteration, int countInIteration) -> { for (int i=0 ; i