]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/ping/PingerDaemon.java
fix: SQL change database call pattern
[gigi.git] / src / org / cacert / gigi / ping / PingerDaemon.java
index 7243a3ebde0a3785b6b497cfe260c55d81eda05b..4936bfc1c1a66e213cb56826ecede219d99c448e 100644 (file)
@@ -5,22 +5,19 @@ import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.Queue;
 
-import org.cacert.gigi.database.DatabaseConnection;
 import org.cacert.gigi.database.GigiPreparedStatement;
 import org.cacert.gigi.database.GigiResultSet;
 import org.cacert.gigi.dbObjects.Domain;
 import org.cacert.gigi.dbObjects.DomainPingConfiguration;
-import org.cacert.gigi.dbObjects.DomainPingConfiguration.PingType;
+import org.cacert.gigi.dbObjects.DomainPingType;
 import org.cacert.gigi.util.RandomToken;
 
 public class PingerDaemon extends Thread {
 
-    HashMap<PingType, DomainPinger> pingers = new HashMap<>();
+    HashMap<DomainPingType, DomainPinger> pingers = new HashMap<>();
 
     private GigiPreparedStatement searchNeededPings;
 
-    private GigiPreparedStatement enterPingResult;
-
     private KeyStore truststore;
 
     private Queue<DomainPingConfiguration> toExecute = new LinkedList<>();
@@ -31,12 +28,11 @@ public class PingerDaemon extends Thread {
 
     @Override
     public void run() {
-        searchNeededPings = DatabaseConnection.getInstance().prepare("SELECT `pingconfig`.`id` FROM `pingconfig` LEFT JOIN `domainPinglog` ON `domainPinglog`.`configId` = `pingconfig`.`id` INNER JOIN `domains` ON `domains`.`id` = `pingconfig`.`domainid` WHERE ( `domainPinglog`.`configId` IS NULL) AND `domains`.`deleted` IS NULL GROUP BY `pingconfig`.`id`");
-        enterPingResult = DatabaseConnection.getInstance().prepare("INSERT INTO `domainPinglog` SET `configId`=?, `state`=?::`pingState`, `result`=?, `challenge`=?");
-        pingers.put(PingType.EMAIL, new EmailPinger());
-        pingers.put(PingType.SSL, new SSLPinger(truststore));
-        pingers.put(PingType.HTTP, new HTTPFetch());
-        pingers.put(PingType.DNS, new DNSPinger());
+        searchNeededPings = new GigiPreparedStatement("SELECT `pingconfig`.`id` FROM `pingconfig` LEFT JOIN `domainPinglog` ON `domainPinglog`.`configId` = `pingconfig`.`id` INNER JOIN `domains` ON `domains`.`id` = `pingconfig`.`domainid` WHERE ( `domainPinglog`.`configId` IS NULL) AND `domains`.`deleted` IS NULL GROUP BY `pingconfig`.`id`");
+        pingers.put(DomainPingType.EMAIL, new EmailPinger());
+        pingers.put(DomainPingType.SSL, new SSLPinger(truststore));
+        pingers.put(DomainPingType.HTTP, new HTTPFetch());
+        pingers.put(DomainPingType.DNS, new DNSPinger());
 
         while (true) {
             try {
@@ -69,24 +65,24 @@ public class PingerDaemon extends Thread {
     }
 
     private void handle(DomainPingConfiguration conf) {
-        PingType type = conf.getType();
+        DomainPingType type = conf.getType();
         String config = conf.getInfo();
         DomainPinger dp = pingers.get(type);
         if (dp != null) {
-            String token = null;
             if (dp instanceof EmailPinger) {
+                String token = null;
                 token = RandomToken.generateToken(16);
                 config = config + ":" + token;
             }
-            enterPingResult.setInt(1, conf.getId());
             Domain target = conf.getTarget();
             System.err.println("Executing " + dp + " on " + target + " (" + System.currentTimeMillis() + ")");
-            String resp = dp.ping(target, config, target.getOwner());
+            try {
+                dp.ping(target, config, target.getOwner(), conf.getId());
+            } catch (Throwable t) {
+                t.printStackTrace();
+                DomainPinger.enterPingResult(conf.getId(), "error", "exception", null);
+            }
             System.err.println("done (" + System.currentTimeMillis() + ")");
-            enterPingResult.setString(2, DomainPinger.PING_STILL_PENDING == resp ? "open" : DomainPinger.PING_SUCCEDED.equals(resp) ? "success" : "failed");
-            enterPingResult.setString(3, resp);
-            enterPingResult.setString(4, token);
-            enterPingResult.execute();
         }
     }