01: package CustomDNS;
02:
03: import java.lang.Exception;
04: import java.util.Properties;
05: import java.util.Enumeration;
06: import java.io.*;
07:
08: /*************************************************************************
09: * A Property subclass which loads our configuration data for us.
10: *************************************************************************
11: * This is very generic and could be reused in just about any project.
12: */
13:
14: public class SimpleConfiguration extends Properties {
15:
16: /*********************************************************************
17: * Load our configuration data from the specified file.
18: *********************************************************************
19: * @param filename The property file with our configuration data.
20: * @exception FileNotFoundException The specified property file
21: * doesn't exist.
22: * @exception IOException An error occured while reading from the
23: * property file.
24: */
25:
26: public SimpleConfiguration(String filename)
27: throws FileNotFoundException, IOException {
28: super ();
29: File file = new File(filename);
30: InputStream input = new FileInputStream(file);
31: try {
32: // Call the load() function (defined by Properties).
33: load(input);
34: } finally {
35: input.close();
36: }
37: }
38:
39: /*********************************************************************
40: * A simple test program.
41: *********************************************************************
42: * Load and print a configuration file. If this works correctly, you
43: * can probably assume the entire class works.
44: *
45: * @param args Our command line arguments. Pass the filename of
46: * a regular Java properties file.
47: */
48:
49: public static void main(String[] args) {
50: try {
51: // Parse our command-line arguments.
52: if (args.length > 1) {
53: System.err.println("Usage: Server [propfile]");
54: }
55: String configfile = "customdns.prop";
56: if (args.length == 1) {
57: configfile = args[0];
58: }
59:
60: // Open and print our configuration file.
61: SimpleConfiguration config = new SimpleConfiguration(
62: configfile);
63: Enumeration propnames = config.propertyNames();
64: while (propnames.hasMoreElements()) {
65: String key = (String) propnames.nextElement();
66: System.out.println(key + "=" + config.getProperty(key));
67: }
68: } catch (Exception e) {
69: // If anything goes wrong, print an error message and exit.
70: System.err.println("CustomDNS.SimpleConfiguration: "
71: + e.toString());
72: System.exit(1);
73: }
74:
75: System.exit(0);
76: }
77:
78: }
|