use of org.keycloak.component.ComponentModelScope in project keycloak by keycloak.
the class DefaultComponentFactoryProviderFactory method getProviderFactory.
@Override
@SuppressWarnings("unchecked")
public <T extends Provider> ProviderFactory<T> getProviderFactory(Class<T> clazz, String realmId, String componentId, Function<KeycloakSessionFactory, ComponentModel> modelGetter) {
ProviderFactory res = componentsMap.get().get(componentId);
if (res != null) {
LOG.tracef("Found cached ProviderFactory for %s in (%s, %s)", clazz, realmId, componentId);
return res;
}
// Apply the expensive operation before putting it into the cache
final ComponentModel cm;
if (modelGetter == null) {
LOG.debugf("Getting component configuration for component (%s, %s) from realm configuration", clazz, realmId, componentId);
cm = KeycloakModelUtils.getComponentModel(factory, realmId, componentId);
} else {
LOG.debugf("Getting component configuration for component (%s, %s) via provided method", realmId, componentId);
cm = modelGetter.apply(factory);
}
if (cm == null) {
return null;
}
final String provider = cm.getProviderId();
ProviderFactory<T> pf = provider == null ? factory.getProviderFactory(clazz) : factory.getProviderFactory(clazz, provider);
if (pf == null) {
// Either not found or not enabled
LOG.debugf("ProviderFactory for %s in (%s, %s) not found", clazz, realmId, componentId);
return null;
}
final ProviderFactory newFactory;
try {
newFactory = pf.getClass().getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException ex) {
LOG.warn("Cannot instantiate factory", ex);
return null;
}
Scope scope = Config.scope(factory.getSpi(clazz).getName(), provider);
ComponentModelScope configScope = new ComponentModelScope(scope, cm);
ProviderFactory<T> providerFactory;
if (this.componentCachingAvailable) {
providerFactory = componentsMap.get().computeIfAbsent(componentId, cId -> initializeFactory(clazz, realmId, componentId, newFactory, configScope));
} else {
providerFactory = initializeFactory(clazz, realmId, componentId, newFactory, configScope);
}
return providerFactory;
}
use of org.keycloak.component.ComponentModelScope in project keycloak by keycloak.
the class ConcurrentHashMapStorageProviderFactory method init.
@Override
public void init(Scope config) {
if (config instanceof ComponentModelScope) {
this.suffix = "-" + ((ComponentModelScope) config).getComponentId();
} else {
this.suffix = "";
}
final String keyType = config.get("keyType", "uuid");
defaultKeyConvertor = getKeyConvertor(keyType);
for (String name : getModelNames()) {
keyConvertors.put(name, getKeyConvertor(config.get("keyType." + name, keyType)));
}
final String dir = config.get("dir");
try {
if (dir == null || dir.trim().isEmpty()) {
LOG.warn("No directory set, created objects will not survive server restart");
this.storageDirectory = null;
} else {
File f = new File(dir);
Files.createDirectories(f.toPath());
if (f.exists()) {
this.storageDirectory = f;
} else {
LOG.warnf("Directory cannot be used, created objects will not survive server restart: %s", dir);
this.storageDirectory = null;
}
}
} catch (IOException ex) {
LOG.warnf("Directory cannot be used, created objects will not survive server restart: %s", dir);
this.storageDirectory = null;
}
}
Aggregations