1
2
3
4
5 package gboat2.base.bridge.util;
6
7 import gboat2.base.bridge.exception.DefaultGboatNestedException;
8
9 import java.io.File;
10 import java.io.FileInputStream;
11 import java.io.FileNotFoundException;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.io.Reader;
15 import java.net.URL;
16 import java.util.Properties;
17
18 import org.apache.commons.configuration.ConfigurationException;
19 import org.apache.commons.configuration.PropertiesConfiguration;
20 import org.apache.commons.io.IOUtils;
21
22
23
24
25
26
27
28 public class PropertiesUtil {
29
30
31
32
33
34
35
36 public static Properties loadProperties(String fileName) {
37 return loadProperties(new File(fileName));
38 }
39
40
41
42
43
44
45
46 public static Properties loadProperties(File file) {
47 FileInputStream input = null;
48 try {
49 input = new FileInputStream(file);
50 return loadProperties(input);
51 } catch (FileNotFoundException e) {
52 throw new DefaultGboatNestedException("配置文件 [" + file.getAbsolutePath() + "] 不存在。", e);
53 } finally {
54 IOUtils.closeQuietly(input);
55 }
56 }
57
58
59
60
61
62
63
64 public static Properties loadProperties(URL url) {
65 InputStream input = null;
66 try {
67 input = url.openStream();
68 return loadProperties(input);
69 } catch (IOException e) {
70 throw new DefaultGboatNestedException("从 URL [" + url + "] 加载 properties 配置信息失败", e);
71 } finally {
72 IOUtils.closeQuietly(input);
73 }
74
75 }
76
77
78
79
80
81
82
83 public static Properties loadPropertiesFromResource(String resource) {
84 return loadPropertiesFromResource(resource, null);
85 }
86
87
88
89
90
91
92
93
94
95 public static Properties loadPropertiesFromResource(String resource, ClassLoader loader) {
96 if (loader == null) {
97 loader = ClassLoader.getSystemClassLoader();
98 }
99
100 InputStream in = null;
101 try {
102 in = loader.getResourceAsStream(resource);
103 return loadProperties(in);
104 } finally {
105 IOUtils.closeQuietly(in);
106 }
107 }
108
109
110
111
112
113
114
115 public static Properties loadProperties(InputStream in) {
116 Properties conf = new Properties();
117 try {
118 conf.load(in);
119 return conf;
120 } catch (IOException e) {
121 throw new DefaultGboatNestedException("从输入流加载 properties 配置信息失败", e);
122 }
123 }
124
125
126
127
128
129
130
131 public static Properties loadProperties(Reader reader) {
132 Properties conf = new Properties();
133 try {
134 conf.load(reader);
135 return conf;
136 } catch (IOException e) {
137 throw new DefaultGboatNestedException("从字符流加载 properties 配置信息失败", e);
138 }
139 }
140
141
142
143
144
145
146
147 public static PropertiesConfiguration LoadPropertiesConfiguration(String fileName) {
148 try {
149 return new PropertiesConfiguration(fileName);
150 } catch (ConfigurationException e) {
151 throw new DefaultGboatNestedException("从文件 [" + fileName + "] 加载 properties 配置信息失败", e);
152 }
153 }
154
155
156
157
158
159
160
161 public static PropertiesConfiguration LoadPropertiesConfiguration(File file) {
162 try {
163 return new PropertiesConfiguration(file);
164 } catch (ConfigurationException e) {
165 throw new DefaultGboatNestedException("从文件 [" + file + "] 加载 properties 配置信息失败", e);
166 }
167 }
168
169
170
171
172
173
174
175 public static PropertiesConfiguration LoadPropertiesConfiguration(URL url) {
176 try {
177 return new PropertiesConfiguration(url);
178 } catch (ConfigurationException e) {
179 throw new DefaultGboatNestedException("从 URL [" + url + "] 加载 properties 配置信息失败", e);
180 }
181 }
182
183
184
185
186
187
188
189 public static PropertiesConfiguration LoadPropertiesConfiguration(Reader reader) {
190 PropertiesConfiguration conf = new PropertiesConfiguration();
191 try {
192 conf.load(reader);
193 return conf;
194 } catch (ConfigurationException e) {
195 throw new DefaultGboatNestedException("从字符流加载 properties 配置信息失败", e);
196 } finally {
197 IOUtils.closeQuietly(reader);
198 }
199 }
200
201
202
203
204
205
206
207 public static PropertiesConfiguration LoadPropertiesConfiguration(InputStream in) {
208 return LoadPropertiesConfiguration(in, null);
209 }
210
211
212
213
214
215
216
217 public static PropertiesConfiguration LoadPropertiesConfiguration(InputStream in, String encoding) {
218 PropertiesConfiguration conf = new PropertiesConfiguration();
219 try {
220 conf.load(in, encoding);
221 return conf;
222 } catch (ConfigurationException e) {
223 throw new DefaultGboatNestedException("从输入流加载 properties 配置信息失败", e);
224 }
225 }
226
227
228
229
230
231
232
233 public static PropertiesConfiguration LoadPropertiesConfigurationFromResource(String resource) {
234 return LoadPropertiesConfigurationFromResource(resource, null);
235 }
236
237
238
239
240
241
242
243
244
245 public static PropertiesConfiguration LoadPropertiesConfigurationFromResource(String resource, ClassLoader loader) {
246 if (loader == null) {
247 loader = ClassLoader.getSystemClassLoader();
248 }
249
250 InputStream in = null;
251 try {
252 in = loader.getResourceAsStream(resource);
253 return LoadPropertiesConfiguration(in);
254 } finally {
255 IOUtils.closeQuietly(in);
256 }
257 }
258 }