X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2FdbObjects%2FJob.java;h=bb357a8d07b742fddda040031abf4a8ca0dd8d83;hb=73ac9fa5e36efef14464f40294e43ef85fadf320;hp=321b89d0c6dd83343b99f49ef6635d66539433a4;hpb=787dc5faad7c6829b0e9b699767fc6e13c17999e;p=gigi.git diff --git a/src/org/cacert/gigi/dbObjects/Job.java b/src/org/cacert/gigi/dbObjects/Job.java index 321b89d0..bb357a8d 100644 --- a/src/org/cacert/gigi/dbObjects/Job.java +++ b/src/org/cacert/gigi/dbObjects/Job.java @@ -3,12 +3,11 @@ package org.cacert.gigi.dbObjects; import java.sql.Date; import org.cacert.gigi.GigiApiException; -import org.cacert.gigi.database.DatabaseConnection; import org.cacert.gigi.database.GigiPreparedStatement; import org.cacert.gigi.database.GigiResultSet; import org.cacert.gigi.output.CertificateValiditySelector; -public class Job { +public class Job implements IdCachable { private int id; @@ -30,40 +29,67 @@ public class Job { } } - public static Job sign(Certificate targetId, Date start, String period) throws GigiApiException { + public synchronized static Job sign(Certificate targetId, Date start, String period) throws GigiApiException { CertificateValiditySelector.checkValidityLength(period); - GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `jobs` SET targetId=?, task=?::`jobType`, 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(ps.lastInsertId()); + try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `jobs` SET targetId=?, task=?::`jobType`, executeFrom=?, executeTo=?")) { + ps.setInt(1, targetId.getId()); + ps.setString(2, JobType.SIGN.getName()); + ps.setDate(3, start); + ps.setString(4, period); + ps.execute(); + return cache.put(new Job(ps.lastInsertId())); + } } - public static Job revoke(Certificate targetId) { + public synchronized static Job revoke(Certificate targetId) { - GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `jobs` SET targetId=?, task=?::`jobType`"); - ps.setInt(1, targetId.getId()); - ps.setString(2, JobType.REVOKE.getName()); - ps.execute(); - return new Job(ps.lastInsertId()); + try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `jobs` SET targetId=?, task=?::`jobType`")) { + ps.setInt(1, targetId.getId()); + ps.setString(2, JobType.REVOKE.getName()); + ps.execute(); + return cache.put(new Job(ps.lastInsertId())); + } } - public boolean waitFor(int max) throws InterruptedException { + public synchronized boolean waitFor(int max) throws InterruptedException { long start = System.currentTimeMillis(); - GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `jobs` WHERE id=? AND state='open'"); - ps.setInt(1, id); - GigiResultSet rs = ps.executeQuery(); - while (rs.next()) { - rs.close(); - if (max != 0 && System.currentTimeMillis() - start > max) { - return false; + try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `jobs` WHERE id=? AND state='open'")) { + ps.setInt(1, id); + GigiResultSet 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(); } - Thread.sleep((long) (2000 + Math.random() * 2000)); - rs = ps.executeQuery(); } - rs.close(); return true; } + + @Override + public int getId() { + return id; + } + + static ObjectCache cache = new ObjectCache<>(); + + public synchronized static Job getById(int id) { + Job i = cache.get(id); + if (i != null) { + return i; + } + try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `jobs` WHERE id=?'")) { + ps.setInt(1, id); + GigiResultSet rs = ps.executeQuery(); + if (rs.next()) { + Job j = new Job(id); + cache.put(j); + return j; + } + return null; + } + + } }