+ private static boolean loadClassPathConfigurationsAndResources() {
+ List<URL> classpathResources = ConfigurationUtils.getAllClassPathResources();
+ Predicate<URL> predicate = ConfigurationUtils::isConfig;
+ Map<Boolean, List<URL>> resources = classpathResources.stream().collect(Collectors.partitioningBy(predicate));
+ List<URL> configResources = resources.get(true);
+ List<URL> nonConfigResources = resources.get(false);
+ AtomicReference<Boolean> successFlagHolder = new AtomicReference<>(true);
+
+ configResources.forEach(url -> successFlagHolder.set(setUpdateModuleConfigStore(url)));
+
+ nonConfigResources.forEach(NON_CONFIG_RESOURCE::add);
+
+ return successFlagHolder.get();
+ }
+
+ private static boolean loadAdditionalConfigurationsAndResources() {
+ String configLocation = System.getProperty("config.location");
+ AtomicReference<Boolean> successFlagHolder = new AtomicReference<>(true);
+ if (!isBlank(configLocation)) {
+ File root = new File(configLocation);
+ Collection<File> filesystemResources = ConfigurationUtils.getAllFiles(root, true, false);
+ Predicate<File> filePredicate = ConfigurationUtils::isConfig;
+ Map<Boolean, List<File>> resources = filesystemResources.stream().collect(Collectors.partitioningBy(filePredicate));
+ List<File> configResources = resources.get(true);
+ List<File> nonConfigResources = resources.get(false);
+
+ configResources.forEach(file -> successFlagHolder.set(setUpdateModuleConfigStore(file)));
+
+ nonConfigResources.forEach(NON_CONFIG_RESOURCE::add);
+ }
+ return successFlagHolder.get();
+ }
+
+ private static boolean loadTenantConfigurations() {
+ String tenantConfigLocation = System.getProperty("tenant.config.location");
+ AtomicReference<Boolean> successFlagHolder = new AtomicReference<>(true);
+ if (!isBlank(tenantConfigLocation)) {
+ File root = new File(tenantConfigLocation);
+ Collection<File> tenantsRoot = ConfigurationUtils.getAllFiles(root, false, true);
+ Collection<File> filesystemResources = ConfigurationUtils.getAllFiles(root, true, false);
+
+ Map<Boolean, List<File>> resources = filesystemResources.stream().collect(Collectors.partitioningBy(ConfigurationUtils::isConfig));
+ Collection<File> tenantResources = resources.get(true);
+
+ tenantResources.forEach(configFile -> {
+ AtomicReference<String> moduleNameHolder = new AtomicReference<>(ConfigurationUtils.getNamespace(configFile));
+ Predicate<File> startsWithRootPredicate = tenantRoot -> configFile.getAbsolutePath().startsWith(tenantRoot.getAbsolutePath());
+ Collection<File> matchesTenantRoot = tenantsRoot.stream().filter(startsWithRootPredicate).collect(Collectors.toCollection(ArrayList::new));
+ AtomicReference<String[]> altResource = new AtomicReference<>();
+ matchesTenantRoot.forEach(file -> altResource.set(
+ (file.getName().toUpperCase() + Constants.TENANT_NAMESPACE_SEPARATOR + moduleNameHolder.get())
+ .split(Constants.TENANT_NAMESPACE_SEPARATOR)
+ )
+ );
+ successFlagHolder.set(setUpdateModuleConfigStore(configFile, altResource.get()));
+ });
+ }
+ return successFlagHolder.get();
+ }
+
+ private static void loadNodeSpecificConfigurations() {
+ String nodeConfigLocation = System.getProperty("node.config.location");
+ if (!isBlank(nodeConfigLocation)) {
+ File root = new File(nodeConfigLocation);
+ Collection<File> filesystemResources = ConfigurationUtils.getAllFiles(root, true, false);
+ filesystemResources.stream().filter(ConfigurationUtils::isConfig).forEach(
+ file -> ConfigurationRepository.lookup().populateOverrideConfiguration(
+ ConfigurationUtils.getConfigurationRepositoryKey(
+ ConfigurationUtils.getNamespace(file).split(Constants.TENANT_NAMESPACE_SEPARATOR)), file)
+ );
+ }
+ }
+
+ private static void populateFinalConfigurationIncrementally(Map<String, AggregateConfiguration> configs) {
+ if (configs.get(Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMITER + Constants.DB_NAMESPACE) != null) {
+ ConfigurationRepository.lookup().populateConfiguration(
+ Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMITER + Constants.DB_NAMESPACE,
+ configs.remove(Constants.DEFAULT_TENANT + Constants.KEY_ELEMENTS_DELIMITER + Constants.DB_NAMESPACE)
+ .getFinalConfiguration());
+ }
+ Set<String> modules = configs.keySet();
+ modules.forEach(
+ m -> ConfigurationRepository.lookup().populateConfiguration(m, configs.get(m).getFinalConfiguration())
+ );
+ }
+
+ private static <T> boolean setUpdateModuleConfigStore(T resource, String... namedResources) {
+ boolean success = true;
+ String moduleName;
+ try {
+ if (namedResources == null || namedResources.length == 0) {
+ moduleName = getConfigurationRepositoryKeyWrapper(resource);
+ } else {
+ moduleName = getConfigurationRepositoryKeyWrapper(namedResources);
+ }
+ moduleAddConfigWrapper(moduleName, resource);
+ } catch (Exception e) {
+ success = false;
+ LOGGER.error("Error occurred while processing config resource {}", resource, e);
+ }
+ return success;
+ }
+
+ private static <T> String getConfigurationRepositoryKeyWrapper(T resource) throws ConfigurationException {
+ switch (resource.getClass().getSimpleName()) {
+ case "URL":
+ return ConfigurationUtils.getConfigurationRepositoryKey((URL) resource);
+ case "File":
+ return ConfigurationUtils.getConfigurationRepositoryKey((File) resource);
+ case "String[]":
+ return ConfigurationUtils.getConfigurationRepositoryKey((String[]) resource);
+ default:
+ throw new ConfigurationException("Unsupported resource type.");
+ }
+ }
+
+ private static <T> void moduleAddConfigWrapper(String moduleName, T resource) throws ConfigurationException {
+ AggregateConfiguration moduleConfig = MODULE_CONFIG_STORE.get(moduleName);
+ if (moduleConfig == null) {
+ moduleConfig = new AggregateConfiguration();
+ MODULE_CONFIG_STORE.put(moduleName, moduleConfig);
+ }
+ switch (resource.getClass().getSimpleName()) {
+ case "URL":
+ moduleConfig.addConfig((URL) resource);
+ break;
+ case "File":
+ moduleConfig.addConfig((File) resource);
+ break;
+ default:
+ throw new ConfigurationException("Unsupported resource type.");
+ }
+ }
+
+ private void parseConfigSubKeys(String subKey, String keyPrefix, String tenantId, String namespace, Map<Object, Object> targetMap) {
+ String value = getAsString(tenantId, namespace, subKey);
+ if (!isBlank(keyPrefix) && subKey.startsWith(keyPrefix + ".")) {
+ subKey = subKey.substring(keyPrefix.trim().length() + 1);
+ }
+ while (subKey.contains(".")) {
+ String subSubKey = subKey.substring(0, subKey.indexOf('.'));
+ subKey = subKey.substring(subKey.indexOf('.') + 1);
+ if (!targetMap.containsKey(subSubKey)) {
+ Map<Object, Object> subMap = new HashMap<>();
+ targetMap.put(subSubKey, subMap);
+ targetMap = subMap;
+ } else {
+ targetMap = (Map<Object, Object>) targetMap.get(subSubKey);
+ }
+ }
+ targetMap.put(subKey, value);
+ }
+