001: package CustomDNS;
002:
003: import java.lang.Exception;
004: import java.io.*;
005:
006: import java.sql.DriverManager;
007: import java.sql.Connection;
008:
009: /*************************************************************************
010: * A configuration manager used by several of our applications.
011: *************************************************************************
012: * This class has been specialized for use with CustomDNS. It knows how to
013: * parse our command-line arguments and act on the information in our
014: * properties file.
015: */
016:
017: public class Configuration extends SimpleConfiguration {
018: // Our instance data.
019: String myConnectionFile;
020:
021: /*********************************************************************
022: * Parse our command line arguments and get our configuration.
023: *********************************************************************
024: * If something goes wrong, this function will print an error message
025: * on the application's behalf and exit promptly.
026: *
027: * @param appname The name of the application (used to print error
028: * messages).
029: * @param args Our command-line arguments. These should be an espeak
030: * connection properties file and customdns.prop.
031: * @return A Configuration object.
032: */
033:
034: public static Configuration parseArguments(String appname,
035: String[] args) {
036: // Parse our command-line arguments.
037: if (args.length > 2 || args.length < 1) {
038: System.err.println("Usage: " + appname
039: + " espeak.prop [customdns.prop]");
040: System.exit(1);
041: }
042: String connectionFile = args[0];
043: String configFile = "customdns.prop";
044: if (args.length == 2) {
045: configFile = args[1];
046: }
047:
048: // Create a configuration object and return it.
049: Configuration config = null;
050: try {
051: config = new Configuration(connectionFile, configFile);
052: } catch (Exception e) {
053: System.err.println(appname + ":" + e.toString());
054: System.exit(1);
055: }
056: return config;
057: }
058:
059: /*********************************************************************
060: * Create a Configuration object from the supplied files.
061: *********************************************************************
062: * @param connectionFile The property file describing our e-speak
063: * connection.
064: * @param configFile The property file with our CustomDNS
065: * configuration.
066: * @exception FileNotFoundException The specified property file
067: * doesn't exist.
068: * @exception IOException An error occured while reading from the
069: * property file.
070: */
071:
072: public Configuration(String connectionFile, String configFile)
073: throws FileNotFoundException, IOException {
074: super (configFile);
075: myConnectionFile = connectionFile;
076: }
077:
078: /*********************************************************************
079: * Get the name of the file with our e-speak connection information.
080: *********************************************************************
081: * @return The name of the connection file we pass to ESConnection.
082: */
083:
084: public String getConnectionFile() {
085: return myConnectionFile;
086: }
087:
088: /*********************************************************************
089: * Create a JDBC Connection object.
090: *********************************************************************
091: * We use information from our configuration file.
092: * @return A JDBC Connection object.
093: */
094:
095: public Connection connectToDatabase()
096: throws java.lang.ClassNotFoundException,
097: java.sql.SQLException {
098: // Connect to our database using JDBC.
099: Class.forName(this .getProperty("customdns.db.jdbc.driver"));
100: String url = this .getProperty("customdns.db.jdbc.url");
101: String user = this .getProperty("customdns.db.user");
102: String pass = this .getProperty("customdns.db.password");
103: return DriverManager.getConnection(url, user, pass);
104: }
105: }
|