wangting
2025-01-16 18c43123b51a1688ab4ae01fe3d171c7d92e619b
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
package com.vci.common.resource;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
 
import com.vci.common.log.ServerWithLog4j;
 
 
 
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2009</p>
 * <p>Company: VCI</p>
 * @author eddie
 * @time 2009-4-14
 * @version 1.0
 */
public class CommonProperties {
 
    private static String fileName = "/properties/conf.properties";
    private static String developerFileName = "properties/conf.properties";
    private static ResourceBundle _cfgResourceBundle = null;
    private static Properties confProperties = null;
 
    static {
        try {
            if ( _cfgResourceBundle == null ) {
                InputStream is = CommonProperties.class.getResourceAsStream(fileName);
                if (is != null)
                {
                    _cfgResourceBundle = new PropertyResourceBundle(is);
                }
                if (_cfgResourceBundle == null) {
                    File file = new File(developerFileName);
                    
                    if (file.exists()) {
                        FileInputStream confPropertiesStream = new FileInputStream(developerFileName);
                        confProperties = new Properties();
                        confProperties.load(confPropertiesStream);
                    }
                }
            }
        } catch(Exception ee) {
            ee.printStackTrace();
            ServerWithLog4j.logger.info("初始化服务器配置文件出错,请检查文件后重新启动");
        }
    }
    
 
    public static String getStringProperty(String strKey)
    {
        return getStrPro(_cfgResourceBundle,strKey);
    }
    
    /**
     * 获取字符串,错误时返回默认值
     * @param strKey
     * @param defaultVal
     * @return
     */
    public static String getStringProperty(String strKey, String defaultVal) {
        return getStrPro(_cfgResourceBundle,strKey, defaultVal);
    }
    
    public static int getIntProperty(String strKey)
    {
        return getIntPro(_cfgResourceBundle,strKey);
    }
    
    /**
     * 获取整数,错误时返回默认值
     * @param strKey
     * @param defaultVal
     * @return
     */
    public static int getIntProperty(String strKey, int defaultVal)
    {
        return getIntPro(_cfgResourceBundle,strKey, defaultVal);
    }
    
    private static String getStrPro(ResourceBundle _cfgResourceBundle, String key){
        String rs = null;
        try{
            if (_cfgResourceBundle != null) {
                rs = _cfgResourceBundle.getString(key).trim();
            } else if (confProperties != null){
                rs = confProperties.getProperty(key);
            }
        }catch(Exception e){
            rs = "";
        } finally {
            if (rs == null) {
                rs = "";
            }
        }
        return rs;
    }
    
    private static String getStrPro(ResourceBundle _cfgResourceBundle, String key, String defaultVal){
        String rs = null;
        try{
            if (_cfgResourceBundle != null) {
                rs = _cfgResourceBundle.getString(key).trim();
            } else if (confProperties != null){
                rs = confProperties.getProperty(key);
            }
        }catch(Exception e){
            rs = defaultVal;
        } finally {
            if (rs == null) {
                rs = defaultVal;
            }
        }
        return rs;
    }
    
    private static int getIntPro(ResourceBundle _cfgResourceBundle, String key){
        try{
            return Integer.valueOf(getStrPro(_cfgResourceBundle, key)).intValue();
        }catch(Exception e){
            return -1;
        }
    }
    
    private static int getIntPro(ResourceBundle _cfgResourceBundle, String key, int defaultVal){
        try{
            return Integer.valueOf(getStrPro(_cfgResourceBundle, key, String.valueOf(defaultVal))).intValue();
        }catch(Exception e){
            return defaultVal;
        }
    }
}