package edu.hawaii.ics.yucheng; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Properties; /** * An enum that defines the types of different queries supported by this * program. */ enum ConfigurationType { createOrDrop, loadCSV, selectOrJoin; } /** * An object that contains the shared portion of different configuration files. * * @author Cheng Jade * @assignment ICS 421 Project * @date Mar 22, 2010 * @bugs None */ public class SharedConfiguration { // The catalog node configuration public final ConfigurationNode catalog; public final Properties properties; public ConfigurationType type; /** * Initialize a new instance of this object. */ public SharedConfiguration(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. this.properties = getProperties(path); this.catalog = new ConfigurationNode(this.properties, "catalog"); if (this.properties.getProperty("tablename") != null) { this.type = ConfigurationType.loadCSV; } else if (this.properties.getProperty("numnodes") != null) { this.type = ConfigurationType.createOrDrop; } else this.type = ConfigurationType.selectOrJoin; } /** * returns a runnable that executes a given list of SQL statements (select * or join statements) or load a given list of CSV entries. * * @param sqlsOrCSV * The given list of SQL statements or CSV entries. * * @return an object that implements Runnable. */ public Runnable getRunnable(final ArrayList<String> sqlsOrCSV) throws ProgramException { if (this.type == ConfigurationType.loadCSV) { final LoadCSVConfiguration config = new LoadCSVConfiguration(this.properties, this.catalog); return new LoadCSV(config, sqlsOrCSV); } if (this.type == ConfigurationType.createOrDrop) { final CreateOrDropConfiguration config = new CreateOrDropConfiguration(this.properties, this.catalog); return new CreateOrDrop(config, sqlsOrCSV); } final SelectOrJoinConfiguration config = new SelectOrJoinConfiguration(this.properties, this.catalog); return new SelectOrJoin(config, sqlsOrCSV); } /** * 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); } } /** * a test main */ public static void main(final String[] args) { assert null != args; // Print usage information, if wrong number of arguments were used. if (args.length != 1) { final String name = SharedConfiguration.class.getSimpleName(); System.err.println("Usage: java " + name + " <path>"); System.err.println(" <path> path to a configuration file"); System.exit(1); return; } try { // final SharedConfiguration config = new // SharedConfiguration(args[0]); // System.out.println(config.getConfiguration()); } catch (final Exception e) { System.err.println("faile to parse due to: " + e.getMessage()); System.exit(1); } } }