X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2FdbObjects%2FDomainPingConfiguration.java;h=baa7b7b00b69c1affad4e21391f67e425f08b01a;hb=3c851b6824c0f54c9082640c4f83e453bbecbb6a;hp=66805abeceacceceafb12b8ab7c55285184a5844;hpb=0fcce31dee5661974d5e8d19b33d1844c9651dca;p=gigi.git diff --git a/src/org/cacert/gigi/dbObjects/DomainPingConfiguration.java b/src/org/cacert/gigi/dbObjects/DomainPingConfiguration.java index 66805abe..baa7b7b0 100644 --- a/src/org/cacert/gigi/dbObjects/DomainPingConfiguration.java +++ b/src/org/cacert/gigi/dbObjects/DomainPingConfiguration.java @@ -1,37 +1,40 @@ package org.cacert.gigi.dbObjects; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; - -import org.cacert.gigi.database.DatabaseConnection; +import java.util.Arrays; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import org.cacert.gigi.Gigi; +import org.cacert.gigi.GigiApiException; +import org.cacert.gigi.database.GigiPreparedStatement; +import org.cacert.gigi.database.GigiResultSet; +import org.cacert.gigi.output.template.Scope; +import org.cacert.gigi.output.template.SprintfCommand; public class DomainPingConfiguration implements IdCachable { - public static enum PingType { - EMAIL, DNS, HTTP, SSL; - } - private int id; private Domain target; - private PingType type; + private DomainPingType type; private String info; - private DomainPingConfiguration(int id) throws SQLException { - PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id, domainid, type, info FROM pingconfig WHERE id=?"); - ps.setInt(1, id); + private DomainPingConfiguration(int id) { + try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id`, `domainid`, `type`, `info` FROM `pingconfig` WHERE `id`=?")) { + ps.setInt(1, id); - ResultSet rs = ps.executeQuery(); - if ( !rs.next()) { - throw new IllegalArgumentException("Invalid pingconfig id " + id); + GigiResultSet rs = ps.executeQuery(); + if ( !rs.next()) { + throw new IllegalArgumentException("Invalid pingconfig id " + id); + } + this.id = rs.getInt("id"); + target = Domain.getById(rs.getInt("domainid")); + type = DomainPingType.valueOf(rs.getString("type").toUpperCase()); + info = rs.getString("info"); } - this.id = rs.getInt("id"); - target = Domain.getById(rs.getInt("domainid")); - type = PingType.valueOf(rs.getString("type").toUpperCase()); - info = rs.getString("info"); } @Override @@ -43,7 +46,7 @@ public class DomainPingConfiguration implements IdCachable { return target; } - public PingType getType() { + public DomainPingType getType() { return type; } @@ -53,16 +56,44 @@ public class DomainPingConfiguration implements IdCachable { private static ObjectCache cache = new ObjectCache<>(); - public static DomainPingConfiguration getById(int id) { + public static synchronized DomainPingConfiguration getById(int id) { DomainPingConfiguration res = cache.get(id); if (res == null) { - try { - cache.put(res = new DomainPingConfiguration(id)); - } catch (SQLException e) { - throw new IllegalArgumentException(e); - } + cache.put(res = new DomainPingConfiguration(id)); } return res; } + public Date getLastExecution() { + try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `when` AS stamp from `domainPinglog` WHERE `configId`=? ORDER BY `when` DESC LIMIT 1")) { + ps.setInt(1, id); + GigiResultSet rs = ps.executeQuery(); + if (rs.next()) { + return new Date(rs.getTimestamp("stamp").getTime()); + } + return new Date(0); + } + } + + public Date getLastSuccess() { + try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `when` AS stamp from `domainPinglog` WHERE `configId`=? AND state='success' ORDER BY `when` DESC LIMIT 1")) { + ps.setInt(1, id); + GigiResultSet rs = ps.executeQuery(); + if (rs.next()) { + return new Date(rs.getTimestamp("stamp").getTime()); + } + return new Date(0); + } + } + + public synchronized void requestReping() throws GigiApiException { + Date lastExecution = getLastExecution(); + if (lastExecution.getTime() + 5 * 60 * 1000 < System.currentTimeMillis()) { + Gigi.notifyPinger(this); + return; + } + Map data = new HashMap(); + data.put("data", new Date(lastExecution.getTime() + 5 * 60 * 1000)); + throw new GigiApiException(new Scope(new SprintfCommand("Reping is only allowed after 5 minutes, yours end at {0}.", Arrays.asList("${data}")), data)); + } }