1 package org.cacert.gigi.dbObjects;
3 import java.util.Arrays;
5 import java.util.HashMap;
8 import org.cacert.gigi.Gigi;
9 import org.cacert.gigi.GigiApiException;
10 import org.cacert.gigi.database.GigiPreparedStatement;
11 import org.cacert.gigi.database.GigiResultSet;
12 import org.cacert.gigi.output.template.Scope;
13 import org.cacert.gigi.output.template.SprintfCommand;
15 public class DomainPingConfiguration implements IdCachable {
19 private Domain target;
21 private DomainPingType type;
25 private DomainPingConfiguration(int id) {
26 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id`, `domainid`, `type`, `info` FROM `pingconfig` WHERE `id`=?")) {
29 GigiResultSet rs = ps.executeQuery();
31 throw new IllegalArgumentException("Invalid pingconfig id " + id);
33 this.id = rs.getInt("id");
34 target = Domain.getById(rs.getInt("domainid"));
35 type = DomainPingType.valueOf(rs.getString("type").toUpperCase());
36 info = rs.getString("info");
45 public Domain getTarget() {
49 public DomainPingType getType() {
53 public String getInfo() {
57 private static ObjectCache<DomainPingConfiguration> cache = new ObjectCache<>();
59 public static synchronized DomainPingConfiguration getById(int id) {
60 DomainPingConfiguration res = cache.get(id);
62 cache.put(res = new DomainPingConfiguration(id));
67 public Date getLastExecution() {
68 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `when` AS stamp from `domainPinglog` WHERE `configId`=? ORDER BY `when` DESC LIMIT 1")) {
70 GigiResultSet rs = ps.executeQuery();
72 return new Date(rs.getTimestamp("stamp").getTime());
78 public Date getLastSuccess() {
79 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `when` AS stamp from `domainPinglog` WHERE `configId`=? AND state='success' ORDER BY `when` DESC LIMIT 1")) {
81 GigiResultSet rs = ps.executeQuery();
83 return new Date(rs.getTimestamp("stamp").getTime());
89 public synchronized void requestReping() throws GigiApiException {
90 Date lastExecution = getLastExecution();
91 if (lastExecution.getTime() + 5 * 60 * 1000 < System.currentTimeMillis()) {
92 Gigi.notifyPinger(this);
95 Map<String, Object> data = new HashMap<String, Object>();
96 data.put("data", new Date(lastExecution.getTime() + 5 * 60 * 1000));
97 throw new GigiApiException(new Scope(new SprintfCommand("Reping is only allowed after 5 minutes, yours end at {0}.", Arrays.asList("${data}")), data));