Search in sources :

Example 1 with OcspCertsReader

use of org.xipki.ca.dbtool.xmlio.ocsp.OcspCertsReader in project xipki by xipki.

the class OcspCertStoreDbImporter method importCert0.

// method importCert
private long importCert0(PreparedStatement psCert, String certsZipFile, long minId, File processLogFile, ProcessLog processLog, int numProcessedInLastProcess) throws Exception {
    ZipFile zipFile = new ZipFile(new File(certsZipFile));
    ZipEntry certsXmlEntry = zipFile.getEntry("certs.xml");
    OcspCertsReader certs;
    try {
        certs = new OcspCertsReader(zipFile.getInputStream(certsXmlEntry));
    } catch (Exception ex) {
        try {
            zipFile.close();
        } catch (Exception e2) {
            LOG.error("could not close ZIP file {}: {}", certsZipFile, e2.getMessage());
            LOG.debug("could not close ZIP file " + certsZipFile, e2);
        }
        throw ex;
    }
    disableAutoCommit();
    try {
        int numEntriesInBatch = 0;
        long lastSuccessfulCertId = 0;
        while (certs.hasNext()) {
            if (stopMe.get()) {
                throw new InterruptedException("interrupted by the user");
            }
            OcspCertType cert = (OcspCertType) certs.next();
            long id = cert.getId();
            if (id < minId) {
                continue;
            }
            numEntriesInBatch++;
            // cert
            try {
                int idx = 1;
                psCert.setLong(idx++, id);
                psCert.setInt(idx++, cert.getIid());
                psCert.setString(idx++, cert.getSn());
                psCert.setLong(idx++, cert.getUpdate());
                psCert.setLong(idx++, cert.getNbefore());
                psCert.setLong(idx++, cert.getNafter());
                setBoolean(psCert, idx++, cert.getRev().booleanValue());
                setInt(psCert, idx++, cert.getRr());
                setLong(psCert, idx++, cert.getRt());
                setLong(psCert, idx++, cert.getRit());
                psCert.setString(idx++, cert.getProfile());
                psCert.setString(idx++, cert.getHash());
                psCert.setString(idx++, cert.getSubject());
                psCert.addBatch();
            } catch (SQLException ex) {
                throw translate(SQL_ADD_CERT, ex);
            }
            boolean isLastBlock = !certs.hasNext();
            if (numEntriesInBatch > 0 && (numEntriesInBatch % this.numCertsPerCommit == 0 || isLastBlock)) {
                if (evaulateOnly) {
                    psCert.clearBatch();
                } else {
                    try {
                        psCert.executeBatch();
                        commit("(commit import cert to OCSP)");
                    } catch (Throwable th) {
                        rollback();
                        deleteCertGreatherThan(lastSuccessfulCertId, LOG);
                        if (th instanceof SQLException) {
                            throw translate(SQL_ADD_CERT, (SQLException) th);
                        } else if (th instanceof Exception) {
                            throw (Exception) th;
                        } else {
                            throw new Exception(th);
                        }
                    }
                }
                lastSuccessfulCertId = id;
                processLog.addNumProcessed(numEntriesInBatch);
                numEntriesInBatch = 0;
                echoToFile((numProcessedInLastProcess + processLog.numProcessed()) + ":" + lastSuccessfulCertId, processLogFile);
                processLog.printStatus();
            }
        }
        return lastSuccessfulCertId;
    } finally {
        recoverAutoCommit();
        zipFile.close();
    }
}
Also used : ZipFile(java.util.zip.ZipFile) SQLException(java.sql.SQLException) ZipEntry(java.util.zip.ZipEntry) OcspCertType(org.xipki.ca.dbtool.xmlio.ocsp.OcspCertType) ZipFile(java.util.zip.ZipFile) File(java.io.File) OcspCertsReader(org.xipki.ca.dbtool.xmlio.ocsp.OcspCertsReader) SQLException(java.sql.SQLException) DataAccessException(org.xipki.datasource.DataAccessException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) JAXBException(javax.xml.bind.JAXBException)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 CertificateException (java.security.cert.CertificateException)1 SQLException (java.sql.SQLException)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 JAXBException (javax.xml.bind.JAXBException)1 OcspCertType (org.xipki.ca.dbtool.xmlio.ocsp.OcspCertType)1 OcspCertsReader (org.xipki.ca.dbtool.xmlio.ocsp.OcspCertsReader)1 DataAccessException (org.xipki.datasource.DataAccessException)1