]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/Job.java
chg: Avoid dirtying the environment with Thread Interruption exceptions if nobody...
[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) {
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                 try {
65                     this.wait((long) (2000 + Math.random() * 2000));
66                 } catch (InterruptedException ie) {
67                     // Ignore the interruption
68                     ie.printStackTrace();
69                 }
70                 rs = ps.executeQuery();
71             }
72         }
73         return true;
74     }
75
76     @Override
77     public int getId() {
78         return id;
79     }
80
81     static ObjectCache<Job> cache = new ObjectCache<>();
82
83     public synchronized static Job getById(int id) {
84         Job i = cache.get(id);
85         if (i != null) {
86             return i;
87         }
88         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `jobs` WHERE id=?'")) {
89             ps.setInt(1, id);
90             GigiResultSet rs = ps.executeQuery();
91             if (rs.next()) {
92                 Job j = new Job(id);
93                 cache.put(j);
94                 return j;
95             }
96             return null;
97         }
98
99     }
100 }