Search in sources :

Example 16 with DataAccessException

use of org.xipki.datasource.DataAccessException in project xipki by xipki.

the class OcspCertPublisher method initialize.

@Override
public void initialize(String conf, PasswordResolver passwordResolver, Map<String, DataSourceWrapper> datasources) throws CertPublisherException {
    ParamUtil.requireNonNull("conf", conf);
    ParamUtil.requireNonEmpty("datasources", datasources);
    ConfPairs pairs = new ConfPairs(conf);
    String str = pairs.value("publish.goodcerts");
    this.publishsGoodCert = (str == null) ? true : Boolean.parseBoolean(str);
    str = pairs.value("asyn");
    this.asyn = (str == null) ? false : Boolean.parseBoolean(str);
    ConfPairs confPairs = new ConfPairs(conf);
    String datasourceName = confPairs.value("datasource");
    DataSourceWrapper datasource = null;
    if (datasourceName != null) {
        datasource = datasources.get(datasourceName);
    }
    if (datasource == null) {
        throw new CertPublisherException("no datasource named '" + datasourceName + "' is specified");
    }
    try {
        queryExecutor = new OcspStoreQueryExecutor(datasource, this.publishsGoodCert);
    } catch (NoSuchAlgorithmException | DataAccessException ex) {
        throw new CertPublisherException(ex.getMessage(), ex);
    }
}
Also used : ConfPairs(org.xipki.common.ConfPairs) CertPublisherException(org.xipki.ca.api.publisher.CertPublisherException) DataSourceWrapper(org.xipki.datasource.DataSourceWrapper) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DataAccessException(org.xipki.datasource.DataAccessException)

Example 17 with DataAccessException

use of org.xipki.datasource.DataAccessException in project xipki by xipki.

the class OcspStoreQueryExecutor method borrowPreparedStatement.

// method addIssuer
/**
 * TODO.
 * @param sqlQuery the SQL query
 * @return the next idle preparedStatement, {@code null} will be returned if no PreparedStament
 *      can be created within 5 seconds.
 */
private PreparedStatement borrowPreparedStatement(String sqlQuery) throws DataAccessException {
    PreparedStatement ps = null;
    Connection col = datasource.getConnection();
    if (col != null) {
        ps = datasource.prepareStatement(col, sqlQuery);
    }
    if (ps == null) {
        throw new DataAccessException("could not create prepared statement for " + sqlQuery);
    }
    return ps;
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) DataAccessException(org.xipki.datasource.DataAccessException)

Example 18 with DataAccessException

use of org.xipki.datasource.DataAccessException in project xipki by xipki.

the class CertStoreQueryExecutor method borrowPreparedStatement.

// method borrowPreparedStatements
private PreparedStatement borrowPreparedStatement(String sqlQuery) throws DataAccessException {
    PreparedStatement ps = null;
    Connection conn = datasource.getConnection();
    if (conn != null) {
        ps = datasource.prepareStatement(conn, sqlQuery);
    }
    if (ps != null) {
        return ps;
    }
    throw new DataAccessException("could not create prepared statement for " + sqlQuery);
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) DataAccessException(org.xipki.datasource.DataAccessException)

Example 19 with DataAccessException

use of org.xipki.datasource.DataAccessException in project xipki by xipki.

the class CertStoreQueryExecutor method borrowPreparedStatements.

private PreparedStatement[] borrowPreparedStatements(String... sqlQueries) throws DataAccessException {
    Connection conn = datasource.getConnection();
    if (conn == null) {
        throw new DataAccessException("could not get connection");
    }
    final int n = sqlQueries.length;
    PreparedStatement[] pss = new PreparedStatement[n];
    for (int i = 0; i < n; i++) {
        pss[i] = datasource.prepareStatement(conn, sqlQueries[i]);
        if (pss[i] != null) {
            continue;
        }
        // destroy all already initialized statements
        for (int j = 0; j < i; j++) {
            try {
                pss[j].close();
            } catch (Throwable th) {
                LOG.warn("could not close preparedStatement", th);
            }
        }
        try {
            conn.close();
        } catch (Throwable th) {
            LOG.warn("could not close connection", th);
        }
        throw new DataAccessException("could not create prepared statement for " + sqlQueries[i]);
    }
    return pss;
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) DataAccessException(org.xipki.datasource.DataAccessException)

Example 20 with DataAccessException

use of org.xipki.datasource.DataAccessException in project xipki by xipki.

the class DbGoodCertSerialIterator method readNextNumber.

private BigInteger readNextNumber() {
    BigInteger firstSerial = nextSerials.pollFirst();
    if (firstSerial != null) {
        return firstSerial;
    }
    if (noUnrevokedCerts) {
        return null;
    }
    String sql = sqlNextSerials;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    int idx = 0;
    try {
        stmt = caDataSource.getConnection().prepareStatement(sql);
        stmt.setInt(1, caId);
        stmt.setLong(2, nextStartId);
        rs = stmt.executeQuery();
        while (rs.next()) {
            idx++;
            long id = rs.getLong("ID");
            if (id + 1 > nextStartId) {
                nextStartId = id + 1;
            }
            String serialStr = rs.getString("SN");
            BigInteger serial = new BigInteger(serialStr, 16);
            if (!caSerial.equals(serial)) {
                nextSerials.addLast(serial);
            }
        }
    } catch (SQLException ex) {
        DataAccessException daex = caDataSource.translate(sql, ex);
        throw new NoSuchElementException(daex.getMessage());
    } catch (DataAccessException ex) {
        throw new NoSuchElementException(ex.getMessage());
    } finally {
        caDataSource.releaseResources(stmt, rs);
    }
    if (idx < numSqlEntries) {
        noUnrevokedCerts = true;
    }
    return nextSerials.pollFirst();
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) BigInteger(java.math.BigInteger) PreparedStatement(java.sql.PreparedStatement) DataAccessException(org.xipki.datasource.DataAccessException) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

DataAccessException (org.xipki.datasource.DataAccessException)21 PreparedStatement (java.sql.PreparedStatement)18 SQLException (java.sql.SQLException)14 CaMgmtException (org.xipki.ca.server.mgmt.api.CaMgmtException)9 ResultSet (java.sql.ResultSet)6 Connection (java.sql.Connection)5 BigInteger (java.math.BigInteger)3 CertificateEncodingException (java.security.cert.CertificateEncodingException)3 CertificateException (java.security.cert.CertificateException)3 X509Certificate (java.security.cert.X509Certificate)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ConfPairs (org.xipki.common.ConfPairs)3 IssuerEntry (org.xipki.ocsp.api.IssuerEntry)3 Date (java.util.Date)2 DataSourceWrapper (org.xipki.datasource.DataSourceWrapper)2 OcspStoreException (org.xipki.ocsp.api.OcspStoreException)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Statement (java.sql.Statement)1