文章标题:基于xml文件实现系统属性配置管理 .

文章地址: http://blog.csdn.net/5iasp/article/details/11774501

作者: javaboy2012
Email:yanek@163.com
qq:    1046011462

项目截图;

主要有如下几个类和配置文件实现:

1. SystemProperties

package com.yanek.cfg;

import java.util.Collection;
import java.util.Map; public interface SystemProperties
extends Map
{ public abstract Collection getChildrenNames(String s); public abstract Collection getPropertyNames(); public void init();
}

2.  XMLSystemProperties类

package com.yanek.cfg;

import java.io.*;
import java.util.*;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.*;
import org.apache.log4j.Logger; public class XMLSystemProperties
implements SystemProperties
{
static Logger logger = Logger.getLogger(XMLSystemProperties.class.getName()); private File file;
private Document doc;
private Map propertyCache;
private Object propLock; public XMLSystemProperties(InputStream in)
throws Exception
{
propertyCache = new HashMap();
propLock = new Object();
Reader reader = new BufferedReader(new InputStreamReader(in));
buildDoc(reader);
} public XMLSystemProperties(String fileName)
throws IOException
{
File tempFile;
boolean error=false;
Reader reader;
propertyCache = new HashMap();
propLock = new Object();
file = new File(fileName);
if(!file.exists())
{
tempFile = new File(file.getParentFile(), file.getName() + ".tmp");
if(tempFile.exists())
{
logger.error("WARNING: " + fileName + " was not found, but temp file from " + "previous write operation was. Attempting automatic recovery. Please " + "check file for data consistency.");
tempFile.renameTo(file);
} else
{
throw new FileNotFoundException("XML properties file does not exist: " + fileName);
}
}
tempFile = new File(file.getParentFile(), file.getName() + ".tmp");
if(tempFile.exists())
{
logger.error("WARNING: found a temp file: " + tempFile.getName() +
". This may " +
"indicate that a previous write operation failed. Attempting automatic " +
"recovery. Please check file " + fileName +
" for data consistency.");
if (tempFile.lastModified() > file.lastModified())
{
error = false;
reader = null;
try{
reader = new InputStreamReader(new FileInputStream(tempFile),
"UTF-8");
SAXReader xmlReader = new SAXReader();
xmlReader.read(reader);
}catch(Exception e){
try
{
reader.close();
}
catch (Exception ex)
{}
error = true;
}
}
}
if(error)
{
String bakFile = tempFile.getName() + "-" + System.currentTimeMillis() + ".bak";
tempFile.renameTo(new File(tempFile.getParentFile(), bakFile));
} else
{
/*String bakFile = file.getName() + "-" + System.currentTimeMillis() + ".bak";
file.renameTo(new File(file.getParentFile(), bakFile));
try
{
Thread.sleep(100L);
}
catch(Exception e) { }
tempFile.renameTo(file);*/
}
error = false;
reader = null;
try{
reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
SAXReader xmlReader = new SAXReader();
xmlReader.read(reader);
try
{
reader.close();
}
catch (Exception e)
{}
}catch(Exception e){
error = true;
}
if(error)
{
String bakFileName = file.getName() + "-" + System.currentTimeMillis() + ".bak";
File bakFile = new File(file.getParentFile(), bakFileName);
file.renameTo(bakFile);
try
{
Thread.sleep(100L);
}
catch(Exception e) { }
tempFile.renameTo(file);
} /*else
{
String bakFile = tempFile.getName() + "-" + System.currentTimeMillis() + ".bak";
tempFile.renameTo(new File(tempFile.getParentFile(), bakFile));
}*/
if(!file.canRead())
throw new IOException("XML properties file must be readable: " + fileName);
if(!file.canWrite())
throw new IOException("XML properties file must be writable: " + fileName);
reader = null;
try
{
reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
buildDoc(reader);
}
catch(Exception e)
{
logger.error("Error creating XML properties file " + fileName + ": " + e.getMessage());
throw new IOException(e.getMessage());
}
finally { }
try
{
reader.close();
}
catch(Exception e)
{
e.printStackTrace();
}
} public Object get(Object o)
{
String name;
String value;
Element element;
name = (String)o;
value = (String)propertyCache.get(name);
if(value != null)
return value;
String propName[] = parsePropertyName(name);
element = doc.getRootElement();
for(int i = 0; i < propName.length; i++)
{
element = element.element(propName[i]);
if(element == null)
return null;
} synchronized(propLock){
value = element.getText();
if ("".equals(value))
return null;
value = value.trim();
propertyCache.put(name, value);
return value;
}
} public Collection getChildrenNames(String parent)
{
String propName[] = parsePropertyName(parent);
Element element = doc.getRootElement();
for(int i = 0; i < propName.length; i++)
{
element = element.element(propName[i]);
if(element == null)
return Collections.EMPTY_LIST;
} List children = element.elements();
int childCount = children.size();
List childrenNames = new ArrayList(childCount);
for(Iterator i = children.iterator(); i.hasNext(); childrenNames.add(((Element)i.next()).getName()));
return childrenNames;
} public Collection getPropertyNames()
{
List propNames = new java.util.LinkedList();
List elements = doc.getRootElement().elements();
if(elements.size() == 0)
return Collections.EMPTY_LIST;
for(int i = 0; i < elements.size(); i++)
{
Element element = (Element)elements.get(i);
getElementNames(propNames, element, element.getName());
} return propNames;
} public String getAttribute(String name, String attribute)
{
if(name == null || attribute == null)
return null;
String propName[] = parsePropertyName(name);
Element element = doc.getRootElement();
int i = 0;
do
{
if(i >= propName.length)
break;
String child = propName[i];
element = element.element(child);
if(element == null)
break;
i++;
} while(true);
if(element != null)
return element.attributeValue(attribute);
else
return null;
} private void getElementNames(List list, Element e, String name)
{
if(e.elements().isEmpty())
{
list.add(name);
} else
{
List children = e.elements();
for(int i = 0; i < children.size(); i++)
{
Element child = (Element)children.get(i);
getElementNames(list, child, name + '.' + child.getName());
} }
} public synchronized Object put(Object k, Object v)
{
String name = (String)k;
String value = (String)v;
propertyCache.put(name, value);
String propName[] = parsePropertyName(name);
Element element = doc.getRootElement();
for(int i = 0; i < propName.length; i++)
{
if(element.element(propName[i]) == null)
element.addElement(propName[i]);
element = element.element(propName[i]);
} element.setText(value);
saveProperties();
return null;
} public synchronized void putAll(Map propertyMap)
{
String propertyName;
String propertyValue;
for(Iterator iter = propertyMap.keySet().iterator(); iter.hasNext(); propertyCache.put(propertyName, propertyValue))
{
propertyName = (String)iter.next();
propertyValue = (String)propertyMap.get(propertyName);
String propName[] = parsePropertyName(propertyName);
Element element = doc.getRootElement();
for(int i = 0; i < propName.length; i++)
{
if(element.element(propName[i]) == null)
element.addElement(propName[i]);
element = element.element(propName[i]);
} if(propertyValue != null)
element.setText(propertyValue);
} saveProperties();
} public synchronized Object remove(Object n)
{
String name = (String)n;
propertyCache.remove(name);
String propName[] = parsePropertyName(name);
Element element = doc.getRootElement();
for(int i = 0; i < propName.length - 1; i++)
{
element = element.element(propName[i]);
if(element == null)
return null;
} String value = element.getText();
element.remove(element.element(propName[propName.length - 1]));
saveProperties();
return value;
} public String getProperty(String name)
{
return (String)get(name);
} public boolean containsKey(Object object)
{
return get(object) != null;
} public boolean containsValue(Object object)
{
throw new UnsupportedOperationException("Not implemented in xml version");
} public Collection values()
{
throw new UnsupportedOperationException("Not implemented in xml version");
} public boolean isEmpty()
{
return false;
} public int size()
{
throw new UnsupportedOperationException("Not implemented in xml version");
} public Set entrySet()
{
throw new UnsupportedOperationException("Not implemented in xml version");
} public void clear()
{
throw new UnsupportedOperationException("Not implemented in xml version");
} public Set keySet()
{
throw new UnsupportedOperationException("Not implemented in xml version");
} private void buildDoc(Reader in)
throws Exception
{
SAXReader xmlReader = new SAXReader();
doc = xmlReader.read(in);
} private synchronized void saveProperties()
{
Writer writer;
boolean error;
File tempFile;
writer = null;
error = false;
tempFile = null;
try{
tempFile = new File(file.getParentFile(), file.getName() + ".tmp");
writer = new OutputStreamWriter(new FileOutputStream(tempFile),
"UTF-8");
XMLWriter xmlWriter = new XMLWriter(writer,
OutputFormat.createPrettyPrint());
xmlWriter.write(doc);
try
{
writer.close();
}
catch (Exception ex)
{
logger.error(ex);
error = true;
}
}catch(Exception ex){
logger.error("Unable to write to file " + file.getName() + ".tmp" +
": " + ex.getMessage());
error = true;
}finally{
try
{
writer.close();
}
catch (Exception e)
{
logger.error(e);
error = true;
}
}
if(error)
return;
error = false;
if(file.exists() && !file.delete())
{
logger.error("Error deleting property file: " + file.getAbsolutePath());
return;
}
try{
writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
XMLWriter xmlWriter = new XMLWriter(writer,
OutputFormat.createPrettyPrint());
xmlWriter.write(doc);
try
{
writer.close();
}
catch (Exception e)
{
logger.error(e);
error = true;
}
}catch(Exception e){
logger.error("Unable to write to file '" + file.getName() + "': " +
e.getMessage());
error = true;
try
{
file.delete();
}
catch(Exception fe) { }
try
{
writer.close();
}
catch(Exception ex)
{
logger.error(ex);
error = true;
}
}
if(!error)
tempFile.delete();
} private String[] parsePropertyName(String name)
{
List propName = new ArrayList(5);
for(StringTokenizer tokenizer = new StringTokenizer(name, "."); tokenizer.hasMoreTokens(); propName.add(tokenizer.nextToken()));
return (String[])(String[])propName.toArray(new String[propName.size()]);
}
public void init() {
}
}

