修复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

@ -21,7 +21,7 @@ public class IniFileReader
{
private Hashtable paramTable;
private String conf_filename;
/**
* @param conf_filename config filename
*/
@ -30,7 +30,7 @@ public class IniFileReader
this.conf_filename = conf_filename;
loadFromFile(conf_filename);
}
/**
* get the config filename
* @return config filename
@ -39,7 +39,7 @@ public class IniFileReader
{
return this.conf_filename;
}
/**
* get string value from config file
* @param name item name in config file
@ -53,12 +53,12 @@ public class IniFileReader
{
return null;
}
if (obj instanceof String)
{
return (String)obj;
}
return (String)((ArrayList)obj).get(0);
}
@ -75,7 +75,7 @@ public class IniFileReader
{
return default_value;
}
return Integer.parseInt(szValue);
}
@ -92,11 +92,11 @@ public class IniFileReader
{
return default_value;
}
return szValue.equalsIgnoreCase("yes") || szValue.equalsIgnoreCase("on") ||
return szValue.equalsIgnoreCase("yes") || szValue.equalsIgnoreCase("on") ||
szValue.equalsIgnoreCase("true") || szValue.equals("1");
}
/**
* get all values from config file
* @param name item name in config file
@ -106,77 +106,101 @@ public class IniFileReader
{
Object obj;
String[] values;
obj = this.paramTable.get(name);
if (obj == null)
{
return null;
}
if (obj instanceof String)
{
values = new String[1];
values[0] = (String)obj;
return values;
}
Object[] objs = ((ArrayList)obj).toArray();
values = new String[objs.length];
System.arraycopy(objs, 0, values, 0, objs.length);
return values;
}
private void loadFromFile(String conf_filename) throws FileNotFoundException, IOException
{
//问题说明 使用中发现原来客户端打jar包后在另一个项目中引用另一个项目打jar包后运行时找不到客户端配置文件
String name;
String value;
Object obj;
ArrayList valueList;
InputStream is=null;
this.paramTable = new Hashtable();
try
{
Properties props;
try {
is = Thread.currentThread().getContextClassLoader().getResourceAsStream(conf_filename);
props = new Properties();
props.load(is);
} catch (Exception ex) {
is = new FileInputStream(conf_filename);
props = new Properties();
props.load(is);
}
Iterator<Map.Entry<Object, Object>> it = props.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Object, Object> entry = it.next();
name= entry.getKey().toString();
value = entry.getValue().toString();
obj = this.paramTable.get(name);
if (obj == null)
{
this.paramTable.put(name, value);
}
else if (obj instanceof String)
{
valueList = new ArrayList();
valueList.add(obj);
valueList.add(value);
this.paramTable.put(name, valueList);
}
else
{
valueList = (ArrayList)obj;
valueList.add(value);
}
}
}
finally
{
if (is != null) {
is.close();
}
}
private void loadFromFile(String confFilePath) throws IOException {
InputStream in = null;
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 value;
Object obj;
ArrayList valueList;
InputStreamReader inReader = null;
BufferedReader bufferedReader = null;
try {
inReader = new InputStreamReader(in);
bufferedReader = new BufferedReader(inReader);
while ((line = bufferedReader.readLine()) != null) {
line = line.trim();
if (line.length() == 0 || line.charAt(0) == '#') {
continue;
}
parts = line.split("=", 2);
if (parts.length != 2) {
continue;
}
name = parts[0].trim();
value = parts[1].trim();
obj = this.paramTable.get(name);
if (obj == null) {
this.paramTable.put(name, value);
} else if (obj instanceof String) {
valueList = new ArrayList();
valueList.add(obj);
valueList.add(value);
this.paramTable.put(name, valueList);
} else {
valueList = (ArrayList) obj;
valueList.add(value);
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
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]));
}
}
}
}