付延余
2022-12-16 f0f8ee8c4a945adbc742d9bab69382b28ad311fb
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
package com.wgcloud.common;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.wgcloud.config.CommonConfig;
import com.wgcloud.dto.HostWarnDiyDto;
import com.wgcloud.util.UUIDUtil;
import com.wgcloud.util.msg.WarnPools;
import com.wgcloud.util.staticvar.StaticKeys;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Component;
 
import javax.servlet.ServletContext;
import java.io.File;
import java.nio.charset.Charset;
 
/**
 * @version v3.3
 * @ClassName:ApplicationContextHelper.java
 * @author: http://www.wgstart.com
 * @date: 2021年4月22日
 * @Description: springboot 容器加载完成后执行一些初始化工作
 * @Copyright: 2019-2021 wgcloud. All rights reserved.
 */
@Component
public class ApplicationStartListener implements ApplicationRunner {
    private Logger logger = LoggerFactory.getLogger(ApplicationStartListener.class);
    @Autowired
    CommonConfig commonConfig;
    @Autowired
    private ServletContext servletContext;
 
    @Override
    public void run(ApplicationArguments args) throws Exception {
        //自动闭合左侧菜单
        servletContext.setAttribute("sidebarCollapse", commonConfig.getSidebarCollapse());
        //是否开启使用主机机组
        servletContext.setAttribute("hostGroup", commonConfig.getHostGroup());
        //是否开启使用用户管理
        servletContext.setAttribute("userInfoManage", commonConfig.getUserInfoManage());
        //是否在列表页面显示告警次数
        servletContext.setAttribute("showWarnCount", commonConfig.getShowWarnCount());
        //每页显示多少条数据,读取配置文件中的pageSize
        StaticKeys.PAGE_SIZE = commonConfig.getPageSize();
        //初始化程序运行所在目录
        ApplicationHome h = new ApplicationHome(getClass());
        String jarPath = h.getSource().getParentFile().toString();
        logger.debug("jar包路径-----------------" + jarPath);
        StaticKeys.JAR_PATH = jarPath;
        //初始化下告警缓存map,这样MEM_WARN_MAP就不为null
        WarnPools.isExpireWarnTime("TEST", commonConfig.getWarnCacheTimes());
        //初始化服务接口告警map,ping设备告警map,连续失败多少次再发送告警
        WarnPools.initWarnCountMap();
        //serverInfoId,只有master节点才给服务标识一个serverInfoId,只是标识,无其他作用
        if (StaticKeys.NODE_MASTER.equals(commonConfig.getNodeType())) {
            servletContext.setAttribute("serverInfoId", UUIDUtil.getUUID());
        }
        try {
            //解析主机告警个性化配置json begin
            String path = System.getProperty("user.dir");
            File file = new File(path + "/config/hostWarnDiy.json");
            if (file.exists()) {
                logger.info("发现并解析hostWarnDiy.json");
                JSONArray hostJsonArray = JSONUtil.readJSONArray(file, Charset.forName("utf-8"));
                if (!CollectionUtil.isEmpty(hostJsonArray)) {
                    for (int i = 0; i < hostJsonArray.size(); i++) {
                        JSONObject jsonObject = JSONUtil.parseObj(hostJsonArray.get(i));
                        HostWarnDiyDto hostWarnDto = JSONUtil.toBean(jsonObject, HostWarnDiyDto.class);
                        if (StringUtils.isEmpty(hostWarnDto.getHostname())) {
                            continue;
                        }
                        StaticKeys.HOST_WARN_MAP.put(hostWarnDto.getHostname(), hostWarnDto);
                    }
                }
            }
            //解析主机告警个性化配置json end
        } catch (Exception e) {
            logger.error("hostWarnDiy.json解析错误:", e);
        }
 
        try {
            servletContext.setAttribute("webSsh", commonConfig.getWebSsh());
            if (StaticKeys.TRUE_VAL.equals(commonConfig.getWebSsh())) {
                logger.info("NettyServer服务启动,端口:" + commonConfig.getWebSshPort());
                NettyServer.start(commonConfig.getWebSshPort());
            }
        } catch (Exception e) {
            logger.error("NettyServer服务启动错误:", e);
        }
 
 
    }
 
 
}