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