]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/DomainPingConfiguration.java
cf1292b8a072ffa744f86e7a203c7fdc00d81514
[gigi.git] / src / org / cacert / gigi / dbObjects / DomainPingConfiguration.java
1 package org.cacert.gigi.dbObjects;
2
3 import java.util.Date;
4
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;
10
11 public class DomainPingConfiguration implements IdCachable {
12
13     public static enum PingType {
14         EMAIL, DNS, HTTP, SSL;
15     }
16
17     private int id;
18
19     private Domain target;
20
21     private PingType type;
22
23     private String info;
24
25     private DomainPingConfiguration(int id) {
26         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("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 = PingType.valueOf(rs.getString("type").toUpperCase());
36         info = rs.getString("info");
37     }
38
39     @Override
40     public int getId() {
41         return id;
42     }
43
44     public Domain getTarget() {
45         return target;
46     }
47
48     public PingType getType() {
49         return type;
50     }
51
52     public String getInfo() {
53         return info;
54     }
55
56     private static ObjectCache<DomainPingConfiguration> cache = new ObjectCache<>();
57
58     public static synchronized DomainPingConfiguration getById(int id) {
59         DomainPingConfiguration res = cache.get(id);
60         if (res == null) {
61             cache.put(res = new DomainPingConfiguration(id));
62         }
63         return res;
64     }
65
66     public Date getLastExecution() {
67         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `when` AS stamp from domainPinglog WHERE configId=? ORDER BY `when` DESC LIMIT 1");
68         ps.setInt(1, id);
69         GigiResultSet rs = ps.executeQuery();
70         if (rs.next()) {
71             return new Date(rs.getTimestamp("stamp").getTime());
72         }
73         return new Date(0);
74     }
75
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");
78         ps.setInt(1, id);
79         GigiResultSet rs = ps.executeQuery();
80         if (rs.next()) {
81             return new Date(rs.getTimestamp("stamp").getTime());
82         }
83         return new Date(0);
84     }
85
86     public synchronized void requestReping() throws GigiApiException {
87         Date lastExecution = getLastExecution();
88         if (lastExecution.getTime() + 5 * 60 * 1000 < System.currentTimeMillis()) {
89             Gigi.notifyPinger(this);
90             return;
91         }
92         throw new GigiApiException("Reping is only allowed after 5 minutes");
93     }
94
95 }