userproperties.java
来自「一个用java写的mail.里面的代码值得我们去研究!学习。」· Java 代码 · 共 1,173 行 · 第 1/3 页
JAVA
1,173 行
/*** $Id: UserProperties.java,v 1.4 2001/05/07 12:34:45 kunugi Exp $**** Copyright (c) 2000-2001 Jeff Gay** on behalf of ICEMail.org <http://www.icemail.org>** Copyright (c) 1998-2000 by Timothy Gerard Endres** ** This program is free software.** ** You may redistribute it and/or modify it under the terms of the GNU** General Public License as published by the Free Software Foundation.** Version 2 of the license should be included with this distribution in** the file LICENSE, as well as License.html. If the license is not** included with this distribution, you may find a copy at the FSF web** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.**** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR** REDISTRIBUTION OF THIS SOFTWARE. */package org.icemail.util;import java.awt.Font;import java.awt.Color;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.IOException;import java.io.PrintStream;import java.lang.reflect.Field;import java.util.Enumeration;import java.util.Hashtable;import java.util.NoSuchElementException;import java.util.Properties;import java.util.Vector;/** * The UserProperties class. This class is used to extend * the concept of System.getProperty(). This class adds the * following features: * * <ul> * <li> A hierarchical property definition structure. * <li> Typed property retrieval with default values. * <li> Hierarchical overrides allowings properties to * be overridden on a per user or per host or per host * and user basis. * <li> The ability to load from any valid resource, including * files stored in JAR files, files located via the file system, * which includes networked file systems, as well as any other * resource that can be identified via a URL, including web pages, * FTP-ed files, and more. * </ul> * * <p> * Here is how it works. We have <em>six</em> levels of * properties which are loaded based on various settings. * The levels look like this: * * <ol> * <li> Hardwired defaults. * <li> Application defined defaults. * <li> Defaults resource. * <li> System level resource list. * <li> Application property file. * <li> Application resource list. * </ol> * * <p> * Resource lists are colon (:) separated strings listing * resource names to be loaded into the properties. * * <p> * In a typical deployment, the developer will place the * defaults resource in the JAR file containing the application's * classes. This file will then define the application properties * file, which will usually be left empty allowing the user to * place all customizations in this local file. For distributed * applications, system resources will typically be supplied via * simple web pages, which allows for automatic updates of many * properties. The application resource list is then typically * reserved for specific user customizations, or for distributed * customizations, or updates. * * <p> * Typically, the System Resource List is defined in the * Defaults Resource. However, it can also be defined by * the application defaults, or can be hardwired into the * code if desired. Further, the Application Resource List * is typically defined by the Application Property File, * although it can be defined in any of the previous loaded * resources. * * <p> * Also note that the application prefix can be set at any * point up to and including the defaults resource. After * the defaults resource is loaded, the prefix property is * consulted, and if set, is used as the new application * prefix. The prefix property is named by adding together * the application's package name and the string 'propertyPrefix'. * Thus, the prefix property for 'org.icemail.mail' would be named * 'org.icemail.mail.propertyPrefix', and would typically be set * in the defaults resource. * * <p> * Things the application <strong>must</strong> do to use this class: * * <ul> * <li> Set the property prefix via UserProperties.setPropertyPrefix() * <li> Set the defaults resource via UserProperties.setDefaultsResource() * <li> Process any arguments via UserProperties.processOptions() * <li> Load all properties. * </ul> * * <p> * Here is an example from a typical main(): * <pre> * UserProperties.setPropertyPrefix( "WebTool." ); * * UserProperties.setDefaultsResource * ( "/org/icemail/webtool/defaults.txt" ); * * // PROCESS PROPERTIES OPTIONS * // The returned args are those not processed. * args = UserProperties.processOptions( args ); * * // Now app should process remaining arguments... * * // LOAD PROPERTIES * UserProperties.loadProperties( "org.icemail.mail", null ); * </pre> * * <p> * Properties are accessed via the getProperty() methods, which * provide versions for String, int, double, and boolean values. * Any property is looked for as follows: * * <ol> * <li> fullname.osname.username * <li> fullname.username * <li> fullname.osname * <li> fullname * </ol> * * Whichever property is found first is returned. * The <em>fullname</em> consists of the property name prefixed * by the application's property prefix. The username and osname * suffixes are used to override general properties by os or by * user. These suffixes are printed at startup to System.err. * The osname suffix is the osname with spaces replaced by * underscores. The username suffix is the user's name with * spaces replaced by underscores. * * <p> * If the property name starts with a period * (.), then the prefix is not added to get the full name. * If the property name ends with a period (.), then none * of the suffixes are applied, and only the name is used * to search for a property. * * <p> * Thus, while the property name "mainWindow.x" would match * a property definition named "prefix.mainWindow.x.user", the * property ".mainWindow.x." would match a property with only * that name and no prefix or suffix, and the property * "mainWindow.x." would match "prefix.mainWindow.x" only * and not allow for suffix overrides. * * <p> * The following parameters are understood by processOptions(): * * <ul> * <li> -propPrefix prefix -- sets the property prefix. * <li> -propFile filename -- sets the application property file. * <li> -propDefaults resource -- sets the defaults resource name. * <li> -propDebug -- turns on debugging of property handling. * <li> -propVerbose -- turns on verbosity. * <li> -propOS osname -- set the property os suffix. * <li> -propUser username -- set the property user name suffix. * </ul> * */public abstract class UserProperties { private static final String PREFIX_PROPERTY = "propertyPrefix"; private static final String DEFAULTS_RSRC_NAME = ".org.icemail.global.defaultsResource."; private static final String GLOBAL_RSRCLIST_NAME = ".org.icemail.global.propertyResourceList"; private static final String GLOBAL_RSRC_PREFIX = ".org.icemail.global.propertyResource."; private static final String APP_RSRCLIST_NAME = ".org.icemail.local.propertyResourceList"; private static final String APP_RSRC_PREFIX = ".org.icemail.local.propertyResource."; private static final String LOCAL_PROPERTY = "global.localPropertyFile"; private static final String LOCAL_DEFAULT = null; private static final String DYNAMIC_PROPERTY_VERSION = "1.0"; private static boolean debug; private static boolean verbose; private static String osname; private static String userName; private static String userHome; private static String javaHome; private static String prefix; private static String osSuffix; private static String userSuffix; private static String defaultsResource; private static String localPropertyFile; /** * This is a Hashtable of Vectors. The table keys are * dynamic property package names. Each Vector contains * the list of property names in the dynamic package. */ private static Hashtable dynKeysTable; /** * This is a Hashtable of Strings. The table keys are * dynamic property package names. Each String is the * pathname to the property file for the dynamic package. */ private static Hashtable dynPathTable; /** * Used for temporary working properties. */ private static Properties workingProps; static { UserProperties.debug = false; UserProperties.verbose = false; UserProperties.prefix = null; UserProperties.defaultsResource = null; UserProperties.localPropertyFile = null; UserProperties.dynKeysTable = new Hashtable(); UserProperties.dynPathTable = new Hashtable(); UserProperties.workingProps = new Properties(); UserProperties.osname = System.getProperty( "os.name" ); UserProperties.userName = System.getProperty( "user.name" ); UserProperties.userHome = System.getProperty( "user.home" ); UserProperties.javaHome = System.getProperty( "java.home" ); UserProperties.osSuffix = UserProperties.osname.replace( ' ', '_' ); UserProperties.userSuffix = UserProperties.userName.replace( ' ', '_' ); } static public String getOSName() { return UserProperties.osname; } static public String getUserHome() { return UserProperties.userHome; } static public String getUserName() { return UserProperties.userName; } static public void setDebug( boolean debug ) { UserProperties.debug = debug; } static public void setVerbose( boolean verbose ) { UserProperties.verbose = verbose; } static public void setLocalPropertyFile( String fileName ) { UserProperties.localPropertyFile = fileName; } static public void setDefaultsResource( String rsrcName ) { UserProperties.defaultsResource = rsrcName; } static public void setOSSuffix( String suffix ) { UserProperties.osSuffix = suffix; } static public void setUserSuffix( String suffix ) { UserProperties.userSuffix = suffix; } static public void setPropertyPrefix( String prefix ) { if ( prefix.endsWith( "." ) ) UserProperties.prefix = prefix; else UserProperties.prefix = prefix + "."; } static public String getPropertyPrefix() { return UserProperties.prefix; } static public String getLineSeparator() { return System.getProperty( "line.separator", "\n" ); } static public Font getFont( String name, Font defaultFont ) { return Font.getFont( UserProperties.prefixedPropertyName( name ), defaultFont ); } static public Color getColor( String name, Color defaultColor ) { return Color.getColor( UserProperties.prefixedPropertyName( name ), defaultColor ); } static public String prefixedPropertyName( String name ) { return UserProperties.prefix + name; } public static String normalizePropertyName( String name ) { if ( name.startsWith( "." ) ) return name.substring(1); else return UserProperties.prefixedPropertyName( name ); } /** * Retrieve a system string property. * Returns a provided default value if the property * is not defined. * * @param name The name of the property to retrieve. * @param defval A default string value. * @return The string value of the named property. */ static private String getOverridableProperty( String name, String defval ) { String value = null; String overName = null; String fullName = null; fullName = UserProperties.normalizePropertyName( name ); if ( fullName.endsWith( "." ) ) { fullName = fullName.substring( 0, fullName.length() - 1 ); value = System.getProperty( fullName, defval ); if ( UserProperties.debug ) System.err.println( "UserProperties.getOverridableProperty: " + fullName + " = '" + value + "'" ); return value; } if ( UserProperties.osSuffix != null && UserProperties.userSuffix != null ) { overName = fullName + "." + UserProperties.osSuffix + "." + UserProperties.userSuffix; value = System.getProperty( overName, null ); if ( UserProperties.debug ) System.err.println( "UserProperties.getOverridableProperty: " + overName + " = '" + value + "'" ); if ( value != null ) return value; } if ( UserProperties.userSuffix != null ) { overName = fullName + "." + UserProperties.userSuffix; value = System.getProperty( overName, null );
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?