userproperties.java
来自「一个用java写的mail.里面的代码值得我们去研究!学习。」· Java 代码 · 共 1,173 行 · 第 1/3 页
JAVA
1,173 行
String rsrcName = UserProperties.getProperty( rsrcTag, null ); if ( rsrcName != null ) { boolean result = UserProperties.loadPropertiesResource( rsrcName, props ); if ( ! result ) { System.err.println( "ERROR loading property resource '" + rsrcName + "'" ); } } } } } // UNDONE // This routine need to use JNDI (?) to get a 'global' property // file name (typically on a network mounted volume) to read, // which should in turn set the name of the local property file. // JNDI should also set some 'critical' properties, such as // the important OTA hostnames, service ports, etc. // REVIEW // UNDONE // This routine should have a 'filter' that filters out all // global properties that do not start with prefix? /** * Load all related properties for this application. * This class method will look for a global properties * file, loading it if found, then looks for a local * properties file and loads that. */ static public void loadProperties( String packageName, Properties appProps ) { boolean result; File propFile; String propPath; String propName; String rsrcName; if ( UserProperties.debug ) { UserProperties.printContext( System.err ); } Properties sysProps = System.getProperties(); if ( sysProps == null ) return; UserProperties.defaultProperties( sysProps ); // // ---- PROCESS THE DEFAULT PROPERTIES RESOURCE // rsrcName = UserProperties.defaultsResource; if ( rsrcName == null ) { rsrcName = UserProperties.getProperty( UserProperties.DEFAULTS_RSRC_NAME, null ); } if ( UserProperties.debug ) System.err.println( "Default Properties Resource '" + rsrcName + "'" ); if ( rsrcName != null ) { result = UserProperties.loadPropertiesResource( rsrcName, sysProps ); System.err.println( "Loaded " + ( result ? "the " : "no " ) + "default properties." ); } // // ---- PROCESS THE APPLICATION DEFAULT PROPERTIES // if ( appProps != null ) { if ( UserProperties.debug ) System.err.println( "Adding application default properties." ); UserProperties.addDefaultProperties( sysProps, appProps ); } // // ---- PROCESS THE PREFIX PROPERTY // String newPrefix = UserProperties.prefix; if ( UserProperties.debug ) System.err.println( "Prefix '" + newPrefix + "'" ); if ( newPrefix == null ) { UserProperties.getProperty( packageName + "." + UserProperties.PREFIX_PROPERTY, null ); if ( newPrefix != null ) { if ( UserProperties.debug ) System.err.println( "Prefix via property '" + newPrefix + "'" ); UserProperties.setPropertyPrefix( newPrefix ); if ( UserProperties.verbose ) System.err.println( "Property prefix set to '" + newPrefix + "'" ); } } // // ---- PROCESS THE GLOBAL PROPERTIES RESOURCES // UserProperties.loadPropertyResourceList( UserProperties.GLOBAL_RSRCLIST_NAME, UserProperties.GLOBAL_RSRC_PREFIX, sysProps ); // // ---- PROCESS THE GLOBAL DYNAMIC PROPERTY REGISTRATIONS // UserProperties.processDynamicProperties(); // // ---- PROCESS THE LOCAL PROPERTIES FILE // propPath = UserProperties.localPropertyFile; if ( propPath == null ) { propPath = UserProperties.getProperty( UserProperties.LOCAL_PROPERTY, UserProperties.LOCAL_DEFAULT ); } if ( UserProperties.debug ) System.err.println( "Local property file '" + propPath + "'" ); if ( propPath != null ) { propFile = new File( propPath ); if ( propFile.exists() ) { result = UserProperties.loadPropertiesFile( propPath, sysProps, null ); if ( ! result ) { System.err.println( "ERROR loading local property file '" + propPath + "'" ); } } } // // ---- PROCESS THE GLOBAL PROPERTIES RESOURCES // UserProperties.loadPropertyResourceList( UserProperties.APP_RSRCLIST_NAME, UserProperties.APP_RSRC_PREFIX, sysProps ); } private static void processDynamicProperties() { // // First, register any dynamic property definitions // defined by global properties. // Properties sysProps = System.getProperties(); String dynPropList = sysProps.getProperty( "global.dynamicPropList", null ); if ( dynPropList != null ) { String[] dynList = StringUtilities.splitString( dynPropList, ":" ); for ( int sIdx = 0 ; sIdx < dynList.length ; ++sIdx ) { String dynName = dynList[sIdx]; String pathPropName = "global.dynamicPropFile." + dynName; String dynPath = sysProps.getProperty( pathPropName, null ); if ( dynPath != null ) { if ( dynPath.startsWith( "~" + File.separator ) ) { dynPath = sysProps.getProperty( "user.home", "" ) + dynPath.substring( 2 ); } UserProperties.registerDynamicProperties( dynName, dynPath, new Properties() ); } } } // Now, we do the actual loading of dynamic properties. Enumeration names = UserProperties.dynKeysTable.keys(); for ( ; names.hasMoreElements() ; ) { String name = (String) names.nextElement(); String path = (String)UserProperties.dynPathTable.get( name ); UserProperties.loadDynamicProperties( name, path ); } } public static void registerDynamicProperties( String name, String path, Properties props ) { UserProperties.dynPathTable.put( name, path ); UserProperties.addDynamicPropertyKeys( name, props ); } private static void addDynamicPropertyKeys( String name, Properties dynProps ) { Vector dynKeys = (Vector)UserProperties.dynKeysTable.get( name ); if ( dynKeys == null ) { dynKeys = ( dynProps == null ) ? new Vector( 0 ) : new Vector( dynProps.size() ); UserProperties.dynKeysTable.put( name, dynKeys ); } if ( dynProps != null ) { // Ensure all key names are Enumeration keys = dynProps.keys(); for ( ; keys.hasMoreElements() ; ) { String keyName = (String)keys.nextElement(); if ( ! dynKeys.contains( keyName ) ) dynKeys.addElement( keyName ); } } } /** * This method expects the property keys to be * <strong>normalized</strong>, meaning that they are * the full property name with the prefix added on. */ private static void copyDynamicProperties( String name, Properties props ) { String path = (String) UserProperties.dynPathTable.get( name ); Vector keys = (Vector) UserProperties.dynKeysTable.get( name ); if ( keys == null || path == null ) throw new NoSuchElementException( "you have not registered the dynamic property " + "package named '" + name + "'" ); Properties sysProps = System.getProperties(); try { Enumeration enum = props.keys(); for ( ; enum.hasMoreElements() ; ) { String key = (String)enum.nextElement(); if ( key != null ) { String normalKey = UserProperties.normalizePropertyName( key ); sysProps.put( normalKey, props.get( key ) ); if ( ! keys.contains( normalKey ) ) keys.addElement( normalKey ); } } } catch ( NoSuchElementException ex ) { } } public static void setDynamicProperties( String name, Properties props ) { UserProperties.copyDynamicProperties( name, props ); } /** * This method expects the property keys to be <strong>not</strong> * <em>normalized</em>, meaning that they are the full * property name with the prefix added on. */ public static void setDynamicProperty( String name, String propName, String propValue ) { UserProperties.workingProps.clear(); UserProperties.workingProps.put( propName, propValue ); UserProperties.setDynamicProperties( name, UserProperties.workingProps ); } /** * This method removes a property from the dynamic properties. */ public static void removeDynamicProperty( String name, String propName ) { String path = (String) UserProperties.dynPathTable.get( name ); Vector keys = (Vector) UserProperties.dynKeysTable.get( name ); if ( keys == null || path == null ) throw new NoSuchElementException( "you have not registered the dynamic property " + "package named '" + name + "'" ); String normalKey = UserProperties.normalizePropertyName( propName ); if ( keys.contains( normalKey ) ) { keys.removeElement( normalKey ); System.getProperties().remove( normalKey ); } } public static void saveDynamicProperties( String name ) throws IOException { String path = (String) UserProperties.dynPathTable.get( name ); Vector keys = (Vector) UserProperties.dynKeysTable.get( name ); if ( keys == null || path == null ) throw new NoSuchElementException( "you have not registered the dynamic property " + "package named '" + name + "'" ); Properties dynProps = new Properties(); Properties sysProps = System.getProperties(); int count = keys.size(); for ( int idx = 0 ; idx < count ; ++idx ) { String pName = (String) keys.elementAt(idx); dynProps.put( pName, sysProps.get( pName ) ); } UserProperties.saveDynamicPropFile( name, path, dynProps ); } // // UNDONE // We should use an intermediate file and move at completion. This // would eliminate the file being trashed on an IOException. // private static void saveDynamicPropFile( String name, String path, Properties dynProps ) throws IOException { String eol = System.getProperty( "line.separator", "\n" ); String comment = eol + "## -------------------- W A R N I N G -------------------- " + eol + "# This file is automatically generated." + eol + "# Any changes you make to this file will be overwritten." + eol + "## ---------------------------------------------------------" + eol + "#"; FileOutputStream out = new FileOutputStream( path ); dynProps.put( "global.dynPropVersion." + name, UserProperties.DYNAMIC_PROPERTY_VERSION );// Java 1.1, 1.2 (deprecated), 1.3 (deprecated) dynProps.save( out, comment );// dynProps.store( out, comment ); out.close(); } public static void printContext( PrintStream out ) { out.println( "os.name = '" + UserProperties.osname + "'" ); out.println( "user.name = '" + UserProperties.userName + "'" ); out.println( "user.home = '" + UserProperties.userHome + "'" ); out.println( "java.home = '" + UserProperties.javaHome + "'" ); out.println( "" ); out.println( "prefix = '" + UserProperties.prefix + "'" ); out.println( "osSuffix = '" + UserProperties.osSuffix + "'" ); out.println( "userSuffix = '" + UserProperties.userSuffix + "'" ); out.println( "" ); } public static void printUsage( PrintStream out ) { out.println( "Properties options:" ); out.println( " -propDebug -- " + "turns on debugging of property loading" ); out.println( " -propVerbose -- " + "turns on verbose messages during loading" ); out.println( " -propDefaults rsrcName -- " + "sets default properties resource name" ); out.println( " -propFile path -- " + "sets application property file path" ); out.println( " -propOS suffix -- " + "sets the os suffix" ); out.println( " -propUser suffix -- " + "sets the user suffix" ); out.println( " -propPrefix prefix -- " + "sets application property prefix" ); } static public String [] processOptions( String [] args ) { Vector newArgs = new Vector( args.length ); for ( int iArg = 0 ; iArg < args.length ; ++iArg ) { if ( args[iArg].equals( "-propPrefix" ) && (iArg + 1) < args.length ) { UserProperties.setPropertyPrefix( args[++iArg] ); } else if ( args[iArg].equals( "-propFile" ) && (iArg + 1) < args.length ) { UserProperties.setLocalPropertyFile( args[++iArg] ); } else if ( args[iArg].equals( "-propDefaults" ) && (iArg + 1) < args.length ) { UserProperties.setDefaultsResource( args[++iArg] ); } else if ( args[iArg].equals( "-propDebug" ) ) { UserProperties.setDebug( true ); } else if ( args[iArg].equals( "-propVerbose" ) ) { UserProperties.setVerbose( true ); } else if ( args[iArg].equals( "-propOS" ) && (iArg + 1) < args.length ) { UserProperties.setOSSuffix( args[++iArg] ); } else if ( args[iArg].equals( "-propUser" ) && (iArg + 1) < args.length ) { UserProperties.setUserSuffix( args[++iArg] ); } else { newArgs.addElement( args[iArg] ); } } String[] result = new String[ newArgs.size() ]; for ( int i = 0 ; i < newArgs.size() ; ++i ) { result[i] = (String) newArgs.elementAt(i); } return result; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?