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

📄 simpleparameters.java

📁 pastry的java实现的2.0b版
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*************************************************************************"FreePastry" Peer-to-Peer Application Development Substrate Copyright 2002, Rice University. All rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions aremet:- Redistributions of source code must retain the above copyrightnotice, this list of conditions and the following disclaimer.- Redistributions in binary form must reproduce the above copyrightnotice, this list of conditions and the following disclaimer in thedocumentation and/or other materials provided with the distribution.- Neither  the name  of Rice  University (RICE) nor  the names  of itscontributors may be  used to endorse or promote  products derived fromthis software without specific prior written permission.This software is provided by RICE and the contributors on an "as is"basis, without any representations or warranties of any kind, expressor implied including, but not limited to, representations orwarranties of non-infringement, merchantability or fitness for aparticular purpose. In no event shall RICE or contributors be liablefor any direct, indirect, incidental, special, exemplary, orconsequential damages (including, but not limited to, procurement ofsubstitute goods or services; loss of use, data, or profits; orbusiness interruption) however caused and on any theory of liability,whether in contract, strict liability, or tort (including negligenceor otherwise) arising in any way out of the use of this software, evenif advised of the possibility of such damage.********************************************************************************/package rice.environment.params.simple;import java.io.*;import java.net.*;import java.util.*;import rice.environment.params.ParameterChangeListener;import rice.environment.params.Parameters;/** * This class represents a generic Java process launching program which reads in * preferences from a preferences file and then invokes another JVM using those * prefs. If the launched JVM dies, this process can be configured to restart * the JVM any number of times before giving up. This process can also be * configured to launch the second JVM with a specified memory allocation, * etc... * * @version $Id: pretty.settings 2305 2005-03-11 20:22:33Z jeffh $ * @author Alan Mislove */public class SimpleParameters implements Parameters {  private MyProperties properties;  private MyProperties defaults;  private Set changeListeners;  /**   * Where items are written out.   */  private String configFileName;  private static String FILENAME_EXTENSION = ".params";  private static String ARRAY_SPACER = ",";  /**   * @param orderedDefaults   * @param mutableConfigFileName if this is null, no params are saved, if this   *      file doesn't exist, you will get a warning printed to stdErr, then the   *      file will be created if you ever store   * @throws IOException   */  public SimpleParameters(String[] orderedDefaults, String mutableConfigFileName) {    if (mutableConfigFileName != null) {      this.configFileName = mutableConfigFileName + FILENAME_EXTENSION;    }    this.properties = new MyProperties();    this.defaults = new MyProperties();    this.changeListeners = new HashSet();    for (int ctr = 0; ctr < orderedDefaults.length; ctr++) {      try {        ClassLoader loader = this.getClass().getClassLoader();        // some VMs report the bootstrap classloader via null-return        if (loader == null) {          loader = ClassLoader.getSystemClassLoader();        }        this.defaults.load(loader.getResource(          orderedDefaults[ctr] + FILENAME_EXTENSION).openStream());      } catch (Exception ioe) {        String errorString = "Warning, couldn't load param file:"          + (orderedDefaults[ctr] + FILENAME_EXTENSION);//        System.err.println(errorString);//        ioe.printStackTrace(System.err);        throw new ParamsNotPresentException(errorString, ioe);      }    }    if (this.configFileName != null) {      File f = new File(this.configFileName);      if (f.exists()) {        try {          properties.load(new FileInputStream(this.configFileName));        } catch (Exception e) {          throw new ParamsNotPresentException("Error loading " + f, e);        }      } else {        System.err.println("Configuration file " + f.getAbsolutePath()          + " not present.  Using defaults.");      }    }  }  /**   * Gets the Property attribute of the SimpleParameters object   *   * @param name DESCRIBE THE PARAMETER   * @return The Property value   */  protected String getProperty(String name) {    String result = properties.getProperty(name);    if (result == null) {      result = defaults.getProperty(name);    }    if (result == null) {      System.err.println("WARNING: The parameter '" + name        + "' was not found - this is likely going to cause an error.");      //You " +      //                     "can fix this by adding this parameter (and appropriate value) to the      // proxy.params file in your ePOST " +      //                     "directory.");    } else {      // remove any surrounding whitespace      result = result.trim();    }    return result;  }  /**   * Gets the Int attribute of the SimpleParameters object   *   * @param name DESCRIBE THE PARAMETER   * @return The Int value   */  public int getInt(String name) {    try {      return Integer.parseInt(getProperty(name));    } catch (NumberFormatException nfe) {      throw new NumberFormatException(nfe.getMessage() + " for parameter "        + name);    }  }  /**   * Gets the Double attribute of the SimpleParameters object   *   * @param name DESCRIBE THE PARAMETER   * @return The Double value   */  public double getDouble(String name) {    try {      return Double.parseDouble(getProperty(name));    } catch (NumberFormatException nfe) {      throw new NumberFormatException(nfe.getMessage() + " for parameter "        + name);    }  }  /**   * Gets the Float attribute of the SimpleParameters object   *   * @param name DESCRIBE THE PARAMETER   * @return The Float value   */  public float getFloat(String name) {    try {      return Float.parseFloat(getProperty(name));    } catch (NumberFormatException nfe) {      throw new NumberFormatException(nfe.getMessage() + " for parameter "        + name);    }  }  /**   * Gets the Long attribute of the SimpleParameters object   *   * @param name DESCRIBE THE PARAMETER   * @return The Long value   */  public long getLong(String name) {    try {      return Long.parseLong(getProperty(name));    } catch (NumberFormatException nfe) {      throw new NumberFormatException(nfe.getMessage() + " for parameter "        + name);    }  }  /**   * Gets the Boolean attribute of the SimpleParameters object   *   * @param name DESCRIBE THE PARAMETER   * @return The Boolean value   */  public boolean getBoolean(String name) {    return (new Boolean(getProperty(name))).booleanValue();  }  /**   * Gets the InetAddress attribute of the SimpleParameters object   *   * @param name DESCRIBE THE PARAMETER   * @return The InetAddress value   * @exception UnknownHostException DESCRIBE THE EXCEPTION   */  public InetAddress getInetAddress(String name) throws UnknownHostException {    return InetAddress.getByName(getString(name));  }  /**   * Gets the InetSocketAddress attribute of the SimpleParameters object   *   * @param name DESCRIBE THE PARAMETER   * @return The InetSocketAddress value   * @exception UnknownHostException DESCRIBE THE EXCEPTION   */  public InetSocketAddress getInetSocketAddress(String name)     throws UnknownHostException {    return parseInetSocketAddress(getString(name));  }  /**   * Gets the InetSocketAddressArray attribute of the SimpleParameters object   *   * @param name DESCRIBE THE PARAMETER   * @return The InetSocketAddressArray value   * @exception UnknownHostException DESCRIBE THE EXCEPTION   */  public InetSocketAddress[] getInetSocketAddressArray(String name)     throws UnknownHostException {    String[] addresses = getString(name).split(ARRAY_SPACER);    List result = new LinkedList();    for (int i = 0; i < addresses.length; i++) {      InetSocketAddress address = parseInetSocketAddress(addresses[i]);      if (address != null) {        result.add(address);      }    }    return (InetSocketAddress[]) result.toArray(new InetSocketAddress[0]);  }  /**   * Gets the String attribute of the SimpleParameters object   *   * @param name DESCRIBE THE PARAMETER   * @return The String value   */  public String getString(String name) {    return getProperty(name);  }  /**   * Gets the StringArray attribute of the SimpleParameters object   *   * @param name DESCRIBE THE PARAMETER   * @return The StringArray value   */  public String[] getStringArray(String name) {    String list = getProperty(name);    if (list != null) {      return (list.equals("") ? new String[0] : list.split(ARRAY_SPACER));    } else {      return null;    }  }

⌨️ 快捷键说明

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