Skip to content

Commit ec1ed97

Browse files
author
veryben
committed
修复IniFileReader读取同名字段(tracker_server)只能取最后一个的bug
1 parent 2e271dd commit ec1ed97

File tree

2 files changed

+116
-63
lines changed

2 files changed

+116
-63
lines changed

src/org/csource/common/IniFileReader.java

Lines changed: 87 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class IniFileReader
2121
{
2222
private Hashtable paramTable;
2323
private String conf_filename;
24-
24+
2525
/**
2626
* @param conf_filename config filename
2727
*/
@@ -30,7 +30,7 @@ public IniFileReader(String conf_filename) throws FileNotFoundException, IOExcep
3030
this.conf_filename = conf_filename;
3131
loadFromFile(conf_filename);
3232
}
33-
33+
3434
/**
3535
* get the config filename
3636
* @return config filename
@@ -39,7 +39,7 @@ public String getConfFilename()
3939
{
4040
return this.conf_filename;
4141
}
42-
42+
4343
/**
4444
* get string value from config file
4545
* @param name item name in config file
@@ -53,12 +53,12 @@ public String getStrValue(String name)
5353
{
5454
return null;
5555
}
56-
56+
5757
if (obj instanceof String)
5858
{
5959
return (String)obj;
6060
}
61-
61+
6262
return (String)((ArrayList)obj).get(0);
6363
}
6464

@@ -75,7 +75,7 @@ public int getIntValue(String name, int default_value)
7575
{
7676
return default_value;
7777
}
78-
78+
7979
return Integer.parseInt(szValue);
8080
}
8181

@@ -92,11 +92,11 @@ public boolean getBoolValue(String name, boolean default_value)
9292
{
9393
return default_value;
9494
}
95-
96-
return szValue.equalsIgnoreCase("yes") || szValue.equalsIgnoreCase("on") ||
95+
96+
return szValue.equalsIgnoreCase("yes") || szValue.equalsIgnoreCase("on") ||
9797
szValue.equalsIgnoreCase("true") || szValue.equals("1");
9898
}
99-
99+
100100
/**
101101
* get all values from config file
102102
* @param name item name in config file
@@ -106,77 +106,101 @@ public String[] getValues(String name)
106106
{
107107
Object obj;
108108
String[] values;
109-
109+
110110
obj = this.paramTable.get(name);
111111
if (obj == null)
112112
{
113113
return null;
114114
}
115-
115+
116116
if (obj instanceof String)
117117
{
118118
values = new String[1];
119119
values[0] = (String)obj;
120120
return values;
121121
}
122-
122+
123123
Object[] objs = ((ArrayList)obj).toArray();
124124
values = new String[objs.length];
125125
System.arraycopy(objs, 0, values, 0, objs.length);
126126
return values;
127127
}
128-
129-
private void loadFromFile(String conf_filename) throws FileNotFoundException, IOException
130-
{
131-
//问题说明 使用中发现原来客户端打jar包后,在另一个项目中引用,另一个项目打jar包后运行时找不到客户端配置文件
132-
String name;
133-
String value;
134-
Object obj;
135-
ArrayList valueList;
136-
InputStream is=null;
137-
this.paramTable = new Hashtable();
138-
139-
try
140-
{
141-
Properties props;
142-
try {
143-
is = Thread.currentThread().getContextClassLoader().getResourceAsStream(conf_filename);
144-
props = new Properties();
145-
props.load(is);
146-
} catch (Exception ex) {
147-
is = new FileInputStream(conf_filename);
148-
props = new Properties();
149-
props.load(is);
150-
}
151-
Iterator<Map.Entry<Object, Object>> it = props.entrySet().iterator();
152-
while (it.hasNext()) {
153-
Map.Entry<Object, Object> entry = it.next();
154-
name= entry.getKey().toString();
155-
value = entry.getValue().toString();
156-
obj = this.paramTable.get(name);
157-
if (obj == null)
158-
{
159-
this.paramTable.put(name, value);
160-
}
161-
else if (obj instanceof String)
162-
{
163-
valueList = new ArrayList();
164-
valueList.add(obj);
165-
valueList.add(value);
166-
this.paramTable.put(name, valueList);
167-
}
168-
else
169-
{
170-
valueList = (ArrayList)obj;
171-
valueList.add(value);
172-
}
173-
}
128+
129+
private void loadFromFile(String confFilePath) throws IOException {
130+
InputStream in = null;
131+
try {
132+
// 从类路径加载
133+
in = classLoader().getResourceAsStream(confFilePath);
134+
//System.out.println("loadFrom...class path done");
135+
readToParamTable(in);
136+
} catch (Exception ex) {
137+
ex.printStackTrace();
138+
} finally {
139+
try {
140+
if(in != null) in.close();
141+
//System.out.println("loadFrom...finally...in.close(); done");
142+
} catch (Exception ex) {
143+
ex.printStackTrace();
144+
}
145+
}
146+
}
147+
148+
private void readToParamTable(InputStream in) throws IOException {
149+
this.paramTable = new Hashtable();
150+
String line;
151+
String[] parts;
152+
String name;
153+
String value;
154+
Object obj;
155+
ArrayList valueList;
156+
InputStreamReader inReader = null;
157+
BufferedReader bufferedReader = null;
158+
try {
159+
inReader = new InputStreamReader(in);
160+
bufferedReader = new BufferedReader(inReader);
161+
while ((line = bufferedReader.readLine()) != null) {
162+
line = line.trim();
163+
if (line.length() == 0 || line.charAt(0) == '#') {
164+
continue;
174165
}
175-
finally
176-
{
177-
if (is != null) {
178-
is.close();
179-
}
166+
parts = line.split("=", 2);
167+
if (parts.length != 2) {
168+
continue;
180169
}
170+
name = parts[0].trim();
171+
value = parts[1].trim();
172+
obj = this.paramTable.get(name);
173+
if (obj == null) {
174+
this.paramTable.put(name, value);
175+
} else if (obj instanceof String) {
176+
valueList = new ArrayList();
177+
valueList.add(obj);
178+
valueList.add(value);
179+
this.paramTable.put(name, valueList);
180+
} else {
181+
valueList = (ArrayList) obj;
182+
valueList.add(value);
183+
}
184+
}
185+
} catch (Exception ex) {
186+
ex.printStackTrace();
187+
} finally {
188+
try {
189+
if(bufferedReader != null) bufferedReader.close();
190+
if(inReader != null) inReader.close();
191+
//System.out.println("readToParamTable...finally...bufferedReader.close();inReader.close(); done");
192+
} catch (Exception ex) {
193+
ex.printStackTrace();
194+
}
195+
}
196+
}
197+
198+
private static ClassLoader classLoader() {
199+
ClassLoader loader = Thread.currentThread().getContextClassLoader();
200+
if (loader == null) {
201+
loader = ClassLoader.getSystemClassLoader();
181202
}
203+
return loader;
204+
}
205+
182206
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.csource.fastdfs.test;
2+
3+
import org.csource.common.IniFileReader;
4+
5+
/**
6+
* Created by James on 2017/5/16.
7+
*/
8+
public class IniFileReaderTests {
9+
10+
public static void main(String[] args) throws Exception {
11+
String conf_filename = "fdfs_client.conf";
12+
IniFileReader iniFileReader = new IniFileReader(conf_filename);
13+
System.out.println("getConfFilename: " + iniFileReader.getConfFilename());
14+
System.out.println("connect_timeout: " + iniFileReader.getIntValue("connect_timeout", 3));
15+
System.out.println("network_timeout: " + iniFileReader.getIntValue("network_timeout", 45));
16+
System.out.println("charset: " + iniFileReader.getStrValue("charset"));
17+
System.out.println("http.tracker_http_port: " + iniFileReader.getIntValue("http.tracker_http_port", 8080));
18+
System.out.println("http.anti_steal_token: " + iniFileReader.getBoolValue("http.anti_steal_token", false));
19+
System.out.println("http.secret_key: " + iniFileReader.getStrValue("http.secret_key"));
20+
String[] tracker_servers = iniFileReader.getValues("tracker_server");
21+
if(tracker_servers != null) {
22+
System.out.println("tracker_servers.length: " + tracker_servers.length);
23+
for (int i = 0; i < tracker_servers.length; i++) {
24+
System.out.println(String.format("tracker_servers[%s]: %s", i, tracker_servers[i]));
25+
}
26+
}
27+
}
28+
29+
}

0 commit comments

Comments
 (0)