過修改源碼讓FastDFS能正確的讀取到classpath:配置文件

說句難聽的話,FastDFS的java客戶端,站在java程序員的角度來講是相當的難用啊。 
試着將FastDFS的javaClient整合進電商平臺之中 
FastDFS客戶端初始化方式有兩種一種是ClientGlobal.init(String)傳入配置文件的路徑,另外一種是通過ClientGlobal.set的方式,不過實在是不爽這種set方式啊,附上源碼就知道了。
 
Java代碼  收藏代碼
  1. public static int g_connect_timeout; //millisecond  
  2. public static int g_network_timeout; //millisecond  
  3. public static String g_charset;  
  4. public static int g_tracker_http_port;  
  5. public static boolean g_anti_steal_token;  //if anti-steal token  
  6. public static String g_secret_key;   //generage token secret key  
  7. public static TrackerGroup g_tracker_group;  
  8.        //set  
  9.         public static int getG_connect_timeout()  
  10. {  
  11.     return g_connect_timeout;  
  12. }  
  13.   
  14. public static void setG_connect_timeout(int connect_timeout)  
  15. {  
  16.     ClientGlobal.g_connect_timeout = connect_timeout;  
  17. }  
  18.   
  19. public static int getG_network_timeout()  
  20. {  
  21.     return g_network_timeout;  
  22. }  
  23.   
  24. public static void setG_network_timeout(int network_timeout)  
  25. {  
  26.     ClientGlobal.g_network_timeout = network_timeout;  
  27. }  
  28.   
  29. public static String getG_charset()  
  30. {  
  31.     return g_charset;  
  32. }  

完全就是C程序的風格啊。作爲一枚稱職的java程序猿,實在是受不了這種方式。那麼就用配置文件的方式了吧,來吧,試一試 
Java代碼  收藏代碼
  1. //先來試試相對路徑  
  2. ClientGlobal.init("/fastdfs.conf");  
  3. //運行,配置文件未找到   
  4. //那麼在來試試classpath  
  5. ClientGlobal.init("classpath:fastdfs.conf");  
  6. //運行配置文件未找到。  
  7. //看來要使用絕招了,直接給系統的絕對路徑  
  8. ClientGlobal.init("D:/seafood_project/seafood-core/src/main/resources/fastdfs.conf");  
  9. //這下子總算是可以了。  

Java代碼  收藏代碼
  1. 但是又來問題了,項目部署的時候不可能這樣子配置啊,好的選擇肯定還是的從classpath下面找纔是。沒辦法又看源碼吧,直接上讀取配置文件的源碼,你就明白了爲啥讀取不到了  

Java代碼  收藏代碼
  1. public class IniFileReader  
  2. {  
  3.     //居然在用hashTable。。。。。  
  4.         private Hashtable paramTable;  
  5.     private String conf_filename;  
  6.       
  7. /** 
  8. * @param conf_filename config filename 
  9. */  
  10.     public IniFileReader(String conf_filename) throws FileNotFoundException, IOException  
  11.     {  
  12.         this.conf_filename = conf_filename;  
  13.                 //看來這裏是我們需要的  
  14.         loadFromFile(conf_filename);  
  15.     }  
  16.         //看到loadFromFile的源碼的時候,根本忍不住要改源碼啊  
  17.         private void loadFromFile(String conf_filename) throws FileNotFoundException, IOException  
  18.     {  
  19.         FileReader fReader;  
  20.         BufferedReader buffReader;  
  21.         String line;  
  22.         String[] parts;  
  23.         String name;  
  24.         String value;  
  25.         Object obj;  
  26.         ArrayList valueList;  
  27.           
  28.       fReader = new FileReader(conf_filename);  
  29.       buffReader = new BufferedReader(fReader);  
  30.       this.paramTable = new Hashtable();  
  31.         
  32.       try  
  33.       {  
  34.         while ((line=buffReader.readLine()) != null)  
  35.         {  
  36.             line = line.trim();  
  37.             if (line.length() == 0 || line.charAt(0) == '#')  
  38.             {  
  39.                 continue;  
  40.             }  
  41.               
  42.             parts = line.split("="2);  
  43.             if (parts.length != 2)  
  44.             {  
  45.                 continue;  
  46.             }  
  47.           
  48.             name = parts[0].trim();  
  49.             value = parts[1].trim();  
  50.               
  51.             obj = this.paramTable.get(name);  
  52.             if (obj == null)  
  53.             {  
  54.                 this.paramTable.put(name, value);  
  55.             }  
  56.             else if (obj instanceof String)  
  57.             {  
  58.                 valueList = new ArrayList();  
  59.                 valueList.add(obj);  
  60.                 valueList.add(value);  
  61.                 this.paramTable.put(name, valueList);  
  62.             }  
  63.             else  
  64.             {  
  65.                 valueList = (ArrayList)obj;  
  66.                 valueList.add(value);  
  67.             }  
  68.         }  
  69.       }  
  70.       finally  
  71.       {  
  72.         fReader.close();  
  73.       }  
  74.   }   

看到這兒應該明白爲啥讀不了了吧!淚奔啊!附上改寫的源代碼 
Java代碼  收藏代碼
  1. public class IniFileReader {  
  2.         private PropertiesLoader loader;  
  3.         private String conf_filename;  
  4.   
  5.         /** 
  6.          *  
  7.         * <p>Title: </p> 
  8.         * <p>Description: </p> 
  9.         * @param conf_filename 
  10.         * @throws FileNotFoundException 
  11.         * @throws IOException 
  12.          */  
  13.         public IniFileReader(String conf_filename) throws FileNotFoundException, IOException {  
  14.                 this.conf_filename = conf_filename;  
  15.                 loadFromFile(conf_filename);  
  16.         }  
  17.   
  18.         /** 
  19.          *  
  20.         * @Description: TODO(這裏用一句話描述這個方法的作用) 
  21.         * @author LiuYi 
  22.         * @date 2014年6月5日 上午10:11:14 
  23.         *  @return  String 
  24.          */  
  25.         public String getConfFilename() {  
  26.                 return this.conf_filename;  
  27.         }  
  28.   
  29.         /** 
  30.          *  
  31.         * @Description: TODO(這裏用一句話描述這個方法的作用) 
  32.         * @author LiuYi 
  33.         * @date 2014年6月6日 上午10:11:11 
  34.         *  @param name 
  35.         *  @return  String 
  36.          */  
  37.         public String getStrValue(String name) {  
  38.                 return this.loader.getProperty(name);  
  39.         }  
  40.   
  41.         /** 
  42.          *  
  43.         * @Description: TODO(這裏用一句話描述這個方法的作用) 
  44.         * @author LiuYi 
  45.         * @date 2014年6月5日 上午10:11:01 
  46.         *  @param name 
  47.         *  @param default_value 
  48.         *  @return  int 
  49.          */  
  50.         public int getIntValue(String name, int default_value) {  
  51.                 String szValue = this.loader.getProperty(name);  
  52.                 if (szValue == null || "".equals(szValue)) {  
  53.                         return default_value;  
  54.                 }  
  55.                 return Integer.parseInt(szValue);  
  56.         }  
  57.   
  58.         /** 
  59.          *  
  60.         * @Description: TODO(這裏用一句話描述這個方法的作用) 
  61.         * @author LiuYi 
  62.         * @date 2014年6月5日 上午10:10:53 
  63.         *  @param name 
  64.         *  @param default_value 
  65.         *  @return  boolean 
  66.          */  
  67.         public boolean getBoolValue(String name, boolean default_value) {  
  68.                 String szValue = this.loader.getProperty(name);  
  69.                 if (szValue == null) {  
  70.                         return default_value;  
  71.                 }  
  72.                 return szValue.equalsIgnoreCase("yes") || szValue.equalsIgnoreCase("on")  
  73.                                 || szValue.equalsIgnoreCase("true") || szValue.equals("1");  
  74.         }  
  75.   
  76.         /** 
  77.          *  
  78.         * @Description: TODO() 
  79.         * @author LiuYi 
  80.         * @date 2014年6月5日 上午10:10:35 
  81.         *  @param name 
  82.         *  @return  String[] 
  83.          */  
  84.         public String[] getValues(String name) {  
  85.                 List<String> values = new ArrayList<String>();  
  86.                 String val = this.loader.getProperty(name);  
  87.                 if (val.contains(",")) {  
  88.                         for (String v : val.split(",")) {  
  89.                                 values.add(v);  
  90.                         }  
  91.                 } else {  
  92.                         values.add(val);  
  93.                 }  
  94.                 return values.toArray(new String[values.size()]);  
  95.         }  
  96.         /** 
  97.          *  
  98.         * @Description: TODO(這裏用一句話描述這個方法的作用) 
  99.         * @author LiuYi 
  100.         * @date 2014年6月5日 上午10:11:54 
  101.         *  @param resourcesPaths 
  102.         *  @throws FileNotFoundException 
  103.         *  @throws IOException  void 
  104.          */  
  105.         private void loadFromFile(String... resourcesPaths) throws FileNotFoundException, IOException {  
  106.                 this.loader = new PropertiesLoader(resourcesPaths);  
  107.         }  
  108. }  

PropertiesLoader依賴了Spring的io包附上源碼 
Java代碼  收藏代碼
  1. /** 
  2.  * Properties文件載入工具類. 可載入多個properties文件, 相同的屬性在最後載入的文件中的值將會覆蓋之前的值,但以System的Property優先. 
  3.  * @author LiuYi 
  4.  * @version 2014-05-21 
  5.  */  
  6. public class PropertiesLoader {  
  7.     private static ResourceLoader resourceLoader = new DefaultResourceLoader();  
  8.     private final Properties properties;  
  9.     public PropertiesLoader(String... resourcesPaths) {  
  10.         properties = loadProperties(resourcesPaths);  
  11.     }  
  12.     public Properties getProperties() {  
  13.         return properties;  
  14.     }  
  15.     /** 
  16.      * 取出Property,但以System的Property優先,取不到返回空字符串. 
  17.      */  
  18.     private String getValue(String key) {  
  19.         String systemProperty = System.getProperty(key);  
  20.         if (systemProperty != null) {  
  21.             return systemProperty;  
  22.         }  
  23.         if (properties.containsKey(key)) {  
  24.             return properties.getProperty(key);  
  25.         }  
  26.         return "";  
  27.     }  
  28.     /** 
  29.      * 取出String類型的Property,但以System的Property優先,如果都爲Null則拋出異常. 
  30.      */  
  31.     public String getProperty(String key) {  
  32.         String value = getValue(key);  
  33.         if (value == null) {  
  34.             throw new NoSuchElementException();  
  35.         }  
  36.         return value;  
  37.     }  
  38.   
  39.     /** 
  40.      * 取出String類型的Property,但以System的Property優先.如果都爲Null則返回Default值. 
  41.      */  
  42.     public String getProperty(String key, String defaultValue) {  
  43.         String value = getValue(key);  
  44.         return value != null ? value : defaultValue;  
  45.     }  
  46.   
  47.     /** 
  48.      * 取出Integer類型的Property,但以System的Property優先.如果都爲Null或內容錯誤則拋出異常. 
  49.      */  
  50.     public Integer getInteger(String key) {  
  51.         String value = getValue(key);  
  52.         if (value == null) {  
  53.             return null;  
  54.         }  
  55.         return Integer.valueOf(value);  
  56.     }  
  57.   
  58.     /** 
  59.      * 取出Integer類型的Property,但以System的Property優先.如果都爲Null則返回Default值,如果內容錯誤則拋出異常 
  60.      */  
  61.     public Integer getInteger(String key, Integer defaultValue) {  
  62.         String value = getValue(key);  
  63.         return value != null ? Integer.valueOf(value) : defaultValue;  
  64.     }  
  65.   
  66.     /** 
  67.      * 取出Double類型的Property,但以System的Property優先.如果都爲Null或內容錯誤則拋出異常. 
  68.      */  
  69.     public Double getDouble(String key) {  
  70.         String value = getValue(key);  
  71.         if (value == null) {  
  72.             throw new NoSuchElementException();  
  73.         }  
  74.         return Double.valueOf(value);  
  75.     }  
  76.   
  77.     /** 
  78.      * 取出Double類型的Property,但以System的Property優先.如果都爲Null則返回Default值,如果內容錯誤則拋出異常 
  79.      */  
  80.     public Double getDouble(String key, Integer defaultValue) {  
  81.         String value = getValue(key);  
  82.         return value != null ? Double.valueOf(value) : defaultValue;  
  83.     }  
  84.   
  85.     /** 
  86.      * 取出Boolean類型的Property,但以System的Property優先.如果都爲Null拋出異常,如果內容不是true/false則返回false. 
  87.      */  
  88.     public Boolean getBoolean(String key) {  
  89.         String value = getValue(key);  
  90.         if (value == null) {  
  91.             throw new NoSuchElementException();  
  92.         }  
  93.         return Boolean.valueOf(value);  
  94.     }  
  95.   
  96.     /** 
  97.      * 取出Boolean類型的Property,但以System的Property優先.如果都爲Null則返回Default值,如果內容不爲true/false則返回false. 
  98.      */  
  99.     public Boolean getBoolean(String key, boolean defaultValue) {  
  100.         String value = getValue(key);  
  101.         return value != null ? Boolean.valueOf(value) : defaultValue;  
  102.     }  
  103.   
  104.     /** 
  105.      * 載入多個文件, 文件路徑使用Spring Resource格式. 
  106.      */  
  107.     private Properties loadProperties(String... resourcesPaths) {  
  108.         Properties props = new Properties();  
  109.         for (String location : resourcesPaths) {  
  110. //          logger.debug("Loading properties file from:" + location);  
  111.             InputStream is = null;  
  112.             try {  
  113.                 Resource resource = resourceLoader.getResource(location);  
  114.                 is = resource.getInputStream();  
  115.                 props.load(is);  
  116.             } catch (IOException ex) {  
  117.             } finally {  
  118.                     try {  
  119.                                         if(is!=null)  
  120.                                         is.close();  
  121.                                 } catch (IOException e) {  
  122.                                         e.printStackTrace();  
  123.                                 }  
  124.             }  
  125.         }  
  126.         return props;  
  127.     }  
  128. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章