package com.dy.pipIrrParamSet.paramSet;
|
|
import com.dy.pipIrrParamSet.ServerProperties;
|
import com.dy.pipIrrParamSet.console.Command;
|
import com.dy.pipIrrParamSet.util.FileUtil;
|
import org.springframework.context.annotation.Scope;
|
import org.springframework.core.env.Environment;
|
import org.springframework.stereotype.Component;
|
|
import java.nio.file.Path;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* @Author: liurunyu
|
* @Date: 2025/5/29 13:43
|
* @Description
|
*/
|
@Component
|
@Scope("prototype")
|
public class ParamSetMw {
|
|
protected void set(Environment env, String[] comIn, Command command){
|
String paramNamePre = "mw.sv" + comIn[1] + "." + comIn[2] + "." ;
|
this.set(env, comIn, command, paramNamePre) ;
|
}
|
|
private void set(Environment env, String[] comIn, Command command, String paramNamePre){
|
Map<String, String> paramMap = null ;
|
try{
|
paramMap = this.getConfigs(env, paramNamePre);
|
}catch (Exception e){
|
command.out(e.getMessage());
|
paramMap = null ;
|
}
|
if(paramMap != null){
|
boolean flag = this.setApplicationCommonMwYml(comIn, command, paramMap);
|
if(flag){
|
flag = this.setConfigProperties(comIn, command, paramMap);
|
}
|
if(flag){
|
command.out("参数设置完成");
|
}
|
}
|
}
|
private boolean setApplicationCommonMwYml(String[] comIn, Command command, Map<String, String> paramMap){
|
String filePath = ServerProperties.WorkspaceRoot
|
+ ParamKey.AbstractPathOfMGlobal
|
+ ParamKey.AbstractPathOfResources
|
+ ParamKey.FileOfApplicationCommonMwYml ;
|
Path applicationCommonMwYmlPath = FileUtil.getFilePath(filePath) ;
|
if(applicationCommonMwYmlPath == null){
|
command.out("未找到配置文件" + filePath);
|
return false ;
|
}
|
List<String> oldLines ;
|
try{
|
oldLines = FileUtil.readFile(applicationCommonMwYmlPath);
|
}catch (Exception e){
|
command.out("读取文件" + filePath + "发生异常:" + e.getMessage());
|
return false ;
|
}
|
if(oldLines == null || oldLines.size() == 0){
|
command.out("文件" + filePath + "是空文件");
|
return false ;
|
}else{
|
List<String> newLines = new ArrayList<>();
|
String newLine = null ;
|
for(String line : oldLines){
|
newLine = line ;
|
if(newLine != null && newLine.trim().startsWith("pipIrr_mw_webPort")){
|
newLine = " pipIrr_mw_webPort: " + paramMap.get(ParamKey.WebPort) + " #通信中间件中应用,不能在web模块系统中应用" ;
|
}else if(newLine != null && newLine.trim().startsWith("pipIrr_mw_actutorPort")){
|
newLine = " pipIrr_mw_actutorPort: " + paramMap.get(ParamKey.ActutorPort) + " #通信中间件中应用,不能在web模块系统中应用" ;
|
}else if(newLine != null && newLine.trim().startsWith("spring_datasource_url_dbname")){
|
newLine = " spring_datasource_url_dbname: " + paramMap.get(ParamKey.DbName) + " #数据库名称" ;
|
}
|
newLines.add(newLine);
|
}
|
if(newLines.size() > 0){
|
try {
|
FileUtil.writeFile(applicationCommonMwYmlPath, newLines);
|
}catch (Exception e){
|
command.out("写入文件" + filePath + "发生异常:" + e.getMessage());
|
return false ;
|
}
|
}
|
}
|
return true ;
|
}
|
private boolean setConfigProperties(String[] comIn, Command command, Map<String, String> paramMap){
|
String filePath = ServerProperties.WorkspaceRoot
|
+ ParamKey.AbstractPathOfMw
|
+ ParamKey.AbstractPathOfResources
|
+ ParamKey.FileOfConfigProperties ;
|
Path configPropertiesPath = FileUtil.getFilePath(filePath) ;
|
if(configPropertiesPath == null){
|
command.out("未找到配置文件" + filePath);
|
return false ;
|
}
|
List<String> oldLines = null ;
|
try{
|
oldLines = FileUtil.readFile(configPropertiesPath);
|
}catch (Exception e){
|
command.out("读取文件" + filePath + "发生异常:" + e.getMessage());
|
return false ;
|
}
|
if(oldLines == null || oldLines.size() == 0){
|
command.out("文件" + filePath + "是空文件");
|
return false ;
|
}else{
|
List<String> newLines = new ArrayList<>();
|
String newLine = null ;
|
for(String line : oldLines){
|
newLine = line ;
|
if(newLine != null && newLine.trim().startsWith("base.orgTag=")){
|
newLine = "base.orgTag=" + comIn[2] ;
|
}else if(newLine != null && newLine.trim().startsWith("tcp.port=")){
|
newLine = "tcp.port=" + paramMap.get(ParamKey.TcpPort);
|
}else if(newLine != null && newLine.trim().startsWith("base.upData.min.interval=")){
|
newLine = "base.upData.min.interval=" + paramMap.get(ParamKey.UpDataMinInterval);
|
}
|
newLines.add(newLine);
|
}
|
if(newLines.size() > 0){
|
try {
|
FileUtil.writeFile(configPropertiesPath, newLines);
|
}catch (Exception e){
|
command.out("写入文件" + filePath + "发生异常:" + e.getMessage());
|
return false ;
|
}
|
}
|
}
|
return true ;
|
}
|
|
private Map<String, String> getConfigs(Environment env, String paramNamePre) throws Exception{
|
Map<String, String> map = new HashMap<>() ;
|
String key = ParamKey.OrgTag;
|
this.getConfig(env, map, paramNamePre + key, key);
|
|
key = ParamKey.TcpPort;
|
this.getConfig(env, map, paramNamePre + key, key);
|
|
key = ParamKey.UpDataMinInterval;
|
this.getConfig(env, map, paramNamePre + key, key);
|
|
key = ParamKey.WebPort;
|
this.getConfig(env, map, paramNamePre + key, key);
|
|
key = ParamKey.ActutorPort;
|
this.getConfig(env, map, paramNamePre + key, key);
|
|
key = ParamKey.DbName;
|
this.getConfig(env, map, paramNamePre + key, key);
|
|
return map ;
|
}
|
private void getConfig(Environment env, Map<String, String> map, String paramName, String key) throws Exception{
|
String paramValue = env.getProperty(paramName);
|
if(paramValue == null){
|
throw new Exception("参数设置器未配置" + paramName);
|
}else{
|
map.put(key, paramValue) ;
|
}
|
}
|
|
}
|