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