org.keycloak.quarkus.runtime.configuration.PersistedConfigSource Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2021 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.quarkus.runtime.configuration;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.function.Supplier;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import io.smallrye.config.ConfigValue;
import io.smallrye.config.PropertiesConfigSource;
import org.keycloak.quarkus.runtime.Environment;
/**
* A {@link org.eclipse.microprofile.config.spi.ConfigSource} based on the configuration properties persisted into the server
* image.
*/
public final class PersistedConfigSource extends PropertiesConfigSource {
public static final String NAME = "PersistedConfigSource";
public static final String PERSISTED_PROPERTIES = "META-INF/keycloak-persisted.properties";
private static final PersistedConfigSource INSTANCE = new PersistedConfigSource();
/**
* MicroProfile Config does not allow removing a config source when resolving properties. In order to be able
* to resolve the current (not the persisted value) value for a property, even if not explicitly set at runtime, we need
* to ignore this config source. Otherwise, default values are not resolved at runtime because the property will be
* resolved from this config source, if persisted.
*/
private static final ThreadLocal ENABLED = ThreadLocal.withInitial(() -> true);
private PersistedConfigSource() {
super(readProperties(), NAME, 200);
}
public static PersistedConfigSource getInstance() {
return INSTANCE;
}
@Override
public String getName() {
return NAME;
}
@Override
public ConfigValue getConfigValue(String propertyName) {
if (isEnabled()) {
ConfigValue value = super.getConfigValue(propertyName);
if (value != null) {
return value;
}
return super.getConfigValue(propertyName.replace(Configuration.OPTION_PART_SEPARATOR_CHAR, '.'));
}
return null;
}
@Override
public Set getPropertyNames() {
if (isEnabled()) {
return super.getPropertyNames();
}
return Set.of();
}
private static Map readProperties() {
if (Environment.isRuntimeMode()) {
InputStream fileStream = loadPersistedConfig();
if (fileStream == null) {
return Collections.emptyMap();
}
try (fileStream) {
Properties properties = new Properties();
properties.load(fileStream);
Map props = new HashMap<>();
for (Map.Entry
© 2015 - 2024 Weber Informatics LLC | Privacy Policy