package com.iamberry.app.international.util;

/*import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Properties;

import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.DefaultPropertiesPersister;
import org.springframework.util.PropertiesPersister;

import com.iamberry.app.international.controller.GlobalController;

*//**
 * 国际化工具类
 * @author Administrator
 *
 *//*
public class languageUtil {
	    
	    public String getKey(String keyName, String language) {
	    	init();
	    	return properties.get(keyName + "-" + language).toString();
	    }
	    
	    *//**
		 * 载入多个properties文件, 相同的属性在最后载入的文件中的值将会覆盖之前的载入. 文件路径使用Spring Resource格式, 文件编码使用UTF-8.
		 * @see org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
		 *//*
		public Properties loadProperties(String... resourcesPaths) throws IOException {
			if (properties != null) {
				return properties;
			}
			if (resourcesPaths == null) {
				throw new NullPointerException("No resource path");
			}
			
			Properties props = new Properties();
			for (String location : resourcesPaths) {
				InputStream is = null;
				try {
					Resource resource = resourceLoader.getResource(location);
					is = resource.getInputStream();
					propertiesPersister.load(props, new InputStreamReader(is, DEFAULT_ENCODING));
				} catch (IOException ex) {
				} finally {
					if (is != null) {
						is.close();
					}
				}
			}
			return props;
		}
		// 编码
		private final String DEFAULT_ENCODING = "UTF-8";
		// Spring-core 获取配置文件的类
		private PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();	
		// Spring-core 获取资源的类
		private ResourceLoader resourceLoader = new PathMatchingResourcePatternResolver();
		private Properties properties = null;
		
		public void init() {
			if (properties == null) {
				try {
					synchronized (this) {
						properties = loadProperties("messages_EN.properties,messages_HK.properties,messages_ZH.properties");
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
			}
		}
		
		
		 * 遍历获取国际化文件
		 
		private String type; // 后缀名
	    public languageUtil(String type)
	    {
	        this.type = type;
	    }
	    public boolean accept(File dir, String name)
	    {
	        return name.endsWith(type);
	    }
	    static void filterfile(String dir, String suffix, ArrayList<String> fileList)
	    {
	        File f = new File(dir);
	        File [] myList = f.listFiles();
	        GlobalController filter = new GlobalController(suffix);
	        String [] files = f.list(filter);
	        fileList.addAll(Arrays.asList(files));
	        for (File a : myList)
	        {
	            if (a.isDirectory())
	            {
	                    filterfile(a.toString(), suffix, fileList);
	            } 
	        }
	    }
	    
	    public static void main(String[] args)
	    {
	        ArrayList<String> fileList = new ArrayList<String>();
	        GlobalController.filterfile("src/main/resources", "properties", fileList);
	        StringBuffer sb = null;
	        for(String s:fileList)
	        {
	            System.out.println(s);
	            sb.append(s);
	        }
	        System.out.println(sb);
	    }
}
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.DefaultPropertiesPersister;
import org.springframework.util.PropertiesPersister;


public class languageUtil {
	
	/**
	 * 载入多个properties文件, 相同的属性在最后载入的文件中的值将会覆盖之前的载入. 文件路径使用Spring Resource格式, 文件编码使用UTF-8.
	 * @see org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
	 */
	private synchronized static void loadProperties(String... resourcesPaths) throws IOException {
		System.out.println("......");
		if (resourceLoader == null) {
			resourceLoader = new PathMatchingResourcePatternResolver();
		}
		if (propertiesPersister == null) {
			propertiesPersister = new DefaultPropertiesPersister();
		}
		if (resourcesPaths == null) {
			throw new NullPointerException("No resource path");
		}
		Properties props = null;
		if (properties == null) {
			props = new Properties();
			properties = props;
		} else {
			props = properties;
		}
		for (String location : resourcesPaths) {
			InputStream is = null;
			try {
				Resource resource = resourceLoader.getResource(location);
				is = resource.getInputStream();
				propertiesPersister.load(props, new InputStreamReader(is, DEFAULT_ENCODING));
			} catch (IOException ex) {
				ex.printStackTrace();
			} finally {
				if (is != null) {
					is.close();
				}
			}
		}
	}
	
	/*public static void main(String[] args) {
		System.out.println(get("username", "ZH"));
		System.out.println(get("username", "EN"));
		System.out.println(get("username", "ZH"));
		System.out.println(get("username", "EN"));
		System.out.println(get("username", "ZH"));
		System.out.println(get("username", "EN"));
		System.out.println(get("username", "ZH"));
		System.out.println(get("username", "EN"));
	}*/
	
	// 已经加载过的资源,为了高效，不使用锁
	private static HashMap<String, String> loadeds = new HashMap<String, String>();
	
	/**
	 * 根据一个key和语言获取对应的数据，并且保证系统延迟加载
	 * @param key
	 * @param language
	 * @return
	 * @throws IOException
	 */
	public static String get(String key, String language) {
		// 判断是否已经加载过此资源文件
		if (loadeds.get("messages_" + language) == null) {
			// 防止多线程
			synchronized (loadeds) {
				// 防止重复加载资源
				if (loadeds.get("messages_" + language) == null) {
					try {
						// 加载资源
						loadProperties("messages_" + language + ".properties");
						loadeds.put("messages_" + language, "messages_" + language + ".properties");
					} catch (IOException e) {
						// 如果失败，默认选择香港
						return null;
					}
				}
			}
		}
		return properties.get(key + "-" + language).toString();
	}
	private static Properties properties = null;
	// 编码
	private static final String DEFAULT_ENCODING = "UTF-8";
	// Spring-core 获取配置文件的类
	private static PropertiesPersister propertiesPersister = null;
	// Spring-core 获取资源的类
	private static ResourceLoader resourceLoader = null;
}