Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public class MultivaluedHashMap<K, V> extends HashMap<K, List<V>>
public MultivaluedHashMap() {
}

public MultivaluedHashMap(Map<K, List<V>> map) {
if (map == null) {
throw new IllegalArgumentException("Map can not be null");
}
putAll(map);
}


public MultivaluedHashMap(MultivaluedHashMap<K, V> config) {
addAll(config);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2018 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.cache.infinispan;

import java.util.function.Function;
import java.util.function.Supplier;

/**
* Default implementation of {@link DefaultLazyLoader} that only fetches data once. This implementation is not thread-safe
* and cached data is assumed to not be shared across different threads to sync state.
*
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
*/
public class DefaultLazyLoader<S, D> implements LazyLoader<S, D> {

private final Function<S, D> loader;
private Supplier<D> fallback;
private D data;

public DefaultLazyLoader(Function<S, D> loader, Supplier<D> fallback) {
this.loader = loader;
this.fallback = fallback;
}

@Override
public D get(Supplier<S> sourceSupplier) {
if (data == null) {
S source = sourceSupplier.get();
data = source == null ? fallback.get() : this.loader.apply(source);
}
return data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,33 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
public class GroupAdapter implements GroupModel {

protected final CachedGroup cached;
protected final RealmCacheSession cacheSession;
protected final KeycloakSession keycloakSession;
protected final RealmModel realm;
private final Supplier<GroupModel> modelSupplier;
protected volatile GroupModel updated;
protected CachedGroup cached;
protected RealmCacheSession cacheSession;
protected KeycloakSession keycloakSession;
protected RealmModel realm;

public GroupAdapter(CachedGroup cached, RealmCacheSession cacheSession, KeycloakSession keycloakSession, RealmModel realm) {
this.cached = cached;
this.cacheSession = cacheSession;
this.keycloakSession = keycloakSession;
this.realm = realm;
modelSupplier = this::getGroupModel;
}

protected void getDelegateForUpdate() {
if (updated == null) {
cacheSession.registerGroupInvalidation(cached.getId());
updated = cacheSession.getRealmDelegate().getGroupById(cached.getId(), realm);
updated = modelSupplier.get();
if (updated == null) throw new IllegalStateException("Not found in database");
}
}
Expand Down Expand Up @@ -128,19 +132,19 @@ public void removeAttribute(String name) {
@Override
public String getFirstAttribute(String name) {
if (isUpdated()) return updated.getFirstAttribute(name);
return cached.getAttributes().getFirst(name);
return cached.getAttributes(modelSupplier).getFirst(name);
}

@Override
public List<String> getAttribute(String name) {
List<String> values = cached.getAttributes().get(name);
List<String> values = cached.getAttributes(modelSupplier).get(name);
if (values == null) return null;
return values;
}

@Override
public Map<String, List<String>> getAttributes() {
return cached.getAttributes();
return cached.getAttributes(modelSupplier);
}

@Override
Expand Down Expand Up @@ -178,7 +182,7 @@ public Set<RoleModel> getClientRoleMappings(ClientModel app) {
@Override
public boolean hasRole(RoleModel role) {
if (isUpdated()) return updated.hasRole(role);
if (cached.getRoleMappings().contains(role.getId())) return true;
if (cached.getRoleMappings(modelSupplier).contains(role.getId())) return true;

Set<RoleModel> mappings = getRoleMappings();
for (RoleModel mapping: mappings) {
Expand All @@ -197,7 +201,7 @@ public void grantRole(RoleModel role) {
public Set<RoleModel> getRoleMappings() {
if (isUpdated()) return updated.getRoleMappings();
Set<RoleModel> roles = new HashSet<RoleModel>();
for (String id : cached.getRoleMappings()) {
for (String id : cached.getRoleMappings(modelSupplier)) {
RoleModel roleById = keycloakSession.realms().getRoleById(id, realm);
if (roleById == null) {
// chance that role was removed, so just delegate to persistence and get user invalidated
Expand Down Expand Up @@ -233,7 +237,7 @@ public String getParentId() {
public Set<GroupModel> getSubGroups() {
if (isUpdated()) return updated.getSubGroups();
Set<GroupModel> subGroups = new HashSet<>();
for (String id : cached.getSubGroups()) {
for (String id : cached.getSubGroups(modelSupplier)) {
GroupModel subGroup = keycloakSession.realms().getGroupById(id, realm);
if (subGroup == null) {
// chance that role was removed, so just delegate to persistence and get user invalidated
Expand Down Expand Up @@ -267,4 +271,8 @@ public void removeChild(GroupModel subGroup) {
getDelegateForUpdate();
updated.removeChild(subGroup);
}

private GroupModel getGroupModel() {
return cacheSession.getRealmDelegate().getGroupById(cached.getId(), realm);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2018 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.cache.infinispan;

import java.util.function.Supplier;

/**
* <p>A functional interface that can be used to return data {@code D} from a source {@code S} where implementations are free to define how and when
* data is fetched from source as well how it is internally cached.
*
* <p>The source does not need to worry about caching data but always fetch data as demanded. The way data will actually be cached is an implementation detail.
*
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
* @see DefaultLazyLoader
*/
public interface LazyLoader<S, D> {

/**
* Returns data from the given {@code source}. Data is only fetched from {@code source} once and only if necessary, it is
* up to implementations to decide the momentum to actually fetch data from source.
*
* @param source the source from where data will be fetched.
* @return the data from source
*/
D get(Supplier<S> source);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,34 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
public class UserAdapter implements CachedUserModel {

private final Supplier<UserModel> modelSupplier;
protected final CachedUser cached;
protected final UserCacheSession userProviderCache;
protected final KeycloakSession keycloakSession;
protected final RealmModel realm;
protected volatile UserModel updated;
protected CachedUser cached;
protected UserCacheSession userProviderCache;
protected KeycloakSession keycloakSession;
protected RealmModel realm;

public UserAdapter(CachedUser cached, UserCacheSession userProvider, KeycloakSession keycloakSession, RealmModel realm) {
this.cached = cached;
this.userProviderCache = userProvider;
this.keycloakSession = keycloakSession;
this.realm = realm;
this.modelSupplier = this::getUserModel;
}

@Override
public UserModel getDelegateForUpdate() {
if (updated == null) {
userProviderCache.registerUserInvalidation(realm, cached);
updated = userProviderCache.getDelegate().getUserById(getId(), realm);
updated = modelSupplier.get();
if (updated == null) throw new IllegalStateException("Not found in database");
}
return updated;
Expand Down Expand Up @@ -147,26 +151,26 @@ public void removeAttribute(String name) {
@Override
public String getFirstAttribute(String name) {
if (updated != null) return updated.getFirstAttribute(name);
return cached.getAttributes().getFirst(name);
return cached.getAttributes(modelSupplier).getFirst(name);
}

@Override
public List<String> getAttribute(String name) {
if (updated != null) return updated.getAttribute(name);
List<String> result = cached.getAttributes().get(name);
List<String> result = cached.getAttributes(modelSupplier).get(name);
return (result == null) ? Collections.<String>emptyList() : result;
}

@Override
public Map<String, List<String>> getAttributes() {
if (updated != null) return updated.getAttributes();
return cached.getAttributes();
return cached.getAttributes(modelSupplier);
}

@Override
public Set<String> getRequiredActions() {
if (updated != null) return updated.getRequiredActions();
return cached.getRequiredActions();
return cached.getRequiredActions(modelSupplier);
}

@Override
Expand Down Expand Up @@ -301,7 +305,7 @@ public Set<RoleModel> getClientRoleMappings(ClientModel app) {
@Override
public boolean hasRole(RoleModel role) {
if (updated != null) return updated.hasRole(role);
if (cached.getRoleMappings().contains(role.getId())) return true;
if (cached.getRoleMappings(modelSupplier).contains(role.getId())) return true;

Set<RoleModel> mappings = getRoleMappings();
for (RoleModel mapping: mappings) {
Expand All @@ -320,7 +324,7 @@ public void grantRole(RoleModel role) {
public Set<RoleModel> getRoleMappings() {
if (updated != null) return updated.getRoleMappings();
Set<RoleModel> roles = new HashSet<RoleModel>();
for (String id : cached.getRoleMappings()) {
for (String id : cached.getRoleMappings(modelSupplier)) {
RoleModel roleById = keycloakSession.realms().getRoleById(id, realm);
if (roleById == null) {
// chance that role was removed, so just delete to persistence and get user invalidated
Expand All @@ -343,7 +347,7 @@ public void deleteRoleMapping(RoleModel role) {
public Set<GroupModel> getGroups() {
if (updated != null) return updated.getGroups();
Set<GroupModel> groups = new HashSet<GroupModel>();
for (String id : cached.getGroups()) {
for (String id : cached.getGroups(modelSupplier)) {
GroupModel groupModel = keycloakSession.realms().getGroupById(id, realm);
if (groupModel == null) {
// chance that role was removed, so just delete to persistence and get user invalidated
Expand Down Expand Up @@ -372,7 +376,7 @@ public void leaveGroup(GroupModel group) {
@Override
public boolean isMemberOf(GroupModel group) {
if (updated != null) return updated.isMemberOf(group);
if (cached.getGroups().contains(group.getId())) return true;
if (cached.getGroups(modelSupplier).contains(group.getId())) return true;
Set<GroupModel> roles = getGroups();
return RoleUtils.isMember(roles, group);
}
Expand All @@ -391,7 +395,7 @@ public int hashCode() {
return getId().hashCode();
}




private UserModel getUserModel() {
return userProviderCache.getDelegate().getUserById(cached.getId(), realm);
}
}
Loading