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 {
18 public static enum PingType {
19 EMAIL, DNS, HTTP, SSL;
24 private Domain target;
26 private PingType type;
30 private DomainPingConfiguration(int id) {
31 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id, domainid, type, info FROM pingconfig WHERE id=?");
34 GigiResultSet rs = ps.executeQuery();
36 throw new IllegalArgumentException("Invalid pingconfig id " + id);
38 this.id = rs.getInt("id");
39 target = Domain.getById(rs.getInt("domainid"));
40 type = PingType.valueOf(rs.getString("type").toUpperCase());
41 info = rs.getString("info");
49 public Domain getTarget() {
53 public PingType getType() {
57 public String getInfo() {
61 private static ObjectCache<DomainPingConfiguration> cache = new ObjectCache<>();
63 public static synchronized DomainPingConfiguration getById(int id) {
64 DomainPingConfiguration res = cache.get(id);
66 cache.put(res = new DomainPingConfiguration(id));
71 public Date getLastExecution() {
72 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `when` AS stamp from domainPinglog WHERE configId=? ORDER BY `when` DESC LIMIT 1");
74 GigiResultSet rs = ps.executeQuery();
76 return new Date(rs.getTimestamp("stamp").getTime());
81 public Date getLastSuccess() {
82 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `when` AS stamp from domainPinglog WHERE configId=? AND state='success' ORDER BY `when` DESC LIMIT 1");
84 GigiResultSet rs = ps.executeQuery();
86 return new Date(rs.getTimestamp("stamp").getTime());
91 public synchronized void requestReping() throws GigiApiException {
92 Date lastExecution = getLastExecution();
93 if (lastExecution.getTime() + 5 * 60 * 1000 < System.currentTimeMillis()) {
94 Gigi.notifyPinger(this);
97 Map<String, Object> data = new HashMap<String, Object>();
98 data.put("data", new Date(lastExecution.getTime() + 5 * 60 * 1000));
99 throw new GigiApiException(new Scope(new SprintfCommand("Reping is only allowed after 5 minutes, yours end at {0}.", Arrays.asList("${data}")), data));