]> WPIA git - gigi.git/blobdiff - util/org/cacert/gigi/util/SimpleSigner.java
Adding dummy signer bot to managed testcases.
[gigi.git] / util / org / cacert / gigi / util / SimpleSigner.java
index 36da084a39da76e18df82d68e90935a49d4d9929..2aaa1659d79a7cf7c3793f06af7a80aa13b73be7 100644 (file)
@@ -22,34 +22,72 @@ public class SimpleSigner {
        private static PreparedStatement readyMail;
        private static PreparedStatement revoke;
        private static PreparedStatement revokeCompleted;
+       private static boolean running = true;
+       private static Thread runner;
 
        public static void main(String[] args) throws IOException, SQLException, InterruptedException {
                Properties p = new Properties();
                p.load(new FileReader("config/gigi.properties"));
                DatabaseConnection.init(p);
 
+               runSigner();
+       }
+
+       public synchronized static void stopSigner() throws InterruptedException {
+               if (runner == null) {
+                       throw new IllegalStateException("already stopped");
+               }
+               running = false;
+               runner.interrupt();
+               runner.join();
+               runner = null;
+       }
+
+       public synchronized static void runSigner() throws SQLException, IOException, InterruptedException {
+               if (runner != null) {
+                       throw new IllegalStateException("already running");
+               }
+               running = true;
                readyMail = DatabaseConnection.getInstance().prepare(
-                               "SELECT id, csr_name, subject FROM emailcerts" + " WHERE csr_name is not null"//
-                                               + " AND created=0"//
-                                               + " AND crt_name=''"//
-                                               + " AND warning<3");
+                       "SELECT id, csr_name, subject FROM emailcerts" + " WHERE csr_name is not null"//
+                               + " AND created=0"//
+                               + " AND crt_name=''"//
+                               + " AND warning<3");
 
                updateMail = DatabaseConnection.getInstance().prepare(
-                               "UPDATE emailcerts SET crt_name=?," + " created=NOW(), serial=? WHERE id=?");
+                       "UPDATE emailcerts SET crt_name=?," + " created=NOW(), serial=? WHERE id=?");
                warnMail = DatabaseConnection.getInstance().prepare("UPDATE emailcerts SET warning=warning+1 WHERE id=?");
 
                revoke = DatabaseConnection.getInstance().prepare(
-                               "SELECT id, csr_name FROM emailcerts" + " WHERE csr_name is not null"//
-                                               + " AND created != 0"//
-                                               + " AND revoked = '1970-01-01'");
+                       "SELECT id, csr_name FROM emailcerts" + " WHERE csr_name is not null"//
+                               + " AND created != 0"//
+                               + " AND revoked = '1970-01-01'");
                revokeCompleted = DatabaseConnection.getInstance().prepare("UPDATE emailcerts SET revoked=NOW() WHERE id=?");
-               gencrl();
-               while (true) {
-                       System.out.println("ping");
-                       signCertificates();
-                       revokeCertificates();
-                       Thread.sleep(5000);
-               }
+               runner = new Thread() {
+                       @Override
+                       public void run() {
+                               try {
+                                       gencrl();
+                               } catch (IOException e2) {
+                                       e2.printStackTrace();
+                               } catch (InterruptedException e2) {
+                                       e2.printStackTrace();
+                               }
+                               while (running) {
+                                       try {
+                                               signCertificates();
+                                               revokeCertificates();
+                                               Thread.sleep(5000);
+                                       } catch (IOException e) {
+                                               e.printStackTrace();
+                                       } catch (SQLException e) {
+                                               e.printStackTrace();
+                                       } catch (InterruptedException e1) {
+                                       }
+                               }
+                       }
+               };
+               runner.start();
        }
 
        private static void revokeCertificates() throws SQLException, IOException, InterruptedException {