1 package org.cacert.gigi.dbObjects;
5 import org.cacert.gigi.Gigi;
6 import org.cacert.gigi.GigiApiException;
7 import org.cacert.gigi.database.DatabaseConnection;
8 import org.cacert.gigi.database.GigiPreparedStatement;
9 import org.cacert.gigi.database.GigiResultSet;
11 public class DomainPingConfiguration implements IdCachable {
13 public static enum PingType {
14 EMAIL, DNS, HTTP, SSL;
19 private Domain target;
21 private PingType type;
25 private DomainPingConfiguration(int id) {
26 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("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 = PingType.valueOf(rs.getString("type").toUpperCase());
36 info = rs.getString("info");
44 public Domain getTarget() {
48 public PingType getType() {
52 public String getInfo() {
56 private static ObjectCache<DomainPingConfiguration> cache = new ObjectCache<>();
58 public static synchronized DomainPingConfiguration getById(int id) {
59 DomainPingConfiguration res = cache.get(id);
61 cache.put(res = new DomainPingConfiguration(id));
66 public Date getLastExecution() {
67 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `when` AS stamp from domainPinglog WHERE configId=? ORDER BY `when` DESC LIMIT 1");
69 GigiResultSet rs = ps.executeQuery();
71 return new Date(rs.getTimestamp("stamp").getTime());
76 public Date getLastSuccess() {
77 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("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());
86 public synchronized void requestReping() throws GigiApiException {
87 Date lastExecution = getLastExecution();
88 if (lastExecution.getTime() + 5 * 60 * 1000 < System.currentTimeMillis()) {
89 Gigi.notifyPinger(this);
92 throw new GigiApiException("Reping is only allowed after 5 minutes");