package com.bsmlab.dfx.agent.config; import com.bsmlab.dfx.agent.config.datasource.DataSourceDto; import com.bsmlab.dfx.agent.task.dropbox.DropBoxDto; 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 Map dropBoxDtoMap = new HashMap<>(); private Resource[] mapperLocations; public void loadSettingFile(String settingFilePath) { try { ObjectMapper objectMapper = new ObjectMapper(); Map settingMap = objectMapper.readValue(new File(settingFilePath), Map.class); this.mapperLocations = this.createMapperLocations(settingMap); 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) { List> dropBoxMapList = (List>)settingMap.get("drop-box"); //TODO drop-box parser 개발 //TODO drop-box executor 개발 for(Map dropBoxMap : dropBoxMapList) { DropBoxDto dataSourceDto = DropBoxDto.builder() .build(); this.dropBoxDtoMap.put("", null); } } public Resource[] getMapperLocations() { return this.mapperLocations; } public Map getDataSourceDtoMap() { return this.dataSourceDtoMap; } private Map getDropBoxDtoMap() { return this.dropBoxDtoMap; } }