]> WPIA git - gigi.git/blob - src/club/wpia/gigi/dbObjects/Job.java
Merge changes If5eed01f,I88c94e39,If36f5b0a
[gigi.git] / src / club / wpia / gigi / dbObjects / Job.java
1 package club.wpia.gigi.dbObjects;
2
3 import java.sql.Date;
4
5 import club.wpia.gigi.GigiApiException;
6 import club.wpia.gigi.database.DBEnum;
7 import club.wpia.gigi.database.GigiPreparedStatement;
8 import club.wpia.gigi.database.GigiResultSet;
9 import club.wpia.gigi.dbObjects.Certificate.RevocationType;
10 import club.wpia.gigi.output.CertificateValiditySelector;
11
12 public class Job implements IdCachable {
13
14     public static int WAIT_MIN = 60000;
15
16     private int id;
17
18     private Job(int id) {
19         this.id = id;
20     }
21
22     public static enum JobType implements DBEnum {
23         SIGN("sign"), REVOKE("revoke");
24
25         private final String name;
26
27         private JobType(String name) {
28             this.name = name;
29         }
30
31         @Override
32         public String getDBName() {
33             return name;
34         }
35     }
36
37     protected synchronized static Job sign(Certificate targetId, Date start, String period) throws GigiApiException {
38         CertificateValiditySelector.checkValidityLength(period);
39         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `jobs` SET targetId=?, task=?::`jobType`, executeFrom=?, executeTo=?")) {
40             ps.setInt(1, targetId.getId());
41             ps.setEnum(2, JobType.SIGN);
42             ps.setDate(3, start);
43             ps.setString(4, period);
44             ps.execute();
45             return cache.put(new Job(ps.lastInsertId()));
46         }
47     }
48
49     protected synchronized static Job revoke(Certificate targetId, RevocationType type) {
50         return revoke(targetId, type, null, null, null);
51     }
52
53     protected synchronized static Job revoke(Certificate targetId, String challenge, String signature, String message) {
54         return revoke(targetId, RevocationType.KEY_COMPROMISE, challenge, signature, message);
55     }
56
57     private synchronized static Job revoke(Certificate targetId, RevocationType type, String challenge, String signature, String message) {
58         try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `certs` SET `revocationType`=?::`revocationType`, `revocationChallenge`=?, `revocationSignature`=?, `revocationMessage`=? WHERE id=?")) {
59             ps.setEnum(1, type);
60             ps.setString(2, challenge);
61             ps.setString(3, signature);
62             ps.setString(4, message);
63             ps.setInt(5, targetId.getId());
64             ps.execute();
65         }
66
67         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `jobs` SET targetId=?, task=?::`jobType`")) {
68             ps.setInt(1, targetId.getId());
69             ps.setEnum(2, JobType.REVOKE);
70             ps.execute();
71             return cache.put(new Job(ps.lastInsertId()));
72         }
73     }
74
75     public synchronized boolean waitFor(int max) {
76         long start = System.currentTimeMillis();
77         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `jobs` WHERE id=? AND state='open'")) {
78             ps.setInt(1, id);
79             GigiResultSet rs = ps.executeQuery();
80             while (rs.next()) {
81                 rs.close();
82                 if (max != 0 && System.currentTimeMillis() - start > max) {
83                     return false;
84                 }
85                 try {
86                     this.wait((long) (2000 + Math.random() * 2000));
87                 } catch (InterruptedException ie) {
88                     // Ignore the interruption
89                     ie.printStackTrace();
90                 }
91                 rs = ps.executeQuery();
92             }
93         }
94         return true;
95     }
96
97     @Override
98     public int getId() {
99         return id;
100     }
101
102     static ObjectCache<Job> cache = new ObjectCache<>();
103
104     public synchronized static Job getById(int id) {
105         Job i = cache.get(id);
106         if (i != null) {
107             return i;
108         }
109         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `jobs` WHERE id=?")) {
110             ps.setInt(1, id);
111             GigiResultSet rs = ps.executeQuery();
112             if (rs.next()) {
113                 Job j = new Job(id);
114                 cache.put(j);
115                 return j;
116             }
117             return null;
118         }
119
120     }
121 }