package com.bsmlab.dfx.agent.config; import com.bsmlab.dfx.agent.config.datasource.DataSourceDto; import com.bsmlab.dfx.agent.task.dropbox.DropBoxDto; import com.bsmlab.dfx.agent.task.postman.PostmanDto; import com.fasterxml.jackson.databind.DatabindException; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.Resource; import org.springframework.stereotype.Component; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; @Slf4j @Component public class Settings { private final Map dataSourceDtoMap = new HashMap<>(); private String messageStorageRootPath; private Map dropBoxDtoMap = new HashMap<>(); private Resource[] mapperLocations; private int listenPort = 12345; private Map postmanDtoMap; public void loadSettingFile(String settingFilePath) { try { ObjectMapper objectMapper = new ObjectMapper(); Map settingMap = objectMapper.readValue(new File(settingFilePath), Map.class); this.mapperLocations = this.createMapperLocations(settingMap); this.listenPort = (Integer)settingMap.get("listen-port"); log.debug("settingMap: {}", settingMap); this.parseDataSources(settingMap); this.parseDropBoxes(settingMap); } catch (DatabindException e) { log.error("cannot parse a setting file. {}", settingFilePath, e); log.error(e.getMessage(), e); } catch (IOException e) { log.error("cannot read a setting file. {}", settingFilePath); log.error(e.getMessage(), e); } } @SuppressWarnings("unchecked") private void parseDataSources(Map settingMap) { List> dataSourceMapList = (List>)settingMap.get("datasource"); for(Map dataSourceMap : dataSourceMapList) { DataSourceDto dataSourceDto = DataSourceDto.builder() .dataSourceId(dataSourceMap.get("dataSourceId")).driverClassName(dataSourceMap.get("driverClassName")) .url(dataSourceMap.get("url")).username(dataSourceMap.get("username")).password(dataSourceMap.get("password")) .build(); dataSourceDtoMap.put(dataSourceDto.getDataSourceId(), dataSourceDto); } } private Resource[] createMapperLocations(Map settingMap) throws FileNotFoundException { Resource[] resources = null; if(ObjectUtils.isNotEmpty(settingMap.get("mapperLocations"))) { String[] locationStringList = (String[])settingMap.get("mapperLocations"); resources = new Resource[locationStringList.length]; int i = 0; for(String location : locationStringList) { resources[i] = new InputStreamResource(new FileInputStream(new File(location))); i++; } } return resources; } @SuppressWarnings("unchecked") private void parseDropBoxes(Map settingMap) { Map dropBoxSettingMap = (Map)settingMap.get("drop-box"); this.messageStorageRootPath = String.valueOf(dropBoxSettingMap.get("message-storage-root")); File messageStorageRootFile = new File(this.messageStorageRootPath); if(!messageStorageRootFile.exists()) { messageStorageRootFile.mkdirs(); } List> dropBoxMapList = (List>)dropBoxSettingMap.get("drop-box-list"); for(Map dropBoxMap : dropBoxMapList) { DropBoxDto dropBoxDto = DropBoxDto.builder() .dropBoxId(dropBoxMap.get("drop-box-id")).taskType(dropBoxMap.get("task-type")) .dataSourceId(dropBoxMap.get("dataSourceId")).sqlId(dropBoxMap.get("sql-id")) .saveDirectoryRoot(dropBoxMap.get("save-directory-root")) .build(); this.dropBoxDtoMap.put(dropBoxDto.getDropBoxId(), dropBoxDto); } } private void parsePostman(Map settingMap) { Map postmanDtoMap = new HashMap<>(); Map postmanSettingMap = (Map)settingMap.get("postman"); } public Resource[] getMapperLocations() { return this.mapperLocations; } public Map getDataSourceDtoMap() { return this.dataSourceDtoMap; } private Map getDropBoxDtoMap() { return this.dropBoxDtoMap; } public String getMessageStorageRootPath() { return this.messageStorageRootPath; } public DropBoxDto getDropBoxDto(String dropBoxId) { return this.dropBoxDtoMap.get(dropBoxId); } public int getListenPort() { return this.listenPort; } }