3. SystemGlobals

package com.yanek.cfg;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger; import javax.naming.InitialContext; import org.dom4j.Document;
import org.dom4j.io.SAXReader; public class SystemGlobals
{
static class InitPropLoader
{ public String getSystemHome()
{
String systemHome;
InputStream in;
systemHome = null;
in = null;
if(systemHome == null)
{
try
{
in = getClass().getResourceAsStream("/sys_init.xml");
if (in != null)
{
Document doc = (new SAXReader()).read(in);
systemHome = doc.getRootElement().getText();
}
}
catch (Exception e)
{
SystemGlobals.log.log(Level.SEVERE,
"Error loading sys_init.xml to find systemHome -> " +
e.getMessage(), e);
}
finally
{
try
{
if (in != null)
in.close();
}
catch (Exception e)
{}
}
}
if(systemHome != null)
for(systemHome = systemHome.trim(); systemHome.endsWith("/") || systemHome.endsWith("\\"); systemHome = systemHome.substring(0, systemHome.length() - 1));
if("".equals(systemHome))
systemHome = null;
return systemHome;
} InitPropLoader()
{
}
} private static final Logger log;
private static String SYS_CONFIG_FILENAME = "system_config.xml"; public static String systemHome = null;
public static boolean failedLoading = false;
private static SystemProperties setupProperties = null; private static Date startupDate = new Date(); private SystemGlobals()
{
} public static Date getStartupDate()
{
return startupDate;
} public static String getSystemHome()
{
if(systemHome == null)
loadSetupProperties();
return systemHome;
} public static synchronized void setSystemHome(String sysHome)
{ setupProperties = null;
failedLoading = false;
systemHome = sysHome; loadSetupProperties();
System.err.println("Warning - sysHome is being reset to " + sysHome + "! Resetting the baduHome is a normal part of the setup process, " + "however it should not occur during the normal operations of System.");
} public static void setConfigName(String configName)
{
SYS_CONFIG_FILENAME = configName;
} public static boolean isSetup()
{
return "true".equals(getLocalProperty("setup"));
} public static String getLocalProperty(String name)
{
if(setupProperties == null)
loadSetupProperties();
if(setupProperties == null)
return null;
else
return (String)setupProperties.get(name);
} public static int getLocalProperty(String name, int defaultValue)
{
String value;
value = getLocalProperty(name);
if(value != null)
{
try{
return Integer.parseInt(value);
}catch(NumberFormatException nfe){}
}
return defaultValue;
} public static List getLocalProperties(String parent)
{
if(setupProperties == null)
loadSetupProperties();
if(setupProperties == null)
return Collections.EMPTY_LIST;
Collection propNames = setupProperties.getChildrenNames(parent);
List values = new ArrayList();
Iterator i = propNames.iterator();
do
{
if(!i.hasNext())
break;
String propName = (String)i.next();
String value = getLocalProperty(parent + "." + propName);
if(value != null)
values.add(value);
} while(true);
return values;
} public static void setLocalProperty(String name, String value)
{
if(setupProperties == null)
loadSetupProperties();
if(setupProperties != null)
setupProperties.put(name, value);
} public static void setLocalProperties(Map propertyMap)
{
if(setupProperties == null)
loadSetupProperties();
if(setupProperties != null)
setupProperties.putAll(propertyMap);
} public static void deleteLocalProperty(String name)
{
if(setupProperties == null)
loadSetupProperties();
setupProperties.remove(name);
} public static boolean isWhiteLabel(){
return true;
} private static synchronized void loadSetupProperties()
{
if(failedLoading)
return;
if(setupProperties == null)
{
if(systemHome == null)
systemHome = (new InitPropLoader()).getSystemHome();
if(systemHome == null)
try
{
InitialContext context = new InitialContext();
systemHome = (String)context.lookup("java:comp/env/baduHome");
}
catch(Exception e) { }
if(systemHome == null)
systemHome = System.getProperty("systemHome");
if(systemHome == null)
{
failedLoading = true;
StringBuffer msg = new StringBuffer();
msg.append("Critical Error! The systemHome directory could not be loaded, \n");
msg.append("which will prevent the application from working correctly.\n\n");
msg.append("You must set systemHome in one of three ways:\n");
msg.append(" 1) Add a sys_init.xml file to your classpath, which points \n ");
msg.append(" to sysHome. Normally, this file will be in WEB-INF/classes.\n");
msg.append(" 2) Set the JNDI value \"java:comp/env/systemHome\" with a String \n");
msg.append(" that points to your systemHome directory. \n");
msg.append(" 3) Set the Java system property \"systemHome\".\n\n");
msg.append("Further instructions for setting systemHome can be found in the \n");
msg.append("installation documentation.");
System.err.println(msg.toString());
return;
}
try
{
File jh = new File(systemHome);
if(!jh.exists())
log.severe("Error - the specified systemHome directory does not exist (" + systemHome + ")");
else
if(!jh.canRead() || !jh.canWrite())
log.severe("Error - the user running this System application can not read and write to the specified systemHome directory (" + systemHome + "). Please grant the executing user " + "read and write perms.");
setupProperties = new XMLSystemProperties(systemHome + File.separator + SYS_CONFIG_FILENAME);
}
catch(IOException ioe)
{
log.log(Level.SEVERE, ioe.getMessage(), ioe);
failedLoading = true;
}
}
} static
{
log = Logger.getLogger((com.yanek.cfg.SystemGlobals.class).getName());
} }

4.  配置文件 sys_init.xml  ,

定义xml 配置文件的目录

<?xml version="1.0"?>
<systemHome>E:\work\XmlProp\conf</systemHome>

5. xml 配置文件:

<?xml version="1.0" encoding="UTF-8"?>

<config>
<setup>true</setup>
<upload>
<file>
<imgFilePath>f:\img\</imgFilePath>
<maxSize>1024</maxSize>
<allowFileType>gif,bmp,png,jpg</allowFileType>
</file>
</upload>
<prop>
<connection>
<pool>
<name>proxool.test.prop</name>
</pool>
<jndi>
<name>test/test</name>
</jndi>
</connection>
</prop>
<log4j>
<filePath>WEB-INF/classes/log4j.properties</filePath>
</log4j>
<taskEngine>
<status>false</status>
</taskEngine>
<list>
<x>aaaa</x>
<y>222</y>
<z>333</z>
</list>
<dbname>testdb</dbname>
<db>
<username>test</username>
<password>test</password>
</db>
</config>

6. 测试类:

package com.yanek.cfg;

import java.util.List;

public class Test {

	/**
* @param args
*/
public static void main(String[] args) { System.out.println("setup:"+SystemGlobals.getLocalProperty("setup"));
System.out.println("taskEngine.status:"+SystemGlobals.getLocalProperty("taskEngine.status"));
System.out.println("prop.connection.pool.name:"+SystemGlobals.getLocalProperty("prop.connection.pool.name")); List list=SystemGlobals.getLocalProperties("upload.file");
System.out.println(list);
for(int i=0;i<list.size();i++)
{
System.out.println((String)list.get(i));
} SystemGlobals.setLocalProperty("db.username", "test");
SystemGlobals.setLocalProperty("db.password", "test"); } }

相关资源免积分下载:http://download.csdn.net/detail/5iasp/6282149

基于xml文件实现系统属性配置管理的更多相关文章

  1. Spring中Bean的配置:基于XML文件的方式

    Bean的配置一共有两种方式:一种是基于XML文件的方式,另一种是基于注解的方式.本文主要介绍基于XML文件的方式 <bean id="helloWorld" class=& ...

  2. Spring框架入门之基于xml文件配置bean详解

    关于Spring中基于xml文件配置bean的详细总结(spring 4.1.0) 一.Spring中的依赖注入方式介绍 依赖注入有三种方式 属性注入 构造方法注入 工厂方法注入(很少使用,不推荐,本 ...

  3. java struts2入门学习--基于xml文件的声明式验证

    一.知识点总结 后台验证有两种实现方式: 1 手工验证顺序:validateXxx(针对Action中某个业务方法验证)--> validate(针对Action中所有的业务方法验证) 2 声明 ...

  4. jboss:在standalone.xml中设置系统属性(system-properties)

    就象在.net的web应用中,可以在web.config中设置appSettings一样,jboss的standalone.xml中也可以由开发人员自行添加系统属性,用法如下: </extens ...

  5. PHP对XML文件操作之属性与方法讲解

    DOMDocument相关的内容. 属性: Attributes 存储节点的属性列表(只读) childNodes 存储节点的子节点列表(只读) dataType 返回此节点的数据类型 Definit ...

  6. 自定义MVC框架(二) -基于XML文件

    1.oracle的脚本 create table STUDENT ( sid NUMBER primary key, sname ), age NUMBER, pwd ) ) create seque ...

  7. Java:使用DOM4j来实现读写XML文件中的属性和元素

    DOM4可以读取和添加XML文件的属性或者元素 读取属性: public static void ReadAttributes() throws DocumentException { File fi ...

  8. 基于XML的类的属性的装配

    基于XML的属性装配 1.手动装配 <!-- 属性的装配:手动装配 --> <bean id="userService" class="com.neue ...

  9. vue项目中使用bpmn-流程图xml文件中节点属性转json结构

    内容概述 本系列“vue项目中使用bpmn-xxxx”分为七篇,均为自己使用过程中用到的实例,手工原创,目前陆续更新中.主要包括vue项目中bpmn使用实例.应用技巧.基本知识点总结和需要注意事项,具 ...

随机推荐

  1. PHP比较全的友好的时间显示,比如‘刚刚’,'几秒前'等

    分享一个php友好的比较完成的时间格式化函数,包括‘刚刚’,'几秒之前',‘几分钟前’,'几小时前',几天前,几周前,几个月前等.调用方式很简单,是从ThinkSNS 里面拿出来的. /** * 友好 ...

  2. Python中文显示问题

    默认pyhon使用ASCII码来解释程序的,默认不支持中文,需要在程序的第一行或者第二行声明编码. 官方解决方案:https://www.python.org/dev/peps/pep-0263/ T ...

  3. 5.java.lang.IndexOutOfBoundsException(数组下标越界异常)

    数组下标越界异常 查看调用的数组或者字符串的下标值是不是超出了数组的范围,一般来说,显示(即直接用常数当下标)调用不太容易出这样的错,但隐式(即用变量表示下标)调用就经常出错了,还有一种情况,是程序中 ...

  4. javascript将异步校验表单改写为同步表单

    同步表单校验的缺点 响应错误信息时,需要重新加载整个页面(虽然有缓存,客户端仍然需要通过http协议对比每个文件是否有更新,以保持文件最新) 服务器响应错误以后,用户之前所输入的信息全部丢失了,用户需 ...

  5. Open source and free log analysis and log management tools.

    Open source and free log analysis and log management tools. Maintained by Dr. Anton Chuvakin Version ...

  6. spring mvc 提交表单的例子

    1. 构建MAVEN项目,然后转换成web格式,结构图如下: 2. 通过@RequestMapping来进行配置,当输入URL时,会以此找到对应方法执行,首先调用setupForm方法,该方法主要是生 ...

  7. 【虚拟化实战】容灾设计之四VPLEX

    作者:范军 (Frank Fan) 新浪微博:@frankfan7 VPLEX等存储设备的出现,可以实现双活数据中心,最大程度的有效利用运算和存储资源. 在“容灾设计之三Stretched Clust ...

  8. iso-开发基础知识-1-程序流程

    main-应用程序委托-视图控制器 main()---主函数 应用程序委托  ---AppDelegate     视图控制器 ---ViewController - (BOOL)applicatio ...

  9. AS3事件机制概述

    事件机制是AS3的核心功能之一,没有充分掌握事件机制的方方面面,就不能算是精通AS3语言. 1. AS3事件机制的主要成员 IEventDispatcher:事件派发对象接口,定义了添加.派发.移除. ...

  10. 【Android进阶学习】shape和selector的结合使用

    shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector.可以这样说,shape和 ...