1 package org.cacert.gigi.util;
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.dbObjects.Certificate;
10 import org.cacert.gigi.output.CertificateValiditySelector;
20 public static enum JobType {
21 SIGN("sign"), REVOKE("revoke");
23 private final String name;
25 private JobType(String name) {
29 public String getName() {
34 public static Job sign(Certificate targetId, Date start, String period) throws GigiApiException {
35 CertificateValiditySelector.checkValidityLength(period);
36 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `jobs` SET targetId=?, task=?, executeFrom=?, executeTo=?");
37 ps.setInt(1, targetId.getId());
38 ps.setString(2, JobType.SIGN.getName());
40 ps.setString(4, period);
42 return new Job(ps.lastInsertId());
45 public static Job revoke(Certificate targetId) {
47 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `jobs` SET targetId=?, task=?");
48 ps.setInt(1, targetId.getId());
49 ps.setString(2, JobType.REVOKE.getName());
51 return new Job(ps.lastInsertId());
54 public boolean waitFor(int max) throws InterruptedException {
55 long start = System.currentTimeMillis();
56 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `jobs` WHERE id=? AND state='open'");
58 GigiResultSet rs = ps.executeQuery();
61 if (max != 0 && System.currentTimeMillis() - start > max) {
64 Thread.sleep((long) (2000 + Math.random() * 2000));
65 rs = ps.executeQuery();