ソースコードを修正することでFastDFSがclasspathに正しく読み込まれるようにする:プロファイル

12235 ワード

聞き苦しいことを言うと、FastDFSのjavaクライアントは、javaプログラマーの立場から言えばかなり使いにくいでしょう.FastDFSのjavaClientを電子商取引プラットフォームに統合してみるFastDFSクライアントの初期化方式には、ClientGlobalの2つがある.Init(String)がプロファイルに転送するパスと、ClientGlobalを通過するパスとがある.setの方式ですが、本当に不快です.このset方式ですね.ソースコードを添付すればわかります.

	public static int g_connect_timeout; //millisecond
	public static int g_network_timeout; //millisecond
	public static String g_charset;
	public static int g_tracker_http_port;
	public static boolean g_anti_steal_token;  //if anti-steal token
	public static String g_secret_key;   //generage token secret key
	public static TrackerGroup g_tracker_group;
        //set
        	public static int getG_connect_timeout()
	{
		return g_connect_timeout;
	}
	
	public static void setG_connect_timeout(int connect_timeout)
	{
		ClientGlobal.g_connect_timeout = connect_timeout;
	}
	
	public static int getG_network_timeout()
	{
		return g_network_timeout;
	}
	
	public static void setG_network_timeout(int network_timeout)
	{
		ClientGlobal.g_network_timeout = network_timeout;
	}
	
	public static String getG_charset()
	{
		return g_charset;
	}
	

完全にCプログラムのスタイルですね.称職のjavaプログラム猿としては、このようなやり方には耐えられない.では、プロファイルを使ってみましょう.さあ、やってみましょう.

//        
ClientGlobal.init("/fastdfs.conf");
//  ,        
//      classpath
ClientGlobal.init("classpath:fastdfs.conf");
//         。
//        ,          
ClientGlobal.init("D:/seafood_project/seafood-core/src/main/resources/fastdfs.conf");
//         。

       ,                ,          classpath     。        ,            ,            

public class IniFileReader
{
	//    hashTable。。。。。
        private Hashtable paramTable;
	private String conf_filename;
	
/**
* @param conf_filename config filename
*/
	public IniFileReader(String conf_filename) throws FileNotFoundException, IOException
	{
		this.conf_filename = conf_filename;
                //          
		loadFromFile(conf_filename);
	}
        //  loadFromFile      ,          
        private void loadFromFile(String conf_filename) throws FileNotFoundException, IOException
	{
		FileReader fReader;
		BufferedReader buffReader;
		String line;
		String[] parts;
		String name;
		String value;
		Object obj;
		ArrayList valueList;
		
	  fReader = new FileReader(conf_filename);
	  buffReader = new BufferedReader(fReader);
	  this.paramTable = new Hashtable();
	  
	  try
	  {
	  	while ((line=buffReader.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);
	  		}
	  	}
	  }
	  finally
	  {
	  	fReader.close();
	  }
  } 


ここを見ると、なぜ読めないのかわかるでしょう.涙で行こう!書き換えたソースコードを添付

public class IniFileReader {
        private PropertiesLoader loader;
        private String conf_filename;

        /**
         * 
        * <p>Title: </p>
        * <p>Description: </p>
        * @param conf_filename
        * @throws FileNotFoundException
        * @throws IOException
         */
        public IniFileReader(String conf_filename) throws FileNotFoundException, IOException {
                this.conf_filename = conf_filename;
                loadFromFile(conf_filename);
        }

        /**
         * 
        * @Description: TODO(               )
        * @author LiuYi
        * @date 2014 6 5    10:11:14
        *  @return  String
         */
        public String getConfFilename() {
                return this.conf_filename;
        }

        /**
         * 
        * @Description: TODO(               )
        * @author LiuYi
        * @date 2014 6 6    10:11:11
        *  @param name
        *  @return  String
         */
        public String getStrValue(String name) {
                return this.loader.getProperty(name);
        }

        /**
         * 
        * @Description: TODO(               )
        * @author LiuYi
        * @date 2014 6 5    10:11:01
        *  @param name
        *  @param default_value
        *  @return  int
         */
        public int getIntValue(String name, int default_value) {
                String szValue = this.loader.getProperty(name);
                if (szValue == null || "".equals(szValue)) {
                        return default_value;
                }
                return Integer.parseInt(szValue);
        }

