org.openmetadata.service.resources.settings.SettingsCache Maven / Gradle / Ivy
/*
* Copyright 2021 Collate
* 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.openmetadata.service.resources.settings;
import static org.openmetadata.schema.settings.SettingsType.CUSTOM_UI_THEME_PREFERENCE;
import static org.openmetadata.schema.settings.SettingsType.EMAIL_CONFIGURATION;
import static org.openmetadata.schema.settings.SettingsType.LOGIN_CONFIGURATION;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java.util.concurrent.TimeUnit;
import javax.annotation.CheckForNull;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.openmetadata.api.configuration.LogoConfiguration;
import org.openmetadata.api.configuration.ThemeConfiguration;
import org.openmetadata.api.configuration.UiThemePreference;
import org.openmetadata.schema.api.configuration.LoginConfiguration;
import org.openmetadata.schema.email.SmtpSettings;
import org.openmetadata.schema.settings.Settings;
import org.openmetadata.schema.settings.SettingsType;
import org.openmetadata.service.Entity;
import org.openmetadata.service.OpenMetadataApplicationConfig;
import org.openmetadata.service.exception.EntityNotFoundException;
import org.openmetadata.service.jdbi3.SystemRepository;
import org.openmetadata.service.util.JsonUtils;
@Slf4j
public class SettingsCache {
private static volatile boolean initialized = false;
protected static final LoadingCache CACHE =
CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterWrite(3, TimeUnit.MINUTES)
.build(new SettingsLoader());
protected static SystemRepository systemRepository;
private SettingsCache() {
// Private constructor for singleton
}
// Expected to be called only once from the DefaultAuthorizer
public static void initialize(OpenMetadataApplicationConfig config) {
if (!initialized) {
systemRepository = Entity.getSystemRepository();
initialized = true;
createDefaultConfiguration(config);
}
}
private static void createDefaultConfiguration(OpenMetadataApplicationConfig applicationConfig) {
// Initialise Email Setting
Settings storedSettings = systemRepository.getConfigWithKey(EMAIL_CONFIGURATION.toString());
if (storedSettings == null) {
// Only in case a config doesn't exist in DB we insert it
SmtpSettings emailConfig = applicationConfig.getSmtpSettings();
Settings setting =
new Settings().withConfigType(EMAIL_CONFIGURATION).withConfigValue(emailConfig);
systemRepository.createNewSetting(setting);
}
// Initialise Theme Setting
Settings storedCustomUiThemeConf =
systemRepository.getConfigWithKey(CUSTOM_UI_THEME_PREFERENCE.toString());
if (storedCustomUiThemeConf == null) {
// Only in case a config doesn't exist in DB we insert it
Settings setting =
new Settings()
.withConfigType(CUSTOM_UI_THEME_PREFERENCE)
.withConfigValue(
new UiThemePreference()
.withCustomLogoConfig(
new LogoConfiguration()
.withCustomLogoUrlPath("")
.withCustomFaviconUrlPath("")
.withCustomMonogramUrlPath(""))
.withCustomTheme(
new ThemeConfiguration()
.withPrimaryColor("")
.withSuccessColor("")
.withErrorColor("")
.withWarningColor("")
.withInfoColor("")));
systemRepository.createNewSetting(setting);
}
// Initialise Login Configuration
// Initialise Logo Setting
Settings storedLoginConf = systemRepository.getConfigWithKey(LOGIN_CONFIGURATION.toString());
if (storedLoginConf == null) {
// Only in case a config doesn't exist in DB we insert it
Settings setting =
new Settings()
.withConfigType(LOGIN_CONFIGURATION)
.withConfigValue(
new LoginConfiguration()
.withMaxLoginFailAttempts(3)
.withAccessBlockTime(600)
.withJwtTokenExpiryTime(3600));
systemRepository.createNewSetting(setting);
}
}
public static T getSetting(SettingsType settingName, Class clazz) {
try {
String json = JsonUtils.pojoToJson(CACHE.get(settingName.toString()).getConfigValue());
return JsonUtils.readValue(json, clazz);
} catch (Exception ex) {
LOG.error("Failed to fetch Settings . Setting {}", settingName, ex);
throw new EntityNotFoundException("Setting not found");
}
}
public static void cleanUp() {
CACHE.invalidateAll();
initialized = false;
}
public static void invalidateSettings(String settingsName) {
try {
CACHE.invalidate(settingsName);
} catch (Exception ex) {
LOG.error("Failed to invalidate cache for settings {}", settingsName, ex);
}
}
static class SettingsLoader extends CacheLoader {
@Override
public @NonNull Settings load(@CheckForNull String settingsName) {
Settings fetchedSettings;
switch (SettingsType.fromValue(settingsName)) {
case EMAIL_CONFIGURATION -> {
fetchedSettings = systemRepository.getEmailConfigInternal();
LOG.info("Loaded Email Setting");
}
case SLACK_APP_CONFIGURATION -> {
// Only if available
fetchedSettings = systemRepository.getSlackApplicationConfigInternal();
LOG.info("Loaded Slack Application Configuration");
}
case SLACK_BOT -> {
// Only if available
fetchedSettings = systemRepository.getSlackbotConfigInternal();
LOG.info("Loaded Slack Bot Configuration");
}
case SLACK_INSTALLER -> {
// Only if available
fetchedSettings = systemRepository.getSlackInstallerConfigInternal();
LOG.info("Loaded Slack Installer Configuration");
}
case SLACK_STATE -> {
// Only if available
fetchedSettings = systemRepository.getSlackStateConfigInternal();
LOG.info("Loaded Slack state Configuration");
}
default -> {
fetchedSettings = systemRepository.getConfigWithKey(settingsName);
LOG.info("Loaded Setting {}", fetchedSettings.getConfigType());
}
}
return fetchedSettings;
}
}
}