]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/DomainPingConfiguration.java
upd: factor out ping-delay for domains to constant
[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 static final int REPING_MINIMUM_DELAY = 5 * 60 * 1000;
18
19     private int id;
20
21     private Domain target;
22
23     private DomainPingType type;
24
25     private String info;
26
27     private DomainPingConfiguration(int id) {
28         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id`, `domainid`, `type`, `info` FROM `pingconfig` WHERE `id`=?")) {
29             ps.setInt(1, id);
30
31             GigiResultSet rs = ps.executeQuery();
32             if ( !rs.next()) {
33                 throw new IllegalArgumentException("Invalid pingconfig id " + id);
34             }
35             this.id = rs.getInt("id");
36             target = Domain.getById(rs.getInt("domainid"));
37             type = DomainPingType.valueOf(rs.getString("type").toUpperCase());
38             info = rs.getString("info");
39         }
40     }
41
42     @Override
43     public int getId() {
44         return id;
45     }
46
47     public Domain getTarget() {
48         return target;
49     }
50
51     public DomainPingType getType() {
52         return type;
53     }
54
55     public String getInfo() {
56         return info;
57     }
58
59     private static ObjectCache<DomainPingConfiguration> cache = new ObjectCache<>();
60
61     public static synchronized DomainPingConfiguration getById(int id) {
62         DomainPingConfiguration res = cache.get(id);
63         if (res == null) {
64             cache.put(res = new DomainPingConfiguration(id));
65         }
66         return res;
67     }
68
69     public Date getLastExecution() {
70         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `when` AS stamp from `domainPinglog` WHERE `configId`=? ORDER BY `when` DESC LIMIT 1")) {
71             ps.setInt(1, id);
72             GigiResultSet rs = ps.executeQuery();
73             if (rs.next()) {
74                 return new Date(rs.getTimestamp("stamp").getTime());
75             }
76             return new Date(0);
77         }
78     }
79
80     public Date getLastSuccess() {
81         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `when` AS stamp from `domainPinglog` WHERE `configId`=? AND state='success' ORDER BY `when` DESC LIMIT 1")) {
82             ps.setInt(1, id);
83             GigiResultSet rs = ps.executeQuery();
84             if (rs.next()) {
85                 return new Date(rs.getTimestamp("stamp").getTime());
86             }
87             return new Date(0);
88         }
89     }
90
91     public synchronized void requestReping() throws GigiApiException {
92         Date lastExecution = getLastExecution();
93         if (lastExecution.getTime() + REPING_MINIMUM_DELAY < System.currentTimeMillis()) {
94             Gigi.notifyPinger(this);
95             return;
96         }
97         Map<String, Object> data = new HashMap<String, Object>();
98         data.put("delay", REPING_MINIMUM_DELAY / 60 / 1000);
99         data.put("data", new Date(lastExecution.getTime() + REPING_MINIMUM_DELAY));
100         throw new GigiApiException(new Scope(new SprintfCommand("Reping is only allowed after {0} minutes, yours end at {1}.", Arrays.asList("${delay}", "${data}")), data));
101     }
102 }