]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/DomainPingConfiguration.java
Fix: followups on sprintf syntax
[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.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;
15
16 public class DomainPingConfiguration implements IdCachable {
17
18     public static enum PingType {
19         EMAIL, DNS, HTTP, SSL;
20     }
21
22     private int id;
23
24     private Domain target;
25
26     private PingType type;
27
28     private String info;
29
30     private DomainPingConfiguration(int id) {
31         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id, domainid, type, info FROM pingconfig WHERE id=?");
32         ps.setInt(1, id);
33
34         GigiResultSet rs = ps.executeQuery();
35         if ( !rs.next()) {
36             throw new IllegalArgumentException("Invalid pingconfig id " + id);
37         }
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");
42     }
43
44     @Override
45     public int getId() {
46         return id;
47     }
48
49     public Domain getTarget() {
50         return target;
51     }
52
53     public PingType getType() {
54         return type;
55     }
56
57     public String getInfo() {
58         return info;
59     }
60
61     private static ObjectCache<DomainPingConfiguration> cache = new ObjectCache<>();
62
63     public static synchronized DomainPingConfiguration getById(int id) {
64         DomainPingConfiguration res = cache.get(id);
65         if (res == null) {
66             cache.put(res = new DomainPingConfiguration(id));
67         }
68         return res;
69     }
70
71     public Date getLastExecution() {
72         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `when` AS stamp from domainPinglog WHERE configId=? ORDER BY `when` DESC LIMIT 1");
73         ps.setInt(1, id);
74         GigiResultSet rs = ps.executeQuery();
75         if (rs.next()) {
76             return new Date(rs.getTimestamp("stamp").getTime());
77         }
78         return new Date(0);
79     }
80
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");
83         ps.setInt(1, id);
84         GigiResultSet rs = ps.executeQuery();
85         if (rs.next()) {
86             return new Date(rs.getTimestamp("stamp").getTime());
87         }
88         return new Date(0);
89     }
90
91     public synchronized void requestReping() throws GigiApiException {
92         Date lastExecution = getLastExecution();
93         if (lastExecution.getTime() + 5 * 60 * 1000 < System.currentTimeMillis()) {
94             Gigi.notifyPinger(this);
95             return;
96         }
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));
100     }
101 }