]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/DomainPingConfiguration.java
baa7b7b00b69c1affad4e21391f67e425f08b01a
[gigi.git] / src / org / cacert / gigi / dbObjects / DomainPingConfiguration.java
1 package org.cacert.gigi.dbObjects;
2
3 import java.util.Arrays;
4 import java.util.Date;
5 import java.util.HashMap;
6 import java.util.Map;
7
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;
14
15 public class DomainPingConfiguration implements IdCachable {
16
17     private int id;
18
19     private Domain target;
20
21     private DomainPingType type;
22
23     private String info;
24
25     private DomainPingConfiguration(int id) {
26         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id`, `domainid`, `type`, `info` FROM `pingconfig` WHERE `id`=?")) {
27             ps.setInt(1, id);
28
29             GigiResultSet rs = ps.executeQuery();
30             if ( !rs.next()) {
31                 throw new IllegalArgumentException("Invalid pingconfig id " + id);
32             }
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");
37         }
38     }
39
40     @Override
41     public int getId() {
42         return id;
43     }
44
45     public Domain getTarget() {
46         return target;
47     }
48
49     public DomainPingType getType() {
50         return type;
51     }
52
53     public String getInfo() {
54         return info;
55     }
56
57     private static ObjectCache<DomainPingConfiguration> cache = new ObjectCache<>();
58
59     public static synchronized DomainPingConfiguration getById(int id) {
60         DomainPingConfiguration res = cache.get(id);
61         if (res == null) {
62             cache.put(res = new DomainPingConfiguration(id));
63         }
64         return res;
65     }
66
67     public Date getLastExecution() {
68         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `when` AS stamp from `domainPinglog` WHERE `configId`=? ORDER BY `when` DESC LIMIT 1")) {
69             ps.setInt(1, id);
70             GigiResultSet rs = ps.executeQuery();
71             if (rs.next()) {
72                 return new Date(rs.getTimestamp("stamp").getTime());
73             }
74             return new Date(0);
75         }
76     }
77
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")) {
80             ps.setInt(1, id);
81             GigiResultSet rs = ps.executeQuery();
82             if (rs.next()) {
83                 return new Date(rs.getTimestamp("stamp").getTime());
84             }
85             return new Date(0);
86         }
87     }
88
89     public synchronized void requestReping() throws GigiApiException {
90         Date lastExecution = getLastExecution();
91         if (lastExecution.getTime() + 5 * 60 * 1000 < System.currentTimeMillis()) {
92             Gigi.notifyPinger(this);
93             return;
94         }
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));
98     }
99 }