All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.anysoft.util.Settings Maven / Gradle / Ivy

There is a newer version: 1.6.17
Show newest version
package com.anysoft.util;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
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日志框架输出日志
    */ public class Settings extends DefaultProperties implements XmlSerializer,Reportable{ /** * JRE环境变量集,作为本变量集的父节点 */ protected static Properties system = new SystemProperties(); /** * 全局对象列表 */ protected Hashtable objects = new Hashtable(); /** * 构造函数 */ 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"); Enumeration _keys = objects.keys(); while (_keys.hasMoreElements()){ String key = _keys.nextElement(); 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 _parameters = new ArrayList(); Enumeration keys = keys(); while (keys.hasMoreElements()){ String key = keys.nextElement(); String value = _GetValue(key); Map _parameter = new HashMap(2); _parameter.put("id", key); _parameter.put("value", value); _parameters.add(_parameter); } json.put("parameter",_parameters); } //objects { List _objects = new ArrayList(); Enumeration _keys = objects.keys(); while (_keys.hasMoreElements()){ String key = _keys.nextElement(); Object obj = objects.get(key); Map _object = new HashMap(2); _object.put("id", key); _object.put("content", obj.toString()); _objects.add(_object); } json.put("object",_objects); } } } /** * 输出到XML节点 * @param root 输出信息的根节点 */ @Override public void toXML(Element root) { //为了输出文件的美观,添加一个\n文件节点 Document doc = root.getOwnerDocument(); root.appendChild(doc.createTextNode("\n")); Enumeration ids = keys(); while (ids.hasMoreElements()){ String id = (String)ids.nextElement(); String value = _GetValue(id); if (value.length() <= 0 || id.length() <= 0){ continue; } Element e = doc.createElement("parameter"); e.setAttribute("id",id); e.setAttribute("value",value); root.appendChild(e); //为了输出文件的美观,添加一个\n文件节点 root.appendChild(doc.createTextNode("\n")); } } /** * 从XML节点中读入 * @param root 读入信息的根节点 */ @Override public void fromXML(Element root) { loadFrom(root); } /** * 获取对象列表中指定id的对象 * @param id * @return Object */ public Object get(String id){ return objects.get(id); } /** * 向对象列表注册已经创建好的对象 * @param id 对象id * @param obj 对象实例 */ public void registerObject(String id,Object obj){ if (obj != null){ objects.put(id, obj); } } /** * 创建指定的对象并注册到对象列表 * @param id 对象id * @param className 对象的类名 */ public void registerObject(String id, String className) { try { registerObject(id,Class.forName(className).newInstance()); } catch (Exception ex){ logger.error("Can not register object:" + className,ex); } } /** * 从对象列表中注销指定ID的对象 * @param id */ public void unregisterObject(String id){ objects.remove(id); } }