⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 configmanager.java

📁 j2ee源码
💻 JAVA
字号:
package com.leeman.common.resource;

import java.util.*;
import java.io.*;

import com.leeman.common.db.*;

public class ConfigManager {
	
	//private static final String CONFIG_FILE_NAME = "lkets";
	//private static final String CONFIG_FILE_PATH = "C:\\lkets_cfg.ini";
	private static final String CONFIG_FILE_NAME = "wkexs";
	private static final String CONFIG_FILE_PATH = "C:\\wkexs_cfg.ini";
		 	
	public ConfigManager(){}
	
	/**
	 * Get the Configuration Parameter
	 * @param parameter's name
	 * @return parameter's value
	 * @throws Exception
	 */

	public static void readFileToSystemProperites() throws Exception
	{
		//Debug
		//System.out.println("Trying to read the properites file...");
		File file = new File(CONFIG_FILE_PATH);
		Properties properties = new Properties();
		
		try{
			FileInputStream fi = new FileInputStream(CONFIG_FILE_PATH);
			properties.load(fi);
			Enumeration emu = properties.keys();
			while (emu.hasMoreElements()) {
				String theKey = (String) emu.nextElement();
				String theValue = properties.getProperty(theKey);
				//Debug
				System.out.println(CONFIG_FILE_NAME + "_" + theKey + "=" + theValue);
				System.setProperty(CONFIG_FILE_NAME + "_" + theKey,theValue);
			}
			
			System.setProperty(CONFIG_FILE_NAME + "_" + "CONFIG_LAST_MODIFYED", String.valueOf(file.lastModified()));
					
			try{
				fi.close();
				fi = null;
			}catch(Exception E){}
		}
		catch(FileNotFoundException e)
		{
			throw new Exception(CONFIG_FILE_NAME + " Configuration file not found. " + file.getAbsolutePath());
		}
		
		//Debug
		//System.out.println("Finish to read the properites file...");
	}
	
	public static String getConfig(String parameterName) throws Exception
	{
		//Debug
		//System.out.println("getConfig");
		String propValue = System.getProperty(CONFIG_FILE_NAME + "_" + parameterName);
		
		File file = new File(CONFIG_FILE_PATH);
		if (!file.isFile())
		{
			throw new Exception(CONFIG_FILE_NAME + " Configuration file not found." + file.getAbsolutePath());
		}
		
		if (propValue == null || !String.valueOf(file.lastModified()).equals(System.getProperty(CONFIG_FILE_NAME + "_" + "CONFIG_LAST_MODIFYED")))
		{
			readFileToSystemProperites();
			propValue = System.getProperty(CONFIG_FILE_NAME + "_"+ parameterName);
		}
		if (propValue == null)
		{
			throw new Exception("Parameter '" + parameterName + "' not found.");
		}
		else
		{
			return propValue;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -