001: package CustomDNS.ZoneController;
002:
003: import java.sql.*;
004: import CustomDNS.Configuration;
005: import net.espeak.infra.cci.exception.*;
006: import net.espeak.jesi.*;
007:
008: import ZoneAuthorityIntf;
009: import ZoneRegistrarIntf;
010:
011: /*************************************************************************
012: * Implements our zone authority and zone registrar services.
013: *************************************************************************
014: * @see CustomDNS.ZoneContoller.Server
015: */
016:
017: public class Implementation implements ZoneAuthorityIntf,
018: ZoneRegistrarIntf {
019:
020: // Our database connection.
021: Connection database;
022:
023: // Our query strings.
024: // XXX - Fix 'now' constant to be portable SQL/JDBC.
025: static final String addressQuery = "select * from address_record where dnsname = ?";
026: static final String addressUpdate = "update address_record"
027: + " set address = ?, last_confirmed = 'now'"
028: + " where dnsname = ?";
029: static final String addressCreate = "insert into address_record values (?, ?, 'now')";
030:
031: /*********************************************************************
032: * Create a new zone controller.
033: *********************************************************************
034: * @param database A JDBC connection to the database holding our
035: * address records.
036: */
037:
038: public Implementation(Connection database) {
039: this .database = database;
040: }
041:
042: /*********************************************************************
043: * Look up the address of the specified host.
044: *********************************************************************
045: * This method is invoked remotely via e-speak.
046: * @param hostname The fully qualified domain name of the host.
047: * @return The IP address of the host (or null, if no address is
048: * found). This should be a string of the format "w.x.y.z".
049: * @exception ESInvocationException Thrown when an error is detected
050: * during remote invocation.
051: */
052:
053: public String getAddressForHost(String hostname)
054: throws ESInvocationException {
055: try {
056: // Query the database for our hostname.
057: PreparedStatement s = database
058: .prepareStatement(addressQuery);
059: s.setString(1, hostname);
060: s.executeQuery();
061:
062: // Get our result set.
063: ResultSet results = s.getResultSet();
064: if (results.next()) {
065: // Return the address column of the first record. (We only
066: // expect a single record, anyway.)
067: return results.getString(2);
068: } else {
069: // No match, so we return null.
070: return null;
071: }
072: } catch (Exception e) {
073: // XXX - For now, just pretend that the lookup failed.
074: System.err.println("CustomDNS.ZoneController: "
075: + e.toString());
076: return null;
077: }
078: }
079:
080: /*********************************************************************
081: * Register a new address for the specified host.
082: *********************************************************************
083: * This method is invoked remotely via e-speak.
084: * @param hostname The fully qualified domain name of the host.
085: * @param address IP address of the host. This should be a string of
086: * the format "w.x.y.z".
087: * @exception ESInvocationException Thrown when an error is detected
088: * during remote invocation.
089: */
090:
091: public void registerAddressForHost(String hostname, String address)
092: throws ESInvocationException {
093: try {
094: // XXX - AFAIK, The PostgreSQL JDBC driver is returning bogus
095: // values for the update count. We work around this by
096: // attempting to *create* the record first, and by falling
097: // back to regular update if an error occurs. Yuck.
098: try {
099: PreparedStatement s = database
100: .prepareStatement(addressCreate);
101: s.setString(1, hostname);
102: s.setString(2, address);
103: s.executeUpdate();
104: //System.err.println("Created record.");
105: } catch (SQLException e) {
106: PreparedStatement s = database
107: .prepareStatement(addressUpdate);
108: s.setString(1, address);
109: s.setString(2, hostname);
110: s.executeUpdate();
111: //System.err.println("Updated record.");
112: }
113: } catch (Exception e) {
114: // XXX - For now, just do nothing.
115: System.err.println("CustomDNS.ZoneController: "
116: + e.toString());
117: }
118: }
119:
120: }
|