修复IniFileReader读取同名字段(tracker_server)只能取最后一个的bug

dev
veryben 2017-05-18 17:36:43 +08:00
parent 2e271dd6fc
commit ec1ed97ae5
2 changed files with 117 additions and 64 deletions

View File

@ -126,57 +126,81 @@ public class IniFileReader
return values; return values;
} }
private void loadFromFile(String conf_filename) throws FileNotFoundException, IOException private void loadFromFile(String confFilePath) throws IOException {
{ InputStream in = null;
//问题说明 使用中发现原来客户端打jar包后在另一个项目中引用另一个项目打jar包后运行时找不到客户端配置文件 try {
// 从类路径加载
in = classLoader().getResourceAsStream(confFilePath);
//System.out.println("loadFrom...class path done");
readToParamTable(in);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if(in != null) in.close();
//System.out.println("loadFrom...finally...in.close(); done");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
private void readToParamTable(InputStream in) throws IOException {
this.paramTable = new Hashtable();
String line;
String[] parts;
String name; String name;
String value; String value;
Object obj; Object obj;
ArrayList valueList; ArrayList valueList;
InputStream is=null; InputStreamReader inReader = null;
this.paramTable = new Hashtable(); BufferedReader bufferedReader = null;
try
{
Properties props;
try { try {
is = Thread.currentThread().getContextClassLoader().getResourceAsStream(conf_filename); inReader = new InputStreamReader(in);
props = new Properties(); bufferedReader = new BufferedReader(inReader);
props.load(is); while ((line = bufferedReader.readLine()) != null) {
} catch (Exception ex) { line = line.trim();
is = new FileInputStream(conf_filename); if (line.length() == 0 || line.charAt(0) == '#') {
props = new Properties(); continue;
props.load(is);
} }
Iterator<Map.Entry<Object, Object>> it = props.entrySet().iterator(); parts = line.split("=", 2);
while (it.hasNext()) { if (parts.length != 2) {
Map.Entry<Object, Object> entry = it.next(); continue;
name= entry.getKey().toString(); }
value = entry.getValue().toString(); name = parts[0].trim();
value = parts[1].trim();
obj = this.paramTable.get(name); obj = this.paramTable.get(name);
if (obj == null) if (obj == null) {
{
this.paramTable.put(name, value); this.paramTable.put(name, value);
} } else if (obj instanceof String) {
else if (obj instanceof String)
{
valueList = new ArrayList(); valueList = new ArrayList();
valueList.add(obj); valueList.add(obj);
valueList.add(value); valueList.add(value);
this.paramTable.put(name, valueList); this.paramTable.put(name, valueList);
} } else {
else
{
valueList = (ArrayList) obj; valueList = (ArrayList) obj;
valueList.add(value); valueList.add(value);
} }
} }
} } catch (Exception ex) {
finally ex.printStackTrace();
{ } finally {
if (is != null) { try {
is.close(); if(bufferedReader != null) bufferedReader.close();
if(inReader != null) inReader.close();
//System.out.println("readToParamTable...finally...bufferedReader.close();inReader.close(); done");
} catch (Exception ex) {
ex.printStackTrace();
} }
} }
} }
private static ClassLoader classLoader() {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (loader == null) {
loader = ClassLoader.getSystemClassLoader();
}
return loader;
}
} }

View File

@ -0,0 +1,29 @@
package org.csource.fastdfs.test;
import org.csource.common.IniFileReader;
/**
* Created by James on 2017/5/16.
*/
public class IniFileReaderTests {
public static void main(String[] args) throws Exception {
String conf_filename = "fdfs_client.conf";
IniFileReader iniFileReader = new IniFileReader(conf_filename);
System.out.println("getConfFilename: " + iniFileReader.getConfFilename());
System.out.println("connect_timeout: " + iniFileReader.getIntValue("connect_timeout", 3));
System.out.println("network_timeout: " + iniFileReader.getIntValue("network_timeout", 45));
System.out.println("charset: " + iniFileReader.getStrValue("charset"));
System.out.println("http.tracker_http_port: " + iniFileReader.getIntValue("http.tracker_http_port", 8080));
System.out.println("http.anti_steal_token: " + iniFileReader.getBoolValue("http.anti_steal_token", false));
System.out.println("http.secret_key: " + iniFileReader.getStrValue("http.secret_key"));
String[] tracker_servers = iniFileReader.getValues("tracker_server");
if(tracker_servers != null) {
System.out.println("tracker_servers.length: " + tracker_servers.length);
for (int i = 0; i < tracker_servers.length; i++) {
System.out.println(String.format("tracker_servers[%s]: %s", i, tracker_servers[i]));
}
}
}
}