diff options
Diffstat (limited to 'common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationRepository.java')
-rw-r--r-- | common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationRepository.java | 459 |
1 files changed, 147 insertions, 312 deletions
diff --git a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationRepository.java b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationRepository.java index 90346d1ec3..276dbe2805 100644 --- a/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationRepository.java +++ b/common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/impl/ConfigurationRepository.java @@ -23,7 +23,6 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashSet; -import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; @@ -37,354 +36,190 @@ import org.apache.commons.configuration2.ex.ConfigurationException; import org.onap.config.ConfigurationUtils; import org.onap.config.Constants; -/** - * The type Configuration repository. - */ public final class ConfigurationRepository { - /** - * The Repo. - */ - static ConfigurationRepository repo; - private static Set<String> validCallers = Collections.unmodifiableSet(new HashSet<>(Arrays - .asList(ConfigurationChangeNotifier.NotificationData.class.getName(), - ConfigurationUtils.class.getName(), CliConfigurationImpl.class.getName(), - ConfigurationChangeNotifier.class.getName(), - ConfigurationImpl.class.getName()))); - - static { - repo = new ConfigurationRepository(); - } - - private boolean dbAccessible = true; - private Set<String> tenants = new HashSet<>(); - private Set<String> namespaces = new HashSet<>(); - private LinkedHashMap<String, ConfigurationHolder> store = - new LinkedHashMap<String, ConfigurationHolder>(16, 0.75f, true) { - @Override - protected boolean removeEldestEntry(Map.Entry eldest) { - try { - return size() > getConfigurationFor(Constants.DEFAULT_TENANT, Constants.DB_NAMESPACE) - .getInt("config.size.max"); - } catch (Exception exception) { - return false; - } + private static final ConfigurationRepository repo; + + private static final Set<String> validCallers = Collections.unmodifiableSet(new HashSet<>( + Arrays.asList(ConfigurationChangeNotifier.NotificationData.class.getName(), + ConfigurationUtils.class.getName(), CliConfigurationImpl.class.getName(), + ConfigurationChangeNotifier.class.getName(), ConfigurationImpl.class.getName()))); + + static { + repo = new ConfigurationRepository(); + } + + private final Set<String> tenants = new HashSet<>(); + private final Set<String> namespaces = new HashSet<>(); + private final LinkedHashMap<String, ConfigurationHolder> store = + new LinkedHashMap<String, ConfigurationHolder>(16, 0.75f, true) { + @Override + protected boolean removeEldestEntry(Map.Entry eldest) { + try { + return size() > getConfigurationFor(Constants.DEFAULT_TENANT, Constants.DB_NAMESPACE) + .getInt("config.size.max"); + } catch (Exception exception) { + return false; + } + } + }; + + private ConfigurationRepository() { + if (repo != null) { + throw new RuntimeException("Illegal access to configuration."); } - }; - - private ConfigurationRepository() { - if (repo != null) { - throw new RuntimeException("Illegal access to configuration."); + tenants.add(Constants.DEFAULT_TENANT); + namespaces.add(Constants.DEFAULT_NAMESPACE); } - tenants.add(Constants.DEFAULT_TENANT); - namespaces.add(Constants.DEFAULT_NAMESPACE); - } - /** - * Lookup configuration repository. - * - * @return the configuration repository - */ - public static ConfigurationRepository lookup() { - if (validCallers.contains(Thread.currentThread().getStackTrace()[2].getClassName())) { - return repo; + public static ConfigurationRepository lookup() { + if (validCallers.contains(Thread.currentThread().getStackTrace()[2].getClassName())) { + return repo; + } + return null; } - return null; - } - - /** - * Gets tenants. - * - * @return the tenants - */ - public Set<String> getTenants() { - return tenants; - } - - /** - * Gets namespaces. - * - * @return the namespaces - */ - public Set<String> getNamespaces() { - return namespaces; - } - private void populateTenantsNamespace(String key, boolean sourcedFromDb) { - String[] array = key.split(Constants.KEY_ELEMENTS_DELEMETER); - if (!array[1].equalsIgnoreCase(Constants.DB_NAMESPACE)) { - if (!sourcedFromDb) { - dbAccessible = false; - } - tenants.add(array[0]); - namespaces.add(array[1]); + public Set<String> getTenants() { + return tenants; } - } - /** - * Init tenants and namespaces. - */ - public void initTenantsAndNamespaces() { - // nothing to do here, left for backward compatibility - } - - /** - * Is valid tenant boolean. - * - * @param tenant the tenant - * @return the boolean - */ - public boolean isValidTenant(String tenant) { - return tenant != null && tenants.contains(tenant.toUpperCase()); - } - - /** - * Is valid namespace boolean. - * - * @param namespace the namespace - * @return the boolean - */ - public boolean isValidNamespace(String namespace) { - return namespace != null && namespaces.contains(namespace.toUpperCase()); - } - - /** - * Gets configuration for. - * - * @param tenant the tenant - * @param namespace the namespace - * @return the configuration for - * @throws Exception the exception - */ - public Configuration getConfigurationFor(String tenant, String namespace) throws Exception { - ConfigurationHolder config; - String module = tenant + Constants.KEY_ELEMENTS_DELEMETER + namespace; - config = store.get(module); - if (config == null) { - config = new ConfigurationHolder(new BasicConfigurationBuilder<>(AgglomerateConfiguration.class)); - store.put(module, config); + public Set<String> getNamespaces() { + return namespaces; } - return config.getConfiguration(tenant + Constants.KEY_ELEMENTS_DELEMETER + namespace); - } - /** - * Populate configurtaion. - * - * @param key the key - * @param builder the builder - */ - public void populateConfigurtaion(String key, Configuration builder) { - store.put(key, new ConfigurationHolder(builder)); - populateTenantsNamespace(key, false); - } - /** - * Populate override configurtaion. - * - * @param key the key - * @param file the file - * @throws Exception the exception - */ - public void populateOverrideConfigurtaion(String key, File file) { - ConfigurationHolder holder = store.get(key); - if (holder == null) { - holder = new ConfigurationHolder(new CombinedConfiguration()); - store.put(key, holder); + public boolean isValidTenant(String tenant) { + return tenant != null && tenants.contains(tenant.toUpperCase()); } - holder.addOverrideConfiguration(file.getAbsolutePath(), - ConfigurationUtils.getConfigurationBuilder(file, true)); - populateTenantsNamespace(key, true); - } - /** - * Refresh override configurtaion for. - * - * @param key the key - * @param index the index - * @throws Exception the exception - */ - public void refreshOverrideConfigurtaionFor(String key, int index) { - ConfigurationHolder holder = store.get(key); - if (holder != null) { - holder.refreshOverrideConfiguration(index); + public boolean isValidNamespace(String namespace) { + return namespace != null && namespaces.contains(namespace.toUpperCase()); } - } - /** - * Remove override configurtaion. - * - * @param file the file - * @throws Exception the exception - */ - public void removeOverrideConfigurtaion(File file) throws Exception { - Iterator<String> iterator = new ArrayList(store.keySet()).iterator(); - while (iterator.hasNext()) { - ConfigurationHolder holder = store.get(iterator.next()); - if (holder.containsOverrideConfiguration(file.getAbsolutePath())) { - holder.removeOverrideConfiguration(file.getAbsolutePath()); - } + public Configuration getConfigurationFor(String tenant, String namespace) throws Exception { + ConfigurationHolder config; + String module = tenant + Constants.KEY_ELEMENTS_DELIMETER + namespace; + config = store.get(module); + if (config == null) { + config = new ConfigurationHolder(new BasicConfigurationBuilder<>(AgglomerateConfiguration.class)); + store.put(module, config); + } + return config.getConfiguration(tenant + Constants.KEY_ELEMENTS_DELIMETER + namespace); } - } - - private class ConfigurationHolder { - - /** - * The Builder. - */ - BasicConfigurationBuilder<Configuration> builder; - /** - * The Last configuration build time. - */ - Timestamp lastConfigurationBuildTime; - /** - * The Config. - */ - Configuration config; - /** - * The Composite. - */ - Configuration composite; - /** - * The Last config change timestamp. - */ - Timestamp lastConfigChangeTimestamp; - private Map<String, FileBasedConfigurationBuilder<FileBasedConfiguration>> - overrideConfiguration = new LinkedHashMap<>(); - - - /** - * Instantiates a new Configuration holder. - * - * @param builder the builder - */ - public ConfigurationHolder(BasicConfigurationBuilder builder) { - this.builder = builder; + public void populateConfiguration(String key, Configuration builder) { + store.put(key, new ConfigurationHolder(builder)); + populateTenantsNamespace(key, false); } - /** - * Instantiates a new Configuration holder. - * - * @param builder the builder - */ - public ConfigurationHolder(Configuration builder) { - this.config = builder; + private void populateTenantsNamespace(String key, boolean sourcedFromDb) { + String[] array = key.split(Constants.KEY_ELEMENTS_DELIMETER); + if (!array[1].equalsIgnoreCase(Constants.DB_NAMESPACE)) { + tenants.add(array[0]); + namespaces.add(array[1]); + } } - /** - * Refresh override configuration. - * - * @param index the index - */ - public void refreshOverrideConfiguration(int index) { - int count = -1; - for (FileBasedConfigurationBuilder overrides : overrideConfiguration.values()) { - try { - if (++count == index) { - overrides.save(); - overrides.resetResult(); - } - } catch (ConfigurationException exception) { - //do nothing + public void populateOverrideConfiguration(String key, File file) { + ConfigurationHolder holder = store.get(key); + if (holder == null) { + holder = new ConfigurationHolder(new CombinedConfiguration()); + store.put(key, holder); } - } + holder.addOverrideConfiguration(file.getAbsolutePath(), ConfigurationUtils.getConfigurationBuilder(file, true)); + populateTenantsNamespace(key, true); } - /** - * Add override configuration. - * - * @param path the path - * @param builder the builder - */ - public void addOverrideConfiguration(String path, - BasicConfigurationBuilder<FileBasedConfiguration> builder) { - overrideConfiguration.put(path.toUpperCase(), (FileBasedConfigurationBuilder) builder); - getEffectiveConfiguration(config, overrideConfiguration.values()); + public void refreshOverrideConfigurationFor(String key, int index) { + ConfigurationHolder holder = store.get(key); + if (holder != null) { + holder.refreshOverrideConfiguration(index); + } } - /** - * Remove override configuration. - * - * @param path the path - */ - public void removeOverrideConfiguration(String path) { - overrideConfiguration.remove(path.toUpperCase()); - getEffectiveConfiguration(config, overrideConfiguration.values()); + public void removeOverrideConfiguration(File file) { + for (String s : (Iterable<String>) new ArrayList(store.keySet())) { + ConfigurationHolder holder = store.get(s); + if (holder.containsOverrideConfiguration(file.getAbsolutePath())) { + holder.removeOverrideConfiguration(file.getAbsolutePath()); + } + } } - /** - * Contains override configuration boolean. - * - * @param path the path - * @return the boolean - */ - public boolean containsOverrideConfiguration(String path) { - return overrideConfiguration.containsKey(path.toUpperCase()); - } + private class ConfigurationHolder { - /** - * Gets configuration. - * - * @param namespace the namespace - * @return the configuration - * @throws Exception the exception - */ - public Configuration getConfiguration(String namespace) throws Exception { - if (config == null) { - config = builder.getConfiguration(); - lastConfigurationBuildTime = new Timestamp(System.currentTimeMillis()); - } else if (lastConfigurationBuildTime != null - && System.currentTimeMillis() - lastConfigurationBuildTime.getTime() - > getConfigurationFor(Constants.DEFAULT_TENANT, Constants.DB_NAMESPACE) - .getInt("config.refresh.interval")) { - Timestamp temp = getLastUpdateTimestampFor(namespace); - if ((temp != null) - && (lastConfigChangeTimestamp == null - || temp.getTime() > lastConfigChangeTimestamp.getTime())) { - builder.resetResult(); - config = builder.getConfiguration(); - lastConfigChangeTimestamp = temp; - getEffectiveConfiguration(config, overrideConfiguration.values()); + private final Map<String, FileBasedConfigurationBuilder<FileBasedConfiguration>> overrideConfiguration = + new LinkedHashMap<>(); + BasicConfigurationBuilder<Configuration> builder; + Timestamp lastConfigurationBuildTime; + Configuration config; + Configuration composite; + + public ConfigurationHolder(BasicConfigurationBuilder builder) { + this.builder = builder; } - lastConfigurationBuildTime = new Timestamp(System.currentTimeMillis()); - } - if (composite == null && overrideConfiguration.size() != 0) { - composite = getEffectiveConfiguration(config, overrideConfiguration.values()); - } - return overrideConfiguration.size() == 0 ? config : composite; - } - private Configuration getEffectiveConfiguration(Configuration configuration, - Collection<FileBasedConfigurationBuilder<FileBasedConfiguration>> list) { - try { - CompositeConfiguration cc = new CompositeConfiguration(); - for (FileBasedConfigurationBuilder<FileBasedConfiguration> b : list) { - cc.addConfiguration(b.getConfiguration()); + public ConfigurationHolder(Configuration builder) { + this.config = builder; } - cc.addConfiguration(configuration); - composite = cc; - return composite; - } catch (Exception exception) { - return null; - } - } - /** - * Gets last update timestamp for. - * - * @param namespace the namespace - * @return the last update timestamp for - */ - public Timestamp getLastUpdateTimestampFor(String namespace) { - return null; - } + public void refreshOverrideConfiguration(int index) { + int count = -1; + for (FileBasedConfigurationBuilder overrides : overrideConfiguration.values()) { + try { + if (++count == index) { + overrides.save(); + overrides.resetResult(); + } + } catch (ConfigurationException exception) { + //do nothing + } + } + } + public void addOverrideConfiguration(String path, BasicConfigurationBuilder<FileBasedConfiguration> builder) { + overrideConfiguration.put(path.toUpperCase(), (FileBasedConfigurationBuilder) builder); + getEffectiveConfiguration(config, overrideConfiguration.values()); + } - } + private Configuration getEffectiveConfiguration(Configuration configuration, + Collection<FileBasedConfigurationBuilder<FileBasedConfiguration>> list) { + try { + CompositeConfiguration cc = new CompositeConfiguration(); + for (FileBasedConfigurationBuilder<FileBasedConfiguration> b : list) { + cc.addConfiguration(b.getConfiguration()); + } + cc.addConfiguration(configuration); + composite = cc; + return composite; + } catch (Exception exception) { + return null; + } + } - public boolean isDBAccessible(){ - return dbAccessible; - } + public void removeOverrideConfiguration(String path) { + overrideConfiguration.remove(path.toUpperCase()); + getEffectiveConfiguration(config, overrideConfiguration.values()); + } + public boolean containsOverrideConfiguration(String path) { + return overrideConfiguration.containsKey(path.toUpperCase()); + } + public Configuration getConfiguration(String namespace) throws Exception { + if (config == null) { + config = builder.getConfiguration(); + lastConfigurationBuildTime = new Timestamp(System.currentTimeMillis()); + } else if (lastConfigurationBuildTime != null + && System.currentTimeMillis() - lastConfigurationBuildTime.getTime() + > getConfigurationFor(Constants.DEFAULT_TENANT, Constants.DB_NAMESPACE) + .getInt("config.refresh.interval")) { + lastConfigurationBuildTime = new Timestamp(System.currentTimeMillis()); + } + if (composite == null && overrideConfiguration.size() != 0) { + composite = getEffectiveConfiguration(config, overrideConfiguration.values()); + } + return overrideConfiguration.size() == 0 ? config : composite; + } + } } |