]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/DomainPingConfiguration.java
145473fdb9a871c892f8f42ad87caa89a9c4ee5d
[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     private int id;
19
20     private Domain target;
21
22     private DomainPingType type;
23
24     private String info;
25
26     private DomainPingConfiguration(int id) {
27         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `id`, `domainid`, `type`, `info` FROM `pingconfig` WHERE `id`=?");
28         ps.setInt(1, id);
29
30         GigiResultSet rs = ps.executeQuery();
31         if ( !rs.next()) {
32             throw new IllegalArgumentException("Invalid pingconfig id " + id);
33         }
34         this.id = rs.getInt("id");
35         target = Domain.getById(rs.getInt("domainid"));
36         type = DomainPingType.valueOf(rs.getString("type").toUpperCase());
37         info = rs.getString("info");
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         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("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     public Date getLastSuccess() {
78         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `when` AS stamp from `domainPinglog` WHERE `configId`=? AND state='success' ORDER BY `when` DESC LIMIT 1");
79         ps.setInt(1, id);
80         GigiResultSet rs = ps.executeQuery();
81         if (rs.next()) {
82             return new Date(rs.getTimestamp("stamp").getTime());
83         }
84         return new Date(0);
85     }
86
87     public synchronized void requestReping() throws GigiApiException {
88         Date lastExecution = getLastExecution();
89         if (lastExecution.getTime() + 5 * 60 * 1000 < System.currentTimeMillis()) {
90             Gigi.notifyPinger(this);
91             return;
92         }
93         Map<String, Object> data = new HashMap<String, Object>();
94         data.put("data", new Date(lastExecution.getTime() + 5 * 60 * 1000));
95         throw new GigiApiException(new Scope(new SprintfCommand("Reping is only allowed after 5 minutes, yours end at {0}.", Arrays.asList("${data}")), data));
96     }
97 }