]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/DomainPingConfiguration.java
66805abeceacceceafb12b8ab7c55285184a5844
[gigi.git] / src / org / cacert / gigi / dbObjects / DomainPingConfiguration.java
1 package org.cacert.gigi.dbObjects;
2
3 import java.sql.PreparedStatement;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
6
7 import org.cacert.gigi.database.DatabaseConnection;
8
9 public class DomainPingConfiguration implements IdCachable {
10
11     public static enum PingType {
12         EMAIL, DNS, HTTP, SSL;
13     }
14
15     private int id;
16
17     private Domain target;
18
19     private PingType type;
20
21     private String info;
22
23     private DomainPingConfiguration(int id) throws SQLException {
24         PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id, domainid, type, info FROM pingconfig WHERE id=?");
25         ps.setInt(1, id);
26
27         ResultSet rs = ps.executeQuery();
28         if ( !rs.next()) {
29             throw new IllegalArgumentException("Invalid pingconfig id " + id);
30         }
31         this.id = rs.getInt("id");
32         target = Domain.getById(rs.getInt("domainid"));
33         type = PingType.valueOf(rs.getString("type").toUpperCase());
34         info = rs.getString("info");
35     }
36
37     @Override
38     public int getId() {
39         return id;
40     }
41
42     public Domain getTarget() {
43         return target;
44     }
45
46     public PingType getType() {
47         return type;
48     }
49
50     public String getInfo() {
51         return info;
52     }
53
54     private static ObjectCache<DomainPingConfiguration> cache = new ObjectCache<>();
55
56     public static DomainPingConfiguration getById(int id) {
57         DomainPingConfiguration res = cache.get(id);
58         if (res == null) {
59             try {
60                 cache.put(res = new DomainPingConfiguration(id));
61             } catch (SQLException e) {
62                 throw new IllegalArgumentException(e);
63             }
64         }
65         return res;
66     }
67
68 }