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

📄 ini.java

📁 Java写的ERP系统
💻 JAVA
字号:
/******************************************************************************
 * The contents of this file are subject to the   Compiere License  Version 1.1
 * ("License"); You may not use this file except in compliance with the License
 * You may obtain a copy of the License at http://www.compiere.org/license.html
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Original Code is                  Compiere  ERP & CRM  Business Solution
 * The Initial Developer of the Original Code is Jorg Janke  and ComPiere, Inc.
 * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
 * created by ComPiere are Copyright (C) ComPiere, Inc.;   All Rights Reserved.
 * Contributor(s): ______________________________________.
 *****************************************************************************/
package org.compiere.util;

import java.util.*;
import java.io.*;
import java.security.*;
import java.sql.Timestamp;

import org.compiere.plaf.*;

/**
 *	Load & Save INI Settings fopm property file
 *	Initiated in Compiere.startup
 *	Settings activated in ALogin.getIni
 *
 *  @author     Jorg Janke
 *  @version    $Id: Ini.java,v 1.19 2003/01/11 05:46:34 jjanke Exp $
 */
public final class Ini implements Serializable
{
	public static final String	COMPIERE_PROPERTY_FILE = "Compiere.properties";

	//	Property Constants and Default Values
	public static final String	P_UID = 			"ApplicationUserID";
	private static final String	DEFAULT_UID = 		"System";
	//
	public static final String	P_PWD = 			"ApplicationPassword";
	private static final String	DEFAULT_PWD = 		"System";
	//
	public static final String	P_STORE_PWD = 		"StorePassword";
	private static final boolean DEFAULT_STORE_PWD = true;
	//
	public static final String	P_DEBUGLEVEL = 		"DebugLevel";
	private static final int 	DEFAULT_DEBUGLEVEL = 0;
	//
	public static final String 	P_LANGUAGE = 		"Language";
	private static final String DEFAULT_LANGUAGE = 	Language.getName
		(System.getProperty("user.language") + "_" + System.getProperty("user.country"));
	//
	public static final String 	P_INI = 			"FileNameINI";
	private static final String DEFAULT_INI = 		"";
	//
	public static final String	P_CONNECTION =		"Connection";
	private static final String	DEFAULT_CONNECTION = "";
	//
	public static final String  P_CONTEXT = 		"DataSource";
	private static final String	DEFAULT_CONTEXT	= 	"OracleDS";
	//
	public static final String	P_OBJECTS = 		"ServerObjects";
	private static final boolean DEFAULT_OBJECTS = 	true;
	//
	public static final String	P_UI_LOOK =			"UILookFeel";
	private static final String	DEFAULT_UI_LOOK =	CompiereLookAndFeel.NAME;
	//
	public static final String	P_UI_THEME =		"UITheme";
	private static final String	DEFAULT_UI_THEME =	CompiereTheme.NAME;
	//
	public static final String  P_A_COMMIT =		"AutoCommit";
	private static final boolean DEFAULT_A_COMMIT =	true;
	//
	public static final String	P_A_LOGIN =			"AutoLogin";
	private static final boolean DEFAULT_A_LOGIN =	false;
	//
	public static final String  P_COMPIERESYS =		"CompiereSys";	//	Save system records
	private static final boolean DEFAULT_COMPIERESYS = false;
	//
	public static final String  P_SHOW_ACCT =		"ShowAcct";
	private static final boolean DEFAULT_SHOW_ACCT = false;
	//
	public static final String  P_SHOW_TRL =		"ShowTrl";
	private static final boolean DEFAULT_SHOW_TRL =	false;
	//
	public static final String  P_TEMP_DIR =    	"TempDir";
	private static final String  DEFAULT_TEMP_DIR =	"";
	//
	public static final String  P_ROLE =			"Role";
	private static final String  DEFAULT_ROLE =		"";
	//
	public static final String	P_CLIENT =			"Client";
	private static final String	DEFAULT_CLIENT =	"";
	//
	public static final String	P_ORG =				"Organization";
	private static final String	DEFAULT_ORG =		"";
	//
	public static final String  P_PRINTER =			"Printer";
	private static final String  DEFAULT_PRINTER =	"";
	//
	public static final String  P_WAREHOUSE =		"Warehouse";
	private static final String  DEFAULT_WAREHOUSE = "";
	//
	public static final String  P_TODAY =       	"Today";
	private static final Timestamp DEFAULT_TODAY =	new Timestamp(System.currentTimeMillis());
	//
	public static final String  P_PRINTPREVIEW = 	"PrintPreview";
	private static final boolean DEFAULT_PRINTPREVIEW =	false;
	//
	private static final String P_WARNING =	    	"Warning";
	private static final String DEFAULT_WARNING =	"Do_not_change_any_of_the_data_as_they_will_have_undocumented_side_effects.";
	private static final String P_WARNING_de =		"WarningD";
	private static final String DEFAULT_WARNING_de ="Einstellungen_nicht_aendern,_da_diese_undokumentierte_Nebenwirkungen_haben.";

	//
	private static final String[]   PROPERTIES = new String[] {
		P_UID, P_PWD, P_DEBUGLEVEL, P_LANGUAGE, P_INI,
		P_CONNECTION, P_OBJECTS, P_STORE_PWD,
		P_UI_LOOK, P_UI_THEME,
		P_A_COMMIT, P_A_LOGIN, P_COMPIERESYS, P_SHOW_ACCT, P_SHOW_TRL,
		P_CONTEXT, P_TEMP_DIR,
		P_ROLE, P_CLIENT, P_ORG, P_PRINTER, P_WAREHOUSE, P_TODAY,
		P_PRINTPREVIEW,
		P_WARNING, P_WARNING_de
	};
	private static final String[]   VALUES = new String[] {
		DEFAULT_UID, DEFAULT_PWD, String.valueOf(DEFAULT_DEBUGLEVEL), DEFAULT_LANGUAGE, DEFAULT_INI,
		DEFAULT_CONNECTION, DEFAULT_OBJECTS?"Y":"N", DEFAULT_STORE_PWD?"Y":"N",
		DEFAULT_UI_LOOK, DEFAULT_UI_THEME,
		DEFAULT_A_COMMIT?"Y":"N", DEFAULT_A_LOGIN?"Y":"N", DEFAULT_COMPIERESYS?"Y":"N", DEFAULT_SHOW_ACCT?"Y":"N", DEFAULT_SHOW_TRL?"Y":"N",
		DEFAULT_CONTEXT, DEFAULT_TEMP_DIR,
		DEFAULT_ROLE, DEFAULT_CLIENT, DEFAULT_ORG, DEFAULT_PRINTER, DEFAULT_WAREHOUSE, DEFAULT_TODAY.toString(),
		DEFAULT_PRINTPREVIEW?"Y":"N",
		DEFAULT_WARNING, DEFAULT_WARNING_de
	};

	/**	Container for Properties    */
	private static Properties 		s_prop = new Properties();

	/**
	 *	Save INI parameters to disk
	 *  @param tryUserHome get user home first
	 */
	public static void saveProperties (boolean tryUserHome)
	{
		String fileName = getFileName (tryUserHome);
		FileOutputStream fos = null;
		try
		{
			File f = new File(fileName);
			fos = new FileOutputStream(f);
			s_prop.store(fos, "Compiere");
			fos.flush();
			fos.close();
		}
		catch (Exception e)
		{
			System.err.println ("Ini.saveProperties - Cannot save Properties to " + fileName + " - " + e.toString());
			return;
		}
		catch (Throwable t)
		{
			System.err.println ("Ini.saveProperties - Cannot save Properties to " + fileName + " - " + t.toString());
			return;
		}
	//	System.out.println("Ini.saveProperties: " + fileName);
	}	//	save

	/**
	 *	Load INI parameters from disk
	 *  @param reload reload
	 */
	public static void loadProperties (boolean reload)
	{
		if (reload || s_prop.size() == 0)
			loadProperties(getFileName(s_client));
	}	//	loadProperties

	/**
	 *  Load INI parameters from filename
	 *
	 * @param filename to load
	 * @return true if first time
	 */
	public static boolean loadProperties (String filename)
	{
		System.out.println("Ini.loadProperties: " + filename);
		//
		boolean loadOK = true;
		boolean firstTime = false;
		s_prop = new Properties();
		FileInputStream fis = null;
		try
		{
			fis = new FileInputStream(filename);
			s_prop.load(fis);
			fis.close();
		}
		catch (FileNotFoundException e)
		{
			System.err.println ("Ini.loadProperties "
				+ filename + " not found");
			loadOK = false;
		}
		catch (Exception e)
		{
			System.err.println ("Ini.loadProperties "
				+ filename + " - " + e.toString());
			loadOK = false;
		}
		catch (Throwable t)
		{
			System.err.println ("Ini.loadProperties "
				+ filename + " - " + t.toString());
			loadOK = false;
		}
		if (!loadOK)
		{
			firstTime = true;
			if (!IniDialog.accept())
				System.exit(-1);
		}

		//	Check/set properties	defaults
		for (int i = 0; i < PROPERTIES.length; i++)
		{
			if (VALUES[i].length() > 0)
				checkProperty(PROPERTIES[i], VALUES[i]);
		}
		//
		String tempDir = System.getProperty("java.io.tmpdir");
		if (tempDir == null || tempDir.length() == 1)
			tempDir = getCompiereHome();
		if (tempDir == null)
			tempDir = "";
		checkProperty(P_TEMP_DIR, tempDir);

		//  Save if not exist or could not be read
		if (!loadOK)
			saveProperties(true);
		s_loaded = true;
		return firstTime;
	}	//	loadProperties

	/**
	 *	Load property and set to default, if not existing
	 *
	 * 	@param key   Key
	 * 	@param defaultValue   Default Value
	 * 	@return Property
	 */
	private static String checkProperty (String key, String defaultValue)
	{
		String result = null;
		if (key.equals(P_WARNING) || key.equals(P_WARNING_de))
			result = defaultValue;
		else if (!isClient())
			result = s_prop.getProperty (key, Secure.CLEARTEXT + defaultValue);
		else
			result = s_prop.getProperty (key, Secure.encrypt(defaultValue));
		s_prop.setProperty (key, result);
		return result;
	}	//	checkProperty

	/**
	 *	Return File Name of INI file
	 *  <pre>
	 *  Examples:
	 *	    C:\WinNT\Profiles\jjanke\Compiere.properties
	 *      D:\Compiere2\Compiere.properties
	 *      Compiere.properties
	 *  </pre>
	 *  Can be overwritten by -DPropertyFile=myFile allowing multiple
	 *  configurations / property files.
	 *  @param tryUserHome get user home first
	 *  @return file name
	 */
	private static String getFileName (boolean tryUserHome)
	{
		if (System.getProperty("PropertyFile") != null)
			return System.getProperty("PropertyFile");
		//
		String base = null;
		if (tryUserHome && s_client)
			base = System.getProperty("user.home");
		//  Server
		if (!s_client || base == null || base.length() == 0)
		{
			String home = getCompiereHome();
			if (home != null)
				base = home;
		}
		if (base != null && !base.endsWith(File.separator))
			base += File.separator;
		if (base == null)
			base = "";
		//
		return base + COMPIERE_PROPERTY_FILE;
	}	//	getFileName

	/*************************************************************************/

	/**
	 *	Set Property
	 *  @param key   Key
	 *  @param value Value
	 */
	public static void setProperty (String key, String value)
	{
		if (s_prop == null)
			s_prop = new Properties();
		if (key.equals(P_WARNING) || key.equals(P_WARNING_de))
			s_prop.setProperty(key, value);
		else if (!isClient())
			s_prop.setProperty(key, Secure.CLEARTEXT + value);
		else
			s_prop.setProperty(key, Secure.encrypt(value));
	}	//	setProperty

	/**
	 *	Set Property
	 *  @param key   Key
	 *  @param value Value
	 */
	public static void setProperty(String key, boolean value)
	{
		setProperty (key, value ? "Y" : "N");
	}   //  setProperty

	/**
	 *	Set Property
	 *  @param key   Key
	 *  @param value Value
	 */
	public static void setProperty(String key, int value)
	{
		setProperty (key, String.valueOf(value));
	}   //  setProperty

	/**
	 *	Get Propery
	 *  @param key  Key
	 *  @return     Value
	 */
	public static String getProperty (String key)
	{
		if (key == null)
			return "";
		String retStr = s_prop.getProperty(key, "");
		if (retStr == null || retStr.length() == 0)
			return "";
		return Secure.decrypt(retStr);
	}	//	getProperty

	/**
	 *	Get Propery as Boolean
	 *  @param key  Key
	 *  @return     Value
	 */
	public static boolean getPropertyBool (String key)
	{
		return getProperty (key).equals("Y");
	}	//	getProperty

	/*************************************************************************/

	/**
	 *  Get Properties
	 *
	 * @return Ini properties
	 */
	public static Properties getProperties()
	{
		return s_prop;
	}   //  getProperties

	/**
	 *  toString
	 *  @return String representation
	 */
	public static String getAsString()
	{
		StringBuffer buf = new StringBuffer ("Ini - ");
		Enumeration e = s_prop.keys();
		while (e.hasMoreElements())
		{
			String key = (String)e.nextElement();
			buf.append(key).append("=");
			buf.append(getProperty(key)).append("; ");
		}
		return buf.toString();
	}   //  toString

	/*************************************************************************/

	/** System environment prefix                                       */
	public static final String  ENV_PREFIX = "env.";
	/** System Property Value of COMPIERE_HOME                          */
	public static final String  COMPIERE_HOME = "COMPIERE_HOME";

	/** IsClient Internal marker            */
	private static boolean      s_client = true;
	/** IsClient Internal marker            */
	private static boolean      s_loaded = false;

	/**
	 *  Are we in Client Mode ?
	 *  @return true if client
	 */
	public static boolean isClient()
	{
		return s_client;
	}   //  isClient

	/**
	 *  Set Client Mode
	 *  @param client client
	 */
	public static void setClient (boolean client)
	{
		s_client = client;
	}   //  setClient

	/**
	 *  Are the properties loaded?
	 *  @return true if properties loaded.
	 */
	public static boolean isLoaded()
	{
		return s_loaded;
	}   //  isLoaded

	/**
	 *  Get Compiere Home
	 *  @return CompiereHome or null
	 */
	public static String getCompiereHome()
	{
		String env = System.getProperty (ENV_PREFIX + COMPIERE_HOME);
		if (env == null)
			env = System.getProperty (COMPIERE_HOME);
		return env;
	}   //  getCompiereHome

	/**
	 *  Set Compiere Home
	 *  @param CompiereHome COMPIERE_HOME
	 */
	public static void setCompiereHome (String CompiereHome)
	{
		if (CompiereHome != null && CompiereHome.length() > 0)
			System.setProperty (COMPIERE_HOME, CompiereHome);
	}   //  setCompiereHome

}	//	Ini

⌨️ 快捷键说明

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