jConfig提供一组用来管理Properties的API,使用它可以更简便,快捷的处理系统的配置文件。jConfig使用xml文件来保存配置信息,相对于传统的properties文件,编程访问和保存配置信息都十分方便。
在项目中,后台的数据保存在Sun ONE Directory Server上,程序通过jndi访问它。客户要求支持LDAP Server双机,这就需要配置两组属性,分别用来连接两台LDAP Server。使用jConfig,非常简单的实现了这个需求。
首先配置文件放置在系统etc目录下,名字为jndi.xml,内容如下:
<?xml version="1.0" ?>
<properties>
<category name="master">
<property name="java.naming.provider.url" value="ldap://192.168.2.14:389/dc=myentry,dc=china,dc=huawei,dc=com"/>
<property name="java.naming.security.principal" value="cn=Directory Manager"/>
<property name="java.naming.security.credentials" value="Encrypted Password"/>
</category>
<category name="slave">
<property name="java.naming.provider.url" value="ldap://192.168.2.215:389/dc=myentry,dc=china,dc=huawei,dc=com"/>
<property name="java.naming.security.principal" value="cn=Directory Manager"/>
<property name="java.naming.security.credentials" value="Encrypted Password"/>
</category>
</properties>
名字为master的category为主机的上下文信息,slave为备机。
程序JNDIFactory.java用来读取配置信息。注释信息为相关程序的解释。
public class JNDIFactory {
// Returns the one and only instance of the ConfigurationManager.
private static final ConfigurationManager cm = ConfigurationManager
.getInstance();
// A key to specify the name of properties
private static final String PROPERTIES_NAME = "PROPERTIES_NAME";
// Loads all the properties
public List<Properties> loadAllProperties() {
File file = new File("etc/jndi.xml");
XMLFileHandler handler = new XMLFileHandler();
handler.setFile(file);
List<Properties> pros = new ArrayList<Properties>();
try {
// Configuration name
String configurationName = "IPASS.JNDI.CONFIG";
// Loads a configuration with the specific ConfigurationHandler and
// stores it in the internal map with the given name.
cm.load(handler, configurationName);
// Returns the configuration for the specific name
Configuration config = ConfigurationManager
.getConfiguration(configurationName);
// Removes the general Category that I do not need
config.removeCategory("general");
// Returns all the names of the properties for the specific category
String[] names = config.getCategoryNames();
for (String CategoryName : names) {
// Loads properties of the specific category
Properties p = config.getProperties(CategoryName);
// Here are some default values
p.setProperty(PROPERTIES_NAME, CategoryName);
p.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
p.setProperty(Context.OBJECT_FACTORIES,
"com.mi.ldap.ProductLDAPFactory");
p.setProperty(Context.STATE_FACTORIES,
"com.mi.ldap.ProductLDAPFactory");
p.setProperty(Context.SECURITY_AUTHENTICATION, "simple");
decryptPassword(p);
pros.add(p);
}
// config.setProperty("TEST", "Added property");
// System.out.println("trying to save file");
// cm.save(handler, config);
// System.out.println("file successfully saved");
// System.out.println("TEST:" + config.getProperty("TEST"));
} catch (Exception e) {
e.printStackTrace();
}
return pros;
}
/**
* Decrypt this CREDENTIALS
*/
private void decryptPassword(Properties p) {
String pwd = p.getProperty(Context.SECURITY_CREDENTIALS);
// TODO Decrypt this pwd
p.setProperty(Context.SECURITY_CREDENTIALS, pwd);
}}
配置信息的保存也十分方便,Properties的内容更新后,直接执行ConfigurationManager.save(handler, configuration),即可保存到jndi.xml文件中。
下载地址:www.jconfig.org,具体的多种使用方法可以参考下载文件包中的demo.