1 package org.cacert.gigi.util;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
8 import org.cacert.gigi.Certificate;
9 import org.cacert.gigi.GigiApiException;
10 import org.cacert.gigi.database.DatabaseConnection;
11 import org.cacert.gigi.output.CertificateValiditySelector;
21 public static enum JobType {
22 SIGN("sign"), REVOKE("revoke");
24 private final String name;
26 private JobType(String name) {
30 public String getName() {
35 public static Job sign(Certificate targetId, Date start, String period) throws SQLException, GigiApiException {
36 CertificateValiditySelector.checkValidityLength(period);
37 PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `jobs` SET targetId=?, task=?, executeFrom=?, executeTo=?");
38 ps.setInt(1, targetId.getId());
39 ps.setString(2, JobType.SIGN.getName());
41 ps.setString(4, period);
43 return new Job(DatabaseConnection.lastInsertId(ps));
46 public static Job revoke(Certificate targetId) throws SQLException {
48 PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `jobs` SET targetId=?, task=?");
49 ps.setInt(1, targetId.getId());
50 ps.setString(2, JobType.REVOKE.getName());
52 return new Job(DatabaseConnection.lastInsertId(ps));
55 public boolean waitFor(int max) throws SQLException, InterruptedException {
56 long start = System.currentTimeMillis();
57 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `jobs` WHERE id=? AND state='open'");
59 ResultSet rs = ps.executeQuery();
62 if (max != 0 && System.currentTimeMillis() - start > max) {
65 Thread.sleep((long) (2000 + Math.random() * 2000));
66 rs = ps.executeQuery();