ApplicationConfiguration 加载

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());
  }
}

你可能感兴趣的:(Java,java)