|
@@ -0,0 +1,96 @@
|
|
|
|
|
+package cz.senslog.analyzer.app;
|
|
|
|
|
+
|
|
|
|
|
+import cz.senslog.analyzer.persistence.DatabaseConfig;
|
|
|
|
|
+import cz.senslog.analyzer.provider.ProviderConfig;
|
|
|
|
|
+import cz.senslog.common.exception.UnsupportedFileException;
|
|
|
|
|
+import cz.senslog.common.util.Triple;
|
|
|
|
|
+import org.apache.logging.log4j.LogManager;
|
|
|
|
|
+import org.apache.logging.log4j.Logger;
|
|
|
|
|
+import org.yaml.snakeyaml.Yaml;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.FileNotFoundException;
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.io.InputStream;
|
|
|
|
|
+import java.nio.file.Files;
|
|
|
|
|
+import java.nio.file.Path;
|
|
|
|
|
+import java.nio.file.Paths;
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+import static java.time.ZoneOffset.UTC;
|
|
|
|
|
+
|
|
|
|
|
+public class Configuration {
|
|
|
|
|
+ private static Logger logger = LogManager.getLogger(Configuration.class);
|
|
|
|
|
+
|
|
|
|
|
+ public static Triple<DatabaseConfig, ProviderConfig, Integer> load(String fileName) throws IOException {
|
|
|
|
|
+
|
|
|
|
|
+ logger.info("Loading '{}' configuration file.", fileName);
|
|
|
|
|
+
|
|
|
|
|
+ if (!fileName.toLowerCase().endsWith(".yaml")) {
|
|
|
|
|
+ throw new UnsupportedFileException(fileName + "does not contain .yaml extension.");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Path filePath = Paths.get(fileName);
|
|
|
|
|
+ if (Files.notExists(filePath)) {
|
|
|
|
|
+ throw new FileNotFoundException(fileName + " does not exist");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Map<Object, Object> properties;
|
|
|
|
|
+
|
|
|
|
|
+ logger.debug("Opening the file '{}'.", fileName);
|
|
|
|
|
+ try (InputStream fileStream = Files.newInputStream(filePath)) {
|
|
|
|
|
+ logger.debug("Parsing the yaml file '{}'.", fileName);
|
|
|
|
|
+ properties = new Yaml().load(fileStream);
|
|
|
|
|
+ logger.debug("The configuration yaml file '{}' was parsed successfully.", fileName);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (properties == null || properties.isEmpty()) {
|
|
|
|
|
+ throw new IOException(String.format(
|
|
|
|
|
+ "The configuration yaml file %s is empty or was not loaded successfully. ", fileName
|
|
|
|
|
+ ));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Object dbMap = properties.get("database");
|
|
|
|
|
+ if (!(dbMap instanceof Map)) {
|
|
|
|
|
+ throw new IOException(String.format(
|
|
|
|
|
+ "Configuration file '%s' contains an error at 'database' attribute.", fileName
|
|
|
|
|
+ ));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Object schedulerMap = properties.get("scheduler");
|
|
|
|
|
+ if (!(schedulerMap instanceof Map)) {
|
|
|
|
|
+ throw new IOException(String.format(
|
|
|
|
|
+ "Configuration file '%s' contains an error at 'scheduler' attribute.", fileName
|
|
|
|
|
+ ));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Object serverMap = properties.get("server");
|
|
|
|
|
+ if (!(serverMap instanceof Map)) {
|
|
|
|
|
+ throw new IOException(String.format(
|
|
|
|
|
+ "Configuration file '%s' contains an error at 'server' attribute.", fileName
|
|
|
|
|
+ ));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> databaseConfigMap = (Map<String, Object>)dbMap;
|
|
|
|
|
+ DatabaseConfig databaseConfig = new DatabaseConfig(
|
|
|
|
|
+ (String)databaseConfigMap.get("url"),
|
|
|
|
|
+ (String)databaseConfigMap.get("username"),
|
|
|
|
|
+ (String)databaseConfigMap.get("password"),
|
|
|
|
|
+ (Integer) databaseConfigMap.get("connectionPoolSize")
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> schedulerConfigMap = (Map<String, Object>)schedulerMap;
|
|
|
|
|
+ LocalDateTime initDate = ((Date)schedulerConfigMap.get("initDate")).toInstant().atZone(UTC).toLocalDateTime();
|
|
|
|
|
+ ProviderConfig providerConfig = ProviderConfig.config()
|
|
|
|
|
+ .startDateTime(initDate)
|
|
|
|
|
+ .period((Integer)schedulerConfigMap.get("period"))
|
|
|
|
|
+ .get();
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> serverConfigMap = (Map<String, Object>)serverMap;
|
|
|
|
|
+ Integer port = (Integer)serverConfigMap.get("port");
|
|
|
|
|
+
|
|
|
|
|
+ return Triple.of(databaseConfig, providerConfig, port);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|