com.anysoft.util.Settings Maven / Gradle / Ivy
package com.anysoft.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import com.anysoft.util.resource.ResourceFactory;
/**
* 全局的配置变量集
* 包含两个方面的功能:
* 维护一个全局性的变量集
* 维护一个全局性的对象列表
*
* @author duanyy
* @since 1.3.1 [20140808 duanyy]
* - 增加静态方法:{@link Settings#getClassLoader()}
* - 增加静态方法:{@link Settings#getResourceFactory()}
*
* @since 1.3.2 [20140814 duanyy]
* - 优化get函数的共享锁机制
*
* @version 1.5.2 [20141017 duanyy]
* - 实现Reportable接口
*
* @version 1.6.4.17 [20151216 duanyy]
* - 根据sonar建议优化代码
*
* @version 1.6.5.4 [20160515 duanyy]
* - XML配置文件的变量可以写入到SystemProperties之中
*
* @version 1.6.7.9 [20170201 duanyy]
* - 采用SLF4j日志框架输出日志
*
* @version 1.6.8.7 [20170412 duanyy]
* - DefaultProperties容器由Hashtable更改为HashMap
*/
public class Settings extends DefaultProperties implements XmlSerializer,Reportable{
/**
* JRE环境变量集,作为本变量集的父节点
*/
protected static Properties system = new SystemProperties();
/**
* 全局对象列表
*/
protected Map objects = new ConcurrentHashMap();
/**
* 构造函数
*/
protected Settings(){
super("Settings",system);
}
/**
* 全局唯一实例
*/
protected static Settings instance = null;
protected static Object lock = new Object();
/**
* 获取全局唯一实例
* @return 对象实例
*/
public static Settings get(){
if (null == instance){
synchronized (lock){
if (null == instance){
instance = new Settings();
}
}
}
return instance;
}
/**
* 获取当前可用的ClassLoader
* @return ClassLoader
*/
public static ClassLoader getClassLoader(){
ClassLoader cl = null;
if (instance != null){
cl = (ClassLoader) instance.get("classLoader");
}
return cl == null ? Thread.currentThread().getContextClassLoader() : cl;
}
/**
* 获取可用的ResourceFactory
* @return ResourceFactory
*/
public static ResourceFactory getResourceFactory(){
ResourceFactory rf = null;
if (instance != null){
rf = (ResourceFactory)instance.get("ResourceFactory");
}
return rf == null ? new ResourceFactory() : rf;
}
/**
* 将变量集写出到XML文档
* @param doc XML文档实例
* @see #toXML(Element)
*/
protected void saveToDocument(Document doc){
if (doc == null)return ;
Element root = doc.getDocumentElement();
toXML(root);
}
/**
* 从XML文档中读入变量信息
* @param doc XML文档实例
* @see #toXML(Element)
*/
protected void loadFromDocument(Document doc){
if (doc == null){
return ;
}
Element root = doc.getDocumentElement();
fromXML(root);
}
@Override
public void report(Element xml) {
if (xml != null){
Document doc = xml.getOwnerDocument();
//parameters
{
Element _parameters = doc.createElement("parameters");
toXML(_parameters);
xml.appendChild(_parameters);
}
//objects
{
Element _objects = doc.createElement("objects");
Iterator _keys = objects.keySet().iterator();
while (_keys.hasNext()){
String key = _keys.next();
Object obj = objects.get(key);
Element _object = doc.createElement("object");
_object.setAttribute("id", key);
_object.setAttribute("content", obj.toString());
_objects.appendChild(_object);
}
xml.appendChild(_objects);
}
}
}
@Override
public void report(Map json) {
if (json != null){
//parameters
{
List