X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Futil%2FJob.java;h=6e502afd6627f41e028f4eb6f5396f2f2b8e72e5;hb=736968682d49753f1cfb9cbf0970fe95435f0658;hp=70c9d569714cd091788cbea36f9d8b8bcabd0c08;hpb=b93a39014fe39b0b3527849acc0e57e5b7772f1b;p=gigi.git diff --git a/src/org/cacert/gigi/util/Job.java b/src/org/cacert/gigi/util/Job.java index 70c9d569..6e502afd 100644 --- a/src/org/cacert/gigi/util/Job.java +++ b/src/org/cacert/gigi/util/Job.java @@ -1,55 +1,71 @@ package org.cacert.gigi.util; +import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.cacert.gigi.Certificate; +import org.cacert.gigi.GigiApiException; import org.cacert.gigi.database.DatabaseConnection; +import org.cacert.gigi.output.CertificateValiditySelector; public class Job { - int id; - - private Job(int id) { - this.id = id; - } - - public static enum JobType { - SIGN("sign"), REVOKE("revoke"); - private final String name; - - private JobType(String name) { - this.name = name; - } - - public String getName() { - return name; - } - } - - public static Job submit(Certificate targetId, JobType type) throws SQLException { - PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `jobs` SET targetId=?, task=?"); - ps.setInt(1, targetId.getId()); - ps.setString(2, type.getName()); - ps.execute(); - return new Job(DatabaseConnection.lastInsertId(ps)); - } - - public boolean waitFor(int max) throws SQLException, InterruptedException { - long start = System.currentTimeMillis(); - PreparedStatement ps = DatabaseConnection.getInstance().prepare( - "SELECT 1 FROM `jobs` WHERE id=? AND state='open'"); - ps.setInt(1, id); - ResultSet rs = ps.executeQuery(); - while (rs.next()) { - rs.close(); - if (max != 0 && System.currentTimeMillis() - start > max) { - return false; - } - Thread.sleep((long) (2000 + Math.random() * 2000)); - rs = ps.executeQuery(); - } - rs.close(); - return true; - } + + private int id; + + private Job(int id) { + this.id = id; + } + + public static enum JobType { + SIGN("sign"), REVOKE("revoke"); + + private final String name; + + private JobType(String name) { + this.name = name; + } + + public String getName() { + return name; + } + } + + public static Job sign(Certificate targetId, Date start, String period) throws SQLException, GigiApiException { + CertificateValiditySelector.checkValidityLength(period); + PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `jobs` SET targetId=?, task=?, executeFrom=?, executeTo=?"); + ps.setInt(1, targetId.getId()); + ps.setString(2, JobType.SIGN.getName()); + ps.setDate(3, start); + ps.setString(4, period); + ps.execute(); + return new Job(DatabaseConnection.lastInsertId(ps)); + } + + public static Job revoke(Certificate targetId) throws SQLException { + + PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `jobs` SET targetId=?, task=?"); + ps.setInt(1, targetId.getId()); + ps.setString(2, JobType.REVOKE.getName()); + ps.execute(); + return new Job(DatabaseConnection.lastInsertId(ps)); + } + + public boolean waitFor(int max) throws SQLException, InterruptedException { + long start = System.currentTimeMillis(); + PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `jobs` WHERE id=? AND state='open'"); + ps.setInt(1, id); + ResultSet rs = ps.executeQuery(); + while (rs.next()) { + rs.close(); + if (max != 0 && System.currentTimeMillis() - start > max) { + return false; + } + Thread.sleep((long) (2000 + Math.random() * 2000)); + rs = ps.executeQuery(); + } + rs.close(); + return true; + } }