liurunyu
7 天以前 fcc1cf275578bd3aa08ef50c6a611fb206b7aac9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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) ;
        }
    }
 
}