package edu.hawaii.ics.yucheng; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; /** * An immutable class that contains the configuration information for a cluster * of nodes and a catalog. They are parsed from the 'clustercfg' file. * * @author Cheng Jade * @assignment ICS 421 Assignment 2-2 * @date Mar 19, 2010 * @bugs None */ public class Configuration { // The catalog node configuration public final ConfigurationNode catalog; // The local node configuration public final ConfigurationNode localNode; public Configuration(final String path) throws ProgramException { // Throw an exception if the specified path is null. if (null == path) throw new NullPointerException("path"); // Declare a Properties object and load it with the specified file. final Properties properties = getProperties(path); this.catalog = new ConfigurationNode(properties, "catalog"); ConfigurationNode tryLocalNode; try { tryLocalNode = new ConfigurationNode(properties, "localnode"); } catch (ProgramException e) { tryLocalNode = new ConfigurationNode(properties, "catalog"); } this.localNode = tryLocalNode; } /** * Open a file, check for errors, and return a loaded properties object. * * @param path * The path to the properties file. * * @return A loaded properties object. * * @throws ProgramException * Thrown if there is a problem reading the configuration file. */ private static Properties getProperties(final String path) throws ProgramException { assert null != path; try { final FileInputStream file = new FileInputStream(path); try { final Properties properties = new Properties(); properties.load(file); return properties; } finally { file.close(); } } catch (final IOException e) { throw new ProgramException("Cannot read configuration file.", e); } } }