package com.fdkankan.common.config;

import ch.qos.logback.core.PropertyDefinerBase;
import com.fdkankan.common.util.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

//@Component
public class LogPathHostNameProperty extends PropertyDefinerBase {

//    @Value("${hostName.filePath:/opt/hosts/hosts.txt}")
//    private String hostNamePath;

    @Override
    public String getPropertyValue() {

        String hostNamePath = getContext().getProperty("hostName.filePath");
        if(ObjectUtils.isEmpty(hostNamePath)){
            hostNamePath = "/opt/hosts/hosts.txt";
        }
        String hostName = "null";
        try {
            hostName = FileUtils.readFile(hostNamePath);
            // 去除空格
            if(!ObjectUtils.isEmpty(hostName)){
                hostName = hostName.trim().replaceAll("\\s","");
            }
        } catch (Exception e) {
            System.err.println("=========================error======================");
            System.err.println("从文件中获取服务器名称失败,文件路径："+hostNamePath);
            return hostName;
        }
        return hostName;
    }


}
