You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.8 KiB

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