]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/dbObjects/Job.java
upd: cleanup SQL statements to make them statically verifiable.
[gigi.git] / src / org / cacert / gigi / dbObjects / Job.java
index c02a0467a79e73a1b589ad6cb56ad149366d6ff6..4fa297385642573f19187d2a92dd7ed94bacc041 100644 (file)
@@ -3,7 +3,7 @@ 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.DBEnum;
 import org.cacert.gigi.database.GigiPreparedStatement;
 import org.cacert.gigi.database.GigiResultSet;
 import org.cacert.gigi.output.CertificateValiditySelector;
@@ -16,7 +16,7 @@ public class Job implements IdCachable {
         this.id = id;
     }
 
-    public static enum JobType {
+    public static enum JobType implements DBEnum {
         SIGN("sign"), REVOKE("revoke");
 
         private final String name;
@@ -25,45 +25,53 @@ public class Job implements IdCachable {
             this.name = name;
         }
 
-        public String getName() {
+        @Override
+        public String getDBName() {
             return name;
         }
     }
 
-    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 cache.put(new Job(ps.lastInsertId()));
+        try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `jobs` SET targetId=?, task=?::`jobType`, executeFrom=?, executeTo=?")) {
+            ps.setInt(1, targetId.getId());
+            ps.setEnum(2, JobType.SIGN);
+            ps.setDate(3, start);
+            ps.setString(4, period);
+            ps.execute();
+            return cache.put(new Job(ps.lastInsertId()));
+        }
     }
 
     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 cache.put(new Job(ps.lastInsertId()));
+        try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `jobs` SET targetId=?, task=?::`jobType`")) {
+            ps.setInt(1, targetId.getId());
+            ps.setEnum(2, JobType.REVOKE);
+            ps.execute();
+            return cache.put(new Job(ps.lastInsertId()));
+        }
     }
 
-    public synchronized boolean waitFor(int max) throws InterruptedException {
+    public synchronized boolean waitFor(int max) {
         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;
+                }
+                try {
+                    this.wait((long) (2000 + Math.random() * 2000));
+                } catch (InterruptedException ie) {
+                    // Ignore the interruption
+                    ie.printStackTrace();
+                }
+                rs = ps.executeQuery();
             }
-            Thread.sleep((long) (2000 + Math.random() * 2000));
-            rs = ps.executeQuery();
         }
-        rs.close();
         return true;
     }
 
@@ -79,15 +87,16 @@ public class Job implements IdCachable {
         if (i != null) {
             return i;
         }
-        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("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;
+        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;
         }
-        return null;
 
     }
 }