        /**
         * 
        * @Description: TODO(               )
        * @author LiuYi
        * @date 2014 6 5    10:10:53
        *  @param name
        *  @param default_value
        *  @return  boolean
         */
        public boolean getBoolValue(String name, boolean default_value) {
                String szValue = this.loader.getProperty(name);
                if (szValue == null) {
                        return default_value;
                }
                return szValue.equalsIgnoreCase("yes") || szValue.equalsIgnoreCase("on")
                                || szValue.equalsIgnoreCase("true") || szValue.equals("1");
        }

        /**
         * 
        * @Description: TODO()
        * @author LiuYi
        * @date 2014 6 5    10:10:35
        *  @param name
        *  @return  String[]
         */
        public String[] getValues(String name) {
                List<String> values = new ArrayList<String>();
                String val = this.loader.getProperty(name);
                if (val.contains(",")) {
                        for (String v : val.split(",")) {
                                values.add(v);
                        }
                } else {
                        values.add(val);
                }
                return values.toArray(new String[values.size()]);
        }
        /**
         * 
        * @Description: TODO(               )
        * @author LiuYi
        * @date 2014 6 5    10:11:54
        *  @param resourcesPaths
        *  @throws FileNotFoundException
        *  @throws IOException  void
         */
        private void loadFromFile(String... resourcesPaths) throws FileNotFoundException, IOException {
                this.loader = new PropertiesLoader(resourcesPaths);
        }
}

PropertiesLoaderはSpringのioパッケージにソースコードを添付しました

/**
 * Properties       .      properties  ,                         ,  System Property  .
 * @author LiuYi
 * @version 2014-05-21
 */
public class PropertiesLoader {
	private static ResourceLoader resourceLoader = new DefaultResourceLoader();
	private final Properties properties;
	public PropertiesLoader(String... resourcesPaths) {
		properties = loadProperties(resourcesPaths);
	}
	public Properties getProperties() {
		return properties;
	}
	/**
	 *   Property,  System Property  ,         .
	 */
	private String getValue(String key) {
		String systemProperty = System.getProperty(key);
		if (systemProperty != null) {
			return systemProperty;
		}
		if (properties.containsKey(key)) {
	        return properties.getProperty(key);
	    }
	    return "";
	}
	/**
	 *   String   Property,  System Property  ,    Null     .
	 */
	public String getProperty(String key) {
		String value = getValue(key);
		if (value == null) {
			throw new NoSuchElementException();
		}
		return value;
	}

	/**
	 *   String   Property,  System Property  .    Null   Default .
	 */
	public String getProperty(String key, String defaultValue) {
		String value = getValue(key);
		return value != null ? value : defaultValue;
	}

	/**
	 *   Integer   Property,  System Property  .    Null          .
	 */
	public Integer getInteger(String key) {
		String value = getValue(key);
		if (value == null) {
			return null;
		}
		return Integer.valueOf(value);
	}

	/**
	 *   Integer   Property,  System Property  .    Null   Default ,           
	 */
	public Integer getInteger(String key, Integer defaultValue) {
		String value = getValue(key);
		return value != null ? Integer.valueOf(value) : defaultValue;
	}

	/**
	 *   Double   Property,  System Property  .    Null          .
	 */
	public Double getDouble(String key) {
		String value = getValue(key);
		if (value == null) {
			throw new NoSuchElementException();
		}
		return Double.valueOf(value);
	}

	/**
	 *   Double   Property,  System Property  .    Null   Default ,           
	 */
	public Double getDouble(String key, Integer defaultValue) {
		String value = getValue(key);
		return value != null ? Double.valueOf(value) : defaultValue;
	}

	/**
	 *   Boolean   Property,  System Property  .    Null    ,      true/false   false.
	 */
	public Boolean getBoolean(String key) {
		String value = getValue(key);
		if (value == null) {
			throw new NoSuchElementException();
		}
		return Boolean.valueOf(value);
	}

	/**
	 *   Boolean   Property,  System Property  .    Null   Default ,      true/false   false.
	 */
	public Boolean getBoolean(String key, boolean defaultValue) {
		String value = getValue(key);
		return value != null ? Boolean.valueOf(value) : defaultValue;
	}

	/**
	 *       ,       Spring Resource  .
	 */
	private Properties loadProperties(String... resourcesPaths) {
		Properties props = new Properties();
		for (String location : resourcesPaths) {
//			logger.debug("Loading properties file from:" + location);
			InputStream is = null;
			try {
				Resource resource = resourceLoader.getResource(location);
				is = resource.getInputStream();
				props.load(is);
			} catch (IOException ex) {
			} finally {
			        try {
                                        if(is!=null)
                                        is.close();
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
			}
		}
		return props;
	}
}