ApplicationConfiguration 加载

本文介绍了一个使用Java实现的应用配置管理系统,该系统通过解析XML配置文件来动态加载和管理应用程序的属性设置,利用DOM4J和Apache Commons Logging进行XML解析和日志记录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package BaseClass;

import java.io.File;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ApplicationConfiguration
{
  private final String extend_name = ".xml";
  private static final Log log = LogFactory.getLog(ApplicationConfiguration.class);
  private static final ApplicationConfiguration applicationConfiguration = new ApplicationConfiguration();
  private Properties properties = new Properties();

  private ApplicationConfiguration()
  {
    String URL = Thread.currentThread().getContextClassLoader().getClass().getResource("/").getPath();
    File dir = new File(URL);
    File[] lrcFiles = dir.listFiles(new FilenameFilter(this)
    {
      public boolean accept(, String name)
      {
        return (name.endsWith(".xml"));
      }

    });
    for (int i = 0; i < lrcFiles.length; ++i) {
      String name = lrcFiles[i].getName();
      if (name.substring(name.indexOf(".")).equals(".cfg.xml")) {
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
        if (is == null)
          throw new RuntimeException("无法找到应用定义文件" + name);
        try
        {
          SAXReader reader = new SAXReader();
          Document doc = reader.read(is);
          Element root = doc.getRootElement();
          Element propertiesElement = root.element("properties");
          if (propertiesElement == null) break label277;
          for (Iterator it = propertiesElement.elementIterator("property"); it.hasNext(); ) {
            Element element = (Element)it.next();
            String propertyName = element.attributeValue("name");
            String propertyValue = element.getText();

            label277: this.properties.put(propertyName, propertyValue);
          }
        }
        catch (Exception e) {
          log.error(e, e);
          throw new RuntimeException(e);
        }
      }
    }
  }

  public static ApplicationConfiguration getConfiguration()
  {
    return applicationConfiguration;
  }

  public String getProperty(String name)
  {
    return this.properties.getProperty(name);
  }

  public Properties getProperties() {
    return this.properties;
  }

  public void setProperties(Properties properties) {
    this.properties = properties;
  }

  public String[] getFilenameWithExtendName(String in_dir, String type_name)
  {
    File dir = new File(in_dir);
    File[] lrcFiles = dir.listFiles(new FilenameFilter(this, type_name)
    {
      public boolean accept(, String name)
      {
        return (name.endsWith(this.val$type_name));
      }

    });
    String name = null;
    ArrayList listName = new ArrayList();
    for (int i = 0; i < lrcFiles.length; ++i) {
      name = lrcFiles[i].getName();
      if (name.substring(name.indexOf(".")).equals(type_name))
        listName.add(name);
    }

    return ((String[])listName.toArray());
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值