package org.colorcoding.ibas.bobas.configuration;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import javax.xml.bind.JAXBException;

import org.colorcoding.ibas.bobas.data.DataConvert;
import org.colorcoding.ibas.bobas.messages.RuntimeLog;

/**
 * 配置
 * 
 * @author Niuren.Zhu
 *
 */
public class Configuration {

	private volatile static IConfigurationManager configuration;

	public static IConfigurationManager getInstance() {
		if (configuration == null) {
			synchronized (Configuration.class) {
				if (configuration == null) {
					try {
						String folder = getStartupFolder();
						if (folder.endsWith("target" + File.separator + "test-classes")) {
							// 测试脚本 target\test-classes
							folder = (new File(folder)).getParentFile().getParentFile().getPath();
						}
						String configFile = String.format("%s%sapp.xml", folder, File.separator);
						configuration = ConfigurationManager.create(configFile);
					} catch (FileNotFoundException | JAXBException e) {
						// 读取配置文件出错
						e.printStackTrace();
					}
					if (configuration == null) {
						configuration = new ConfigurationManager();
					}
				}
			}
		}
		return configuration;
	}

	/**
	 * 程序启动的目录（主要的配置文件目录）
	 * 
	 * @return
	 */
	public static String getStartupFolder() {
		try {
			File file = null;
			URL url = Thread.currentThread().getContextClassLoader().getResource("");
			String path;
			path = url.toURI().getPath();
			// file:/E:/WorkTemp/ibas/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/ibcp.systemcenter.service/WEB-INF/classes/
			// 取到的值如上
			if (path.split(":").length > 2) {
				path = path.substring(path.indexOf(":") + 1, path.length());
			}
			if (path.indexOf("!") > 0) {
				path = path.substring(0, path.indexOf("!"));
			}
			if (path == null) {
				path = System.getProperty("user.dir");
			}
			file = new File(path);
			if (file.isFile()) {
				file = file.getParentFile();
			}
			if (file.getParentFile().isDirectory() && file.getParentFile().getName().equals("WEB-INF")) {
				// web路径
				file = file.getParentFile();
			}
			return file.getPath();
		} catch (URISyntaxException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 获取配置的值
	 * 
	 * @param key
	 *            配置项
	 * 
	 * @param defaultValue
	 *            默认值
	 * 
	 * @return 配置的值（P类型）
	 */
	@SuppressWarnings("unchecked")
	public static <P> P getConfigValue(String key, P defaultValue) {
		String valueString = getConfigValue(key);
		if (valueString == null || valueString.equals("")) {
			return defaultValue;
		} else {
			try {
				// 强行转换配置值为P类型
				if (defaultValue != null) {
					return (P) DataConvert.convert(defaultValue.getClass(), valueString);
				}
				return (P) valueString;
			} catch (Exception e) {
				RuntimeLog.log(e);
				return defaultValue;
			}
		}
	}

	/**
	 * 静态方法，供直接调用
	 * 
	 * @param key
	 * @return
	 */
	public static String getConfigValue(String key) {
		return getInstance().getValue(key);
	}

	/**
	 * 配置项目-工作目录
	 */
	public static String CONFIG_ITEM_WORK_FOLDER = "WorkFolder";

	private volatile static String workFolder = null;

	/**
	 * 获取工作目录
	 * 
	 * @return
	 */
	public static String getWorkFolder() {
		if (workFolder == null) {
			synchronized (Configuration.class) {
				if (workFolder == null) {
					String path = getConfigValue(CONFIG_ITEM_WORK_FOLDER);
					if (path == null || path.equals("")) {
						// 没有配置工作目录
						path = getStartupFolder();
					}
					workFolder = (new File(path)).getPath();
				}
			}
		}
		return workFolder;
	}

	/**
	 * 获取资源地址
	 * 
	 * @param type
	 *            资源名称
	 * @return 统一格式（此对象避免路径的中文问题）
	 * @throws URISyntaxException
	 */
	public static URI getResource(String name) throws URISyntaxException {
		URL url = Thread.currentThread().getContextClassLoader().getResource(name);
		if (url == null) {
			return null;
		}
		return url.toURI();
	}

}

@@ -1471,42 +1471,183 @@
 path
-;%0A%09%09%09path = url.toURI().getPath();
+ = null;%0A%09%09%09if (url != null) %7B%0A%09%09%09%09URI uri = url.toURI();%0A%09%09%09%09if (uri != null) %7B%0A%09%09%09%09%09path = uri.getPath();%0A%09%09%09%09%7D%0A%09%09%09%09if (path == null) %7B%0A%09%09%09%09%09path = url.getPath();%0A%09%09%09%09%7D%0A%09%09%09%7D
 %0A%09%09%09
@@ -1791,16 +1791,40 @@
  %E5%8F%96%E5%88%B0%E7%9A%84%E5%80%BC%E5%A6%82%E4%B8%8A%0A
+%09%09%09if (path != null) %7B%0A%09
 %09%09%09if (p
@@ -1848,24 +1848,25 @@
 ngth %3E 2) %7B%0A
+%09
 %09%09%09%09path = p
@@ -1914,32 +1914,33 @@
 .length());%0A
+%09
 %09%09%09%7D%0A%09%09%09
 if (path.ind
@@ -1919,32 +1919,33 @@
 th());%0A%09%09%09%09%7D%0A%09%09%09
+%09
 if (path.indexOf
@@ -1953,24 +1953,25 @@
 %22!%22) %3E 0) %7B%0A
+%09
 %09%09%09%09path = p
@@ -2007,16 +2007,22 @@
 (%22!%22));%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%09

修正启动路径获取问题。