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.
127 lines
5.1 KiB
127 lines
5.1 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.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<String, DataSourceDto> dataSourceDtoMap = new HashMap<>();
|
|
private String messageStorageRootPath;
|
|
private Map<String, DropBoxDto> dropBoxDtoMap = new HashMap<>();
|
|
private Resource[] mapperLocations;
|
|
private int listenPort = 12345;
|
|
private Map<String, PostmanDto> postmanDtoMap;
|
|
|
|
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);
|
|
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<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) {
|
|
Map<String, Object> dropBoxSettingMap = (Map<String, Object>)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<Map<String, String>> dropBoxMapList = (List<Map<String, String>>)dropBoxSettingMap.get("drop-box-list");
|
|
for(Map<String, String> 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<String, Object> settingMap) {
|
|
Map<String, PostmanDto> postmanDtoMap = new HashMap<>();
|
|
Map<String, Object> postmanSettingMap = (Map<String, Object>)settingMap.get("postman");
|
|
|
|
|
|
|
|
}
|
|
|
|
public Resource[] getMapperLocations() {
|
|
return this.mapperLocations;
|
|
}
|
|
|
|
public Map<String, DataSourceDto> getDataSourceDtoMap() {
|
|
return this.dataSourceDtoMap;
|
|
}
|
|
|
|
private Map<String, DropBoxDto> 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;
|
|
}
|
|
}
|