configmanager.java

来自「java 调用windows的api」· Java 代码 · 共 106 行

JAVA
106
字号
package org.jawin.browser.config;

import java.util.Properties;
import java.io.IOException;
import org.jawin.browser.util.FileUtilities;

/**
 * A singleton that exposes configuration parameters
 * loaded from disk on startup
 *
 * <p>Title: Jawin Code Generation GUI</p>
 * <p>Description: GUI for exploring type libraries and generating Java code</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Open Source Incentive</p>
 *
 * @author Josh Passenger
 * @version 1.0
 */

public class ConfigManager
{
	private static ConfigManager instance = null;
	private Properties config = null;

	private ConfigManager()
	{
		loadConfiguration();
	}

	public static synchronized void initialize()
	{
		instance = new ConfigManager();
	}

	public static ConfigManager getInstance()
	{
		if (instance == null)
		{
			throw new IllegalStateException("ConfigManager.getInstance() ConfigManager was not initialized");
		}

		return instance;
	}

	/**
	 * Return a boolean flag indicating if the value of the
	 * key is, case insensitively equal to true
	 *
	 * @param key the config file key
	 * @return the value of the key or false if it does not exist
	 */
	public boolean getBoolean(String key)
	{
		return Boolean.valueOf(config.getProperty(key)).booleanValue();
	}

	/**
	 * Fetch the value fo the config key or the defaultValue if it is not specified
	 *
	 * @param key the key in the config file
	 * @param defaultValue the default value to return if the config is not specified
	 * @return the value of the key or the default value if it is not specified
	 */
	public String getString(String key, String defaultValue)
	{
		return config.getProperty(key, defaultValue);
	}

	/**
	 * Fetch the value of the specified key as an int or the default value if it
	 * does not exist or is not a valid int
	 *
	 * @param key the config key
	 * @param defaultValue the default value to return on failure
	 * @return the int value of the key or the default value
	 */
	public int getInt(String key, int defaultValue)
	{
		int value;

		try
		{
			value = Integer.parseInt(config.getProperty(key));
		}
		catch (Exception ex)
		{
			value = defaultValue;
		}

		return value;

	}

	private void loadConfiguration()
	{
		try
		{
			config = FileUtilities.loadProperties("config/config.properties");
		}
		catch (IOException io)
		{
			config = new Properties();
			io.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

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