hefeixia
2021-02-18 5b8c95c760840f09910730943b21391e47187315
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
package chat.server;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Properties;
 
import org.apache.log4j.Logger;
 
 
public class ConfigerLoader {
 
    protected static Logger logger;
    private static ConfigerLoader instance;
    private Properties properties;
 
    static {
        logger = Logger.getLogger(ConfigerLoader.class);
    }
 
    private ConfigerLoader() {
        properties = new Properties();
    }
 
    public synchronized static ConfigerLoader getInstance() {
        if (instance == null) {
            instance = new ConfigerLoader();
        }
 
        return instance;
    }
    
    public static void staticLoad() throws Exception {
        getInstance();
        instance.load();
    }
 
    public void load() throws Exception {
        String path = Configer.getPath_MainConfig();
        File file = new File(path);
 
        loadOneFile(file);
    }
 
    private void loadOneFile(File file) throws Exception {
        try {
            logger.debug("load config file:" + file);
            
            BufferedReader reader = new BufferedReader(new FileReader(file));
            try {
                String line;
                
                while ((line = reader.readLine()) != null) {
                    if (line.startsWith("#")) {
                        continue;
                    }
                    
                    if (line.isEmpty() || line.matches("^\\s*$")) {
                        continue;
                    }
                    
                    int delimiterIdx = line.indexOf(' ');
                    String key; String value = "";
                    
                    if (delimiterIdx > 0) {
                        key = line.substring(0, delimiterIdx).trim();
                        value = line.substring(delimiterIdx).trim();
                    } else {
                        key = line.trim();
                    }
 
                    properties.put(key, value);
                }            
            }
            finally {
                reader.close();
            }
        } 
        catch (Exception e) {
            logger.error("can not load dispatch file: " + file);
            logger.error(e);
            throw e;
        }
    }
    
}