]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/Job.java
bb357a8d07b742fddda040031abf4a8ca0dd8d83
[gigi.git] / src / org / cacert / gigi / dbObjects / Job.java
1 package org.cacert.gigi.dbObjects;
2
3 import java.sql.Date;
4
5 import org.cacert.gigi.GigiApiException;
6 import org.cacert.gigi.database.GigiPreparedStatement;
7 import org.cacert.gigi.database.GigiResultSet;
8 import org.cacert.gigi.output.CertificateValiditySelector;
9
10 public class Job implements IdCachable {
11
12     private int id;
13
14     private Job(int id) {
15         this.id = id;
16     }
17
18     public static enum JobType {
19         SIGN("sign"), REVOKE("revoke");
20
21         private final String name;
22
23         private JobType(String name) {
24             this.name = name;
25         }
26
27         public String getName() {
28             return name;
29         }
30     }
31
32     public synchronized static Job sign(Certificate targetId, Date start, String period) throws GigiApiException {
33         CertificateValiditySelector.checkValidityLength(period);
34         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `jobs` SET targetId=?, task=?::`jobType`, executeFrom=?, executeTo=?")) {
35             ps.setInt(1, targetId.getId());
36             ps.setString(2, JobType.SIGN.getName());
37             ps.setDate(3, start);
38             ps.setString(4, period);
39             ps.execute();
40             return cache.put(new Job(ps.lastInsertId()));
41         }
42     }
43
44     public synchronized static Job revoke(Certificate targetId) {
45
46         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `jobs` SET targetId=?, task=?::`jobType`")) {
47             ps.setInt(1, targetId.getId());
48             ps.setString(2, JobType.REVOKE.getName());
49             ps.execute();
50             return cache.put(new Job(ps.lastInsertId()));
51         }
52     }
53
54     public synchronized boolean waitFor(int max) throws InterruptedException {
55         long start = System.currentTimeMillis();
56         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `jobs` WHERE id=? AND state='open'")) {
57             ps.setInt(1, id);
58             GigiResultSet rs = ps.executeQuery();
59             while (rs.next()) {
60                 rs.close();
61                 if (max != 0 && System.currentTimeMillis() - start > max) {
62                     return false;
63                 }
64                 Thread.sleep((long) (2000 + Math.random() * 2000));
65                 rs = ps.executeQuery();
66             }
67         }
68         return true;
69     }
70
71     @Override
72     public int getId() {
73         return id;
74     }
75
76     static ObjectCache<Job> cache = new ObjectCache<>();
77
78     public synchronized static Job getById(int id) {
79         Job i = cache.get(id);
80         if (i != null) {
81             return i;
82         }
83         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `jobs` WHERE id=?'")) {
84             ps.setInt(1, id);
85             GigiResultSet rs = ps.executeQuery();
86             if (rs.next()) {
87                 Job j = new Job(id);
88                 cache.put(j);
89                 return j;
90             }
91             return null;
92         }
93
94     }
95 }