1 package org.cacert.gigi.util;
4 import org.cacert.gigi.GigiApiException;
5 import org.cacert.gigi.database.DatabaseConnection;
6 import org.cacert.gigi.database.GigiPreparedStatement;
7 import org.cacert.gigi.database.GigiResultSet;
8 import org.cacert.gigi.dbObjects.Certificate;
9 import org.cacert.gigi.output.CertificateValiditySelector;
19 public static enum JobType {
20 SIGN("sign"), REVOKE("revoke");
22 private final String name;
24 private JobType(String name) {
28 public String getName() {
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=?, executeFrom=?, executeTo=?");
36 ps.setInt(1, targetId.getId());
37 ps.setString(2, JobType.SIGN.getName());
39 ps.setString(4, period);
41 return new Job(ps.lastInsertId());
44 public static Job revoke(Certificate targetId) {
46 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `jobs` SET targetId=?, task=?");
47 ps.setInt(1, targetId.getId());
48 ps.setString(2, JobType.REVOKE.getName());
50 return new Job(ps.lastInsertId());
53 public 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'");
57 GigiResultSet rs = ps.executeQuery();
60 if (max != 0 && System.currentTimeMillis() - start > max) {
63 Thread.sleep((long) (2000 + Math.random() * 2000));
64 rs = ps.executeQuery();