From ec1ed97ae547acc1d2a6cb2b8f59c8fec254cce5 Mon Sep 17 00:00:00 2001 From: veryben Date: Thu, 18 May 2017 17:36:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DIniFileReader=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E5=90=8C=E5=90=8D=E5=AD=97=E6=AE=B5(tracker=5Fserver)?= =?UTF-8?q?=E5=8F=AA=E8=83=BD=E5=8F=96=E6=9C=80=E5=90=8E=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/org/csource/common/IniFileReader.java | 152 ++++++++++-------- .../fastdfs/test/IniFileReaderTests.java | 29 ++++ 2 files changed, 117 insertions(+), 64 deletions(-) create mode 100644 src/org/csource/fastdfs/test/IniFileReaderTests.java diff --git a/src/org/csource/common/IniFileReader.java b/src/org/csource/common/IniFileReader.java index 727e100..6be87e4 100644 --- a/src/org/csource/common/IniFileReader.java +++ b/src/org/csource/common/IniFileReader.java @@ -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> it = props.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry 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; + } + } diff --git a/src/org/csource/fastdfs/test/IniFileReaderTests.java b/src/org/csource/fastdfs/test/IniFileReaderTests.java new file mode 100644 index 0000000..4a95c71 --- /dev/null +++ b/src/org/csource/fastdfs/test/IniFileReaderTests.java @@ -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])); + } + } + } + +}