userproperties.java

来自「一个用java写的mail.里面的代码值得我们去研究!学习。」· Java 代码 · 共 1,173 行 · 第 1/3 页

JAVA
1,173
字号
      if ( UserProperties.debug )        System.err.println( "UserProperties.getOverridableProperty: " +                            overName + " = '" + value + "'" );      if ( value != null )        return value;    }    if ( UserProperties.osSuffix != null ) {      overName = fullName + "." + UserProperties.osSuffix;      value = System.getProperty( overName, null );      if ( UserProperties.debug )        System.err.println( "UserProperties.getOverridableProperty: " +                            overName + " = '" + value + "'" );      if ( value != null )        return value;    }    if ( value == null ) {      value = System.getProperty( fullName, null );      if ( UserProperties.debug )        System.err.println( "UserProperties.getOverridableProperty: " +                            fullName + " = '" + value + "'" );    }    if ( value == null ) {      value = defval;      if ( UserProperties.debug )        System.err.println( "UserProperties.getOverridableProperty: " +                            name + " defaulted to '" + value + "'" );    }    return value;  }  /**   * 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 public String  getProperty( String name, String defval ) {    String result = UserProperties.getOverridableProperty ( name, defval );    return result;  }  /**   * Retrieve a system integer 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 integer value.   * @return The integer value of the named property.   */  static public int  getProperty( String name, int defval ) {    int result = defval;    String val = UserProperties.getProperty( name, null );    if ( val != null ) {      try {        result = Integer.parseInt( val );      } catch ( NumberFormatException ex ) {        result = defval;      }    }    return result;  }  /**   * Retrieve a system long 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 integer value.   * @return The integer value of the named property.   */  static public long  getProperty( String name, long defval ) {    long result = defval;    String val = UserProperties.getProperty( name, null );    if ( val != null ) {      try {        result = Long.parseLong( val );      } catch ( NumberFormatException ex ) {        result = defval;      }    }    return result;  }  /**   * Retrieve a system double 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 double value.   * @return The double value of the named property.   */  static public double  getProperty( String name, double defval ) {    double result = defval;    String val = UserProperties.getProperty( name, null );    if ( val != null ) {      try {        result = Double.valueOf( val ).doubleValue();      } catch ( NumberFormatException ex ) {        result = defval;      }    }    return result;  }  /**   * Retrieve a system boolean 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 boolean value.   * @return The boolean value of the named property.   */  static public boolean  getProperty( String name, boolean defval ) {    boolean result = defval;    String val = UserProperties.getProperty( name, null );    if ( val != null ) {      if ( val.equalsIgnoreCase( "T" ) || val.equalsIgnoreCase( "TRUE" ) ||           val.equalsIgnoreCase( "Y" ) || val.equalsIgnoreCase( "YES" ) )      {        result = true;      } else if ( val.equalsIgnoreCase( "F" ) || val.equalsIgnoreCase( "FALSE" ) ||                  val.equalsIgnoreCase( "N" ) || val.equalsIgnoreCase( "NO" ) ) {        result = false;      }    }    return result;  }  /**   * Retrieve a system string array property list. String   * arrays are represented by colon separated strings.   * Returns a provided default value if the property   * is not defined.   *   * @param name The name of the property to retrieve.   * @param defval A default boolean value.   * @return The string array value of the named property.   */  static public String[]  getStringArray( String name, String[] defval ) {    String[] result = defval;    String val = UserProperties.getProperty( name, null );    if ( val != null ) {      result = StringUtilities.splitString( val, ":" );    }    return result;  }  static public Vector  getStringVector( String name, Vector defval ) {    Vector result = defval;    String[] sa = UserProperties.getStringArray ( name, null );    if ( sa != null ) {      result = new Vector();      for ( int s = 0 ; s < sa.length ; ++s ) {        result.addElement( sa[s] );      }    }    return result;  }  /**   * Retrieve a system Class constant property.   *   * @param name The name of the property to retrieve.   * @param defval A default integer value.   * @return The integer value of the named property.   */  static public int  getClassConstant( String name, int defval ) {    int result = defval;    String val = UserProperties.getProperty( name, null );    if ( val != null ) {      int index = val.lastIndexOf( "." );      if ( index > 0 ) {        try {          String className = val.substring( 0, index );          String constName = val.substring( index + 1);          Class cls = Class.forName( className );          Field fld = cls.getField( constName );          result = fld.getInt( null );        } catch ( Exception ex ) {          result = defval;        }      }    }    return result;  }  /**   * Establishes critical default properties.   *   * @param props The system properties to add properties into.   */  static public void  defaultProperties( Properties props ) {    props.put( "org.icemail.util.UserProperties.revision", "$Revision: 1.4 $" );    //    // Define the following to create a global    // enterprise-wide defaults resource...    // e.g.    //    // props.put    //  ( UserProperties.DEFAULTS_RSRC_NAME,    //    "http://www.ice.com/properties/defaults.txt" );    //  }  static public void  includeProperties( Properties into, Properties from ) {    Enumeration enum = from.keys();    for ( ; enum.hasMoreElements() ; ) {      Object key = null;      try {        key = (String)enum.nextElement();      }        catch ( NoSuchElementException ex ) {        key = null;      }            if ( key != null ) {        into.put( key, from.get( key ) );      }    }  }  static public void  addDefaultProperties( Properties props, Properties defaultProps ) {    UserProperties.includeProperties( props, defaultProps );  }  /**   * Loads a properties stream into the System properties table.   *   * @param path The properties data's input stream.   * @param props The system properties to add properties into.   */  static private boolean  loadPropertiesStream( InputStream in, Properties props ) throws IOException {    props.load( in );    return true;  }  static private void  doLoadPropertiesFile( String path, Properties props, Properties loaded )    throws IOException {    FileInputStream in;    boolean         result = true;    try {      in = new FileInputStream( path );    } catch ( IOException ex ) {      throw new IOException( "opening property file '" + path + "' - " + ex.getMessage() );    }    try {      if ( loaded != null ) {        UserProperties.loadPropertiesStream( in, loaded );        UserProperties.includeProperties( props, loaded );      } else {        UserProperties.loadPropertiesStream( in, props );      }    } catch ( IOException ex ) {      throw new IOException( "loading property file '" + path + "' - " + ex.getMessage() );    }    try {     in.close();    } catch ( IOException ex ) {      throw new IOException( "closing property file '" + path + "' - " + ex.getMessage() );    }  }  /**   * Loads a named properties file into the System properties table.   *   * @param path The properties file's pathname.   * @param props The system properties to add properties into.   * @param loaded If not null, insert properties here before loading.   */  static private boolean  loadPropertiesFile( String path, Properties props, Properties loaded ) {    FileInputStream in;    boolean         result = true;    if ( UserProperties.debug )      System.err.println( "Loading property file '" + path + "'." );    try {      UserProperties.doLoadPropertiesFile( path, props, loaded );    } catch ( IOException ex ) {      System.err.println( "ERROR " + ex.getMessage() );      result = false;    }    if ( result )      System.err.println( "Loaded property file '" + path + "'." );    return result;  }  /**   * Loads a named properties file into the System properties table.   * This method fails <em>silently,</em>, as we do not care if the   * file is there, and it is a feature that the user can remove the   * file to <em>reset</em> their settings.   *   * @param path The properties file's pathname.   * @param props The system properties to add properties into.   */  static private void  loadDynamicProperties( String name, String path ) {    Properties  dynProps = new Properties();    Properties  sysProps = System.getProperties();    if ( UserProperties.debug )      System.err.println( "Loading  '" + name + "' protperties from '" + path + "'." );    try {      UserProperties.doLoadPropertiesFile( path, sysProps, dynProps );      UserProperties.addDynamicPropertyKeys( name, dynProps );      System.err.println( "Loaded '" + name + "' properties from '" + path + "'." );    } catch ( IOException ex ) {      // Silently fail on dynamic property files!    }  }  static private boolean  loadPropertiesResource( String name, Properties props ) {    InputStream in;    boolean     result = false;    if ( UserProperties.debug )      System.err.println( "Load properties resource '" + name + "'" );    try {      in = ResourceUtilities.openNamedResource( name );      UserProperties.loadPropertiesStream( in, props );      in.close();      result = true;    } catch ( java.io.IOException ex ) {      System.err.println( "ERROR loading properties resource '" +                          name + "' - " + ex.getMessage() );    }    return result;  }  private static void  loadPropertyResourceList( String listPropName, String rsrcPrefix, Properties props ) {    String rsrcListStr = UserProperties.getProperty( listPropName, null );    if ( rsrcListStr != null ) {      String[] rsrcList = StringUtilities.splitString( rsrcListStr, ":" );            for ( int rIdx = 0 ; rsrcList != null && rIdx < rsrcList.length ; ++rIdx ) {        String rsrcTag = rsrcPrefix + rsrcList[rIdx];

⌨️ 快捷键说明

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