/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.nacos.core.utils;

import com.alibaba.nacos.common.util.IoUtils;
import com.sun.management.OperatingSystemMXBean;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.lang.management.ManagementFactory;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.alibaba.nacos.core.utils.Constants.FUNCTION_MODE_PROPERTY_NAME;
import static com.alibaba.nacos.core.utils.Constants.STANDALONE_MODE_PROPERTY_NAME;
import static org.apache.commons.lang3.CharEncoding.UTF_8;

/**
 * @author nacos
 */
public class SystemUtils {

    /**
     * Standalone mode or not
     */
    public static final boolean STANDALONE_MODE = Boolean.getBoolean(STANDALONE_MODE_PROPERTY_NAME);

    public static final String STANDALONE_MODE_ALONE = "standalone";
    public static final String STANDALONE_MODE_CLUSTER = "cluster";

    /**
     * server
     */
    public static final String FUNCTION_MODE = System.getProperty(FUNCTION_MODE_PROPERTY_NAME);

    public static final String FUNCTION_MODE_CONFIG = "config";
    public static final String FUNCTION_MODE_NAMING = "naming";


    private static OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory
        .getOperatingSystemMXBean();

    /**
     * nacos local ip
     */
    public static final String LOCAL_IP = InetUtils.getSelfIp();


    /**
     * The key of nacos home.
     */
    public static final String NACOS_HOME_KEY = "nacos.home";

    /**
     * The home of nacos.
     */
    public static final String NACOS_HOME = getNacosHome();

    /**
     * The file path of cluster conf.
     */
    public static final String CLUSTER_CONF_FILE_PATH = getClusterConfFilePath();

    public static List<String> getIPsBySystemEnv(String key) {
        String env = getSystemEnv(key);
        List<String> ips = new ArrayList<>();
        if (StringUtils.isNotEmpty(env)) {
            ips = Arrays.asList(env.split(","));
        }
        return ips;
    }

    public static String getSystemEnv(String key) {
        return System.getenv(key);
    }

    public static float getLoad() {
        return (float) operatingSystemMXBean.getSystemLoadAverage();
    }

    public static float getCPU() {
        return (float) operatingSystemMXBean.getSystemCpuLoad();
    }

    public static float getMem() {
        return (float) (1 - (double) operatingSystemMXBean.getFreePhysicalMemorySize() / (double) operatingSystemMXBean
            .getTotalPhysicalMemorySize());
    }

    private static String getNacosHome() {
        String nacosHome = System.getProperty(NACOS_HOME_KEY);
        if (StringUtils.isBlank(nacosHome)) {
            nacosHome = System.getProperty("user.home") + File.separator + "nacos";
        }
        return nacosHome;
    }

    public static String getConfFilePath() {
        return NACOS_HOME + File.separator + "conf" + File.separator;
    }

    private static String getClusterConfFilePath() {
        return NACOS_HOME + File.separator + "conf" + File.separator + "cluster.conf";
    }

    public static List<String> readClusterConf() throws IOException {
        List<String> instanceList = new ArrayList<String>();
        Reader reader = null;

        try {
            reader = new InputStreamReader(new FileInputStream(new File(CLUSTER_CONF_FILE_PATH)),
                StandardCharsets.UTF_8);
            List<String> lines = IoUtils.readLines(reader);
            String comment = "#";
            for (String line : lines) {
                String instance = line.trim();
                if (instance.startsWith(comment)) {
                    // # it is ip
                    continue;
                }
                if (instance.contains(comment)) {
                    // 192.168.71.52:8848 # Instance A
                    instance = instance.substring(0, instance.indexOf(comment));
                    instance = instance.trim();
                }
                int multiIndex = instance.indexOf(Constants.COMMA_DIVISION);
                if (multiIndex > 0) {
                    // support the format: ip1:port,ip2:port  # multi inline
                    for (String ins : instance.split(Constants.COMMA_DIVISION)) {
                        instanceList.add(ins);
                    }
                } else {
                    //support the format: 192.168.71.52:8848
                    instanceList.add(instance);
                }
            }
            return instanceList;
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }

    public static void writeClusterConf(String content) throws IOException {
        IoUtils.writeStringToFile(new File(CLUSTER_CONF_FILE_PATH), content, UTF_8);
    }

}

@@ -3883,56 +3883,18 @@
     
+try(
 Reader
- reader = null;%0A%0A        try %7B%0A           
  rea
@@ -3976,24 +3976,16 @@
 PATH)),%0A
-        
         
@@ -4003,25 +4003,27 @@
 rsets.UTF_8)
-;
+) %7B
 %0A           
@@ -4794,26 +4794,42 @@
     
-for (String ins : 
+instanceList.addAll(Arrays.asList(
 inst
@@ -4869,79 +4869,10 @@
 ON))
- %7B%0A                        instanceList.add(ins);%0A                    %7D
+);
 %0A   
@@ -5026,32 +5026,32 @@
 %7D%0A            %7D%0A
+
             retu
@@ -5071,108 +5071,8 @@
 st;%0A
-        %7D finally %7B%0A            if (reader != null) %7B%0A                reader.close();%0A            %7D%0A
     

IO改成try resource。instanceList循环直接改成addAll