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.DatabaseConnection;
11 import org.cacert.gigi.database.GigiPreparedStatement;
12 import org.cacert.gigi.database.GigiResultSet;
13 import org.cacert.gigi.output.template.Scope;
14 import org.cacert.gigi.output.template.SprintfCommand;
16 public class DomainPingConfiguration implements IdCachable {
20 private Domain target;
22 private DomainPingType type;
26 private DomainPingConfiguration(int id) {
27 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `id`, `domainid`, `type`, `info` FROM `pingconfig` WHERE `id`=?");
30 GigiResultSet rs = ps.executeQuery();
32 throw new IllegalArgumentException("Invalid pingconfig id " + id);
34 this.id = rs.getInt("id");
35 target = Domain.getById(rs.getInt("domainid"));
36 type = DomainPingType.valueOf(rs.getString("type").toUpperCase());
37 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 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("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());
77 public Date getLastSuccess() {
78 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `when` AS stamp from `domainPinglog` WHERE `configId`=? AND state='success' ORDER BY `when` DESC LIMIT 1");
80 GigiResultSet rs = ps.executeQuery();
82 return new Date(rs.getTimestamp("stamp").getTime());
87 public synchronized void requestReping() throws GigiApiException {
88 Date lastExecution = getLastExecution();
89 if (lastExecution.getTime() + 5 * 60 * 1000 < System.currentTimeMillis()) {
90 Gigi.notifyPinger(this);
93 Map<String, Object> data = new HashMap<String, Object>();
94 data.put("data", new Date(lastExecution.getTime() + 5 * 60 * 1000));
95 throw new GigiApiException(new Scope(new SprintfCommand("Reping is only allowed after 5 minutes, yours end at {0}.", Arrays.asList("${data}")), data));