Search in sources :

Example 1 with SQLExceptionChainBuilder

use of org.firebirdsql.util.SQLExceptionChainBuilder in project jaybird by FirebirdSQL.

the class FBManagedConnection method disassociateConnections.

/**
 * Disassociate connections from current managed connection.
 */
private void disassociateConnections() throws ResourceException {
    SQLExceptionChainBuilder<SQLException> chain = new SQLExceptionChainBuilder<>();
    // Iterate over copy of list as connection.close() will remove connection
    List<FBConnection> connectionHandleCopy = new ArrayList<>(connectionHandles);
    for (FBConnection connection : connectionHandleCopy) {
        try {
            connection.close();
        } catch (SQLException sqlex) {
            chain.append(sqlex);
        }
    }
    if (chain.hasException())
        throw new FBResourceException(chain.getException());
}
Also used : SQLExceptionChainBuilder(org.firebirdsql.util.SQLExceptionChainBuilder) SQLException(java.sql.SQLException) FBConnection(org.firebirdsql.jdbc.FBConnection) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 2 with SQLExceptionChainBuilder

use of org.firebirdsql.util.SQLExceptionChainBuilder in project jaybird by FirebirdSQL.

the class ChaChaEncryptionPlugin method initializeEncryption.

@Override
public EncryptionInitInfo initializeEncryption() {
    SQLExceptionChainBuilder<SQLException> chainBuilder = new SQLExceptionChainBuilder<>();
    Cipher encryptionCipher = null;
    Cipher decryptionCipher = null;
    try (ChaChaIV iv = new ChaChaIV()) {
        encryptionCipher = createEncryptionCipher(iv, chainBuilder);
        decryptionCipher = createDecryptionCipher(iv, chainBuilder);
    } catch (SQLException e) {
        chainBuilder.append(e);
    }
    if (chainBuilder.hasException()) {
        return EncryptionInitInfo.failure(getEncryptionIdentifier(), chainBuilder.getException());
    }
    return EncryptionInitInfo.success(getEncryptionIdentifier(), encryptionCipher, decryptionCipher);
}
Also used : SQLExceptionChainBuilder(org.firebirdsql.util.SQLExceptionChainBuilder) SQLException(java.sql.SQLException) Cipher(javax.crypto.Cipher)

Example 3 with SQLExceptionChainBuilder

use of org.firebirdsql.util.SQLExceptionChainBuilder in project jaybird by FirebirdSQL.

the class FBResultSet method closeFields.

/**
 * Close the fields if they were open (applies mainly to the stream fields).
 *
 * @throws SQLException
 *         if something wrong happened.
 */
protected void closeFields() throws SQLException {
    // TODO See if we can apply completion reason logic (eg no need to close blob on commit)
    wasNullValid = false;
    // if there are no fields to close, then nothing to do
    if (closeableFields.isEmpty())
        return;
    SQLExceptionChainBuilder<SQLException> chain = new SQLExceptionChainBuilder<>();
    // close current fields, so that resources are freed.
    for (final FBCloseableField field : closeableFields) {
        try {
            field.close();
        } catch (SQLException ex) {
            chain.append(ex);
        }
    }
    if (chain.hasException()) {
        throw chain.getException();
    }
}
Also used : SQLExceptionChainBuilder(org.firebirdsql.util.SQLExceptionChainBuilder) FBCloseableField(org.firebirdsql.jdbc.field.FBCloseableField)

Example 4 with SQLExceptionChainBuilder

use of org.firebirdsql.util.SQLExceptionChainBuilder in project jaybird by FirebirdSQL.

the class V13WireOperations method tryKnownServerKeys.

private void tryKnownServerKeys() throws IOException, SQLException {
    boolean initializedEncryption = false;
    SQLExceptionChainBuilder<SQLException> chainBuilder = new SQLExceptionChainBuilder<>();
    for (KnownServerKey.PluginSpecificData pluginSpecificData : getPluginSpecificData()) {
        EncryptionIdentifier encryptionIdentifier = pluginSpecificData.getEncryptionIdentifier();
        EncryptionPluginSpi currentEncryptionSpi = SUPPORTED_ENCRYPTION_PLUGINS.get(encryptionIdentifier);
        if (currentEncryptionSpi == null) {
            continue;
        }
        try (CryptSessionConfig cryptSessionConfig = getCryptSessionConfig(encryptionIdentifier, pluginSpecificData.getSpecificData())) {
            EncryptionPlugin encryptionPlugin = currentEncryptionSpi.createEncryptionPlugin(cryptSessionConfig);
            EncryptionInitInfo encryptionInitInfo = encryptionPlugin.initializeEncryption();
            if (encryptionInitInfo.isSuccess()) {
                enableEncryption(encryptionInitInfo);
                clearServerKeys();
                initializedEncryption = true;
                log.debug("Wire encryption established with " + encryptionIdentifier);
                break;
            } else {
                chainBuilder.append(encryptionInitInfo.getException());
            }
        } catch (SQLException e) {
            chainBuilder.append(e);
        }
    }
    if (!initializedEncryption && getAttachProperties().getWireCryptAsEnum() == WireCrypt.REQUIRED) {
        FbExceptionBuilder exceptionBuilder = new FbExceptionBuilder().nonTransientException(ISCConstants.isc_wirecrypt_incompatible);
        if (chainBuilder.hasException()) {
            exceptionBuilder.cause(chainBuilder.getException());
        }
        throw exceptionBuilder.toFlatSQLException();
    }
    if (chainBuilder.hasException()) {
        SQLException current = chainBuilder.getException();
        if (log.isWarnEnabled()) {
            log.warn(initializedEncryption ? "Wire encryption established, but some plugins failed; see other loglines for details" : "No wire encryption established because of errors");
            log.warn("Encryption plugin failed; see debug level for stacktraces:\n" + ExceptionHelper.collectAllMessages(current));
        }
        if (log.isDebugEnabled()) {
            do {
                log.debug("Encryption plugin failed", current);
            } while ((current = current.getNextException()) != null);
        }
    }
}
Also used : KnownServerKey(org.firebirdsql.gds.ng.wire.crypt.KnownServerKey) SQLExceptionChainBuilder(org.firebirdsql.util.SQLExceptionChainBuilder) SQLException(java.sql.SQLException) EncryptionPlugin(org.firebirdsql.gds.ng.wire.crypt.EncryptionPlugin) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) EncryptionInitInfo(org.firebirdsql.gds.ng.wire.crypt.EncryptionInitInfo) EncryptionIdentifier(org.firebirdsql.gds.ng.wire.crypt.EncryptionIdentifier) EncryptionPluginSpi(org.firebirdsql.gds.ng.wire.crypt.EncryptionPluginSpi) CryptSessionConfig(org.firebirdsql.gds.ng.wire.crypt.CryptSessionConfig)

Example 5 with SQLExceptionChainBuilder

use of org.firebirdsql.util.SQLExceptionChainBuilder in project jaybird by FirebirdSQL.

the class Arc4EncryptionPlugin method initializeEncryption.

@Override
public EncryptionInitInfo initializeEncryption() {
    SQLExceptionChainBuilder<SQLException> chainBuilder = new SQLExceptionChainBuilder<>();
    Cipher encryptionCipher = createEncryptionCipher(chainBuilder);
    Cipher decryptionCipher = createDecryptionCipher(chainBuilder);
    if (chainBuilder.hasException()) {
        return EncryptionInitInfo.failure(getEncryptionIdentifier(), chainBuilder.getException());
    }
    return EncryptionInitInfo.success(getEncryptionIdentifier(), encryptionCipher, decryptionCipher);
}
Also used : SQLExceptionChainBuilder(org.firebirdsql.util.SQLExceptionChainBuilder) SQLException(java.sql.SQLException) Cipher(javax.crypto.Cipher)

Aggregations

SQLExceptionChainBuilder (org.firebirdsql.util.SQLExceptionChainBuilder)5 SQLException (java.sql.SQLException)4 Cipher (javax.crypto.Cipher)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 FbExceptionBuilder (org.firebirdsql.gds.ng.FbExceptionBuilder)1 CryptSessionConfig (org.firebirdsql.gds.ng.wire.crypt.CryptSessionConfig)1 EncryptionIdentifier (org.firebirdsql.gds.ng.wire.crypt.EncryptionIdentifier)1 EncryptionInitInfo (org.firebirdsql.gds.ng.wire.crypt.EncryptionInitInfo)1 EncryptionPlugin (org.firebirdsql.gds.ng.wire.crypt.EncryptionPlugin)1 EncryptionPluginSpi (org.firebirdsql.gds.ng.wire.crypt.EncryptionPluginSpi)1 KnownServerKey (org.firebirdsql.gds.ng.wire.crypt.KnownServerKey)1 FBConnection (org.firebirdsql.jdbc.FBConnection)1 FBCloseableField (org.firebirdsql.jdbc.field.FBCloseableField)1