1 package org.cacert.gigi.dbObjects;
5 import org.cacert.gigi.Gigi;
6 import org.cacert.gigi.GigiApiException;
7 import org.cacert.gigi.database.GigiPreparedStatement;
8 import org.cacert.gigi.database.GigiResultSet;
9 import org.cacert.gigi.output.template.SprintfCommand;
11 public class DomainPingConfiguration implements IdCachable {
13 private static final int REPING_MINIMUM_DELAY = 5 * 60 * 1000;
17 private Domain target;
19 private DomainPingType type;
23 private DomainPingConfiguration(int id) {
24 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id`, `domainid`, `type`, `info` FROM `pingconfig` WHERE `id`=?")) {
27 GigiResultSet rs = ps.executeQuery();
29 throw new IllegalArgumentException("Invalid pingconfig id " + id);
31 this.id = rs.getInt("id");
32 target = Domain.getById(rs.getInt("domainid"));
33 type = DomainPingType.valueOf(rs.getString("type").toUpperCase());
34 info = rs.getString("info");
43 public Domain getTarget() {
47 public DomainPingType getType() {
51 public String getInfo() {
55 private static ObjectCache<DomainPingConfiguration> cache = new ObjectCache<>();
57 public static synchronized DomainPingConfiguration getById(int id) {
58 DomainPingConfiguration res = cache.get(id);
60 cache.put(res = new DomainPingConfiguration(id));
65 public Date getLastExecution() {
66 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `when` AS stamp from `domainPinglog` WHERE `configId`=? ORDER BY `when` DESC LIMIT 1")) {
68 GigiResultSet rs = ps.executeQuery();
70 return new Date(rs.getTimestamp("stamp").getTime());
76 public Date getLastSuccess() {
77 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `when` AS stamp from `domainPinglog` WHERE `configId`=? AND state='success' ORDER BY `when` DESC LIMIT 1")) {
79 GigiResultSet rs = ps.executeQuery();
81 return new Date(rs.getTimestamp("stamp").getTime());
87 public synchronized void requestReping() throws GigiApiException {
88 Date lastExecution = getLastExecution();
89 if (lastExecution.getTime() + REPING_MINIMUM_DELAY < System.currentTimeMillis()) {
90 Gigi.notifyPinger(this);
93 throw new GigiApiException(SprintfCommand.createSimple("Reping is only allowed after {0} minutes, yours end at {1}.", REPING_MINIMUM_DELAY / 60 / 1000, new Date(lastExecution.getTime() + REPING_MINIMUM_DELAY)));