一个处理Properties的好东西 jConfig

jConfig提供一组用来管理Properties的API,使用它可以更简便,快捷的处理系统的配置文件。jConfig使用xml文件来保存配置信息,相对于传统的properties文件,编程访问和保存配置信息都十分方便。

在项目中,后台的数据保存在Sun ONE Directory Server上,程序通过jndi访问它。客户要求支持LDAP Server双机,这就需要配置两组属性,分别用来连接两台LDAP Server。使用jConfig,非常简单的实现了这个需求。

首先配置文件放置在系统etc目录下,名字为jndi.xml,内容如下:



 
       
   
 
 
 
      
   
 

名字为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 loadAllProperties() {
  File file = new File("etc/jndi.xml");
  XMLFileHandler handler = new XMLFileHandler();
  handler.setFile(file);
  List pros = new ArrayList ();
  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.




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值