All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kz.greetgo.conf.hot.ConfigDataLoader Maven / Gradle / Ivy

There is a newer version: 2.0.5
Show newest version
package kz.greetgo.conf.hot;

import java.util.Date;
import java.util.Map;

public class ConfigDataLoader {

  static void loadConfigDataTo(Map target,
                               HotConfigDefinition configDefinition,
                               ConfigStorage configStorage, Date now) {

    try {
      new ConfigDataLoader(target, configDefinition, configStorage, now).load();
    } catch (Exception e) {
      if (e instanceof RuntimeException) throw (RuntimeException) e;
      throw new RuntimeException(e);
    }

  }

  private final Map target;
  private final HotConfigDefinition configDefinition;
  private final ConfigStorage configStorage;
  private final LoadingLines loadingLines;

  private ConfigDataLoader(Map target, HotConfigDefinition configDefinition,
                           ConfigStorage configStorage, Date now) {
    this.target = target;
    this.configDefinition = configDefinition;
    this.configStorage = configStorage;
    loadingLines = new LoadingLines(now, configDefinition.description());
  }

  private void load() throws Exception {

    for (ElementDefinition ed : configDefinition.elementList()) {
      loadingLines.putDefinition(ed);
    }

    boolean contentExists = configStorage.isConfigContentExists(configDefinition.location());
    loadingLines.setContentExists(contentExists);
    if (contentExists) {
      for (String line : configStorage.loadConfigContent(configDefinition.location()).split("\n")) {
        loadingLines.readStorageLine(line);
      }
    }

    loadingLines.saveTo(target);

    if (loadingLines.needToSave()) {
      configStorage.saveConfigContent(configDefinition.location(), loadingLines.content());
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy