use of org.apache.ignite.spi.IgniteSpiException in project ignite by apache.
the class TcpDiscoveryJdbcIpFinder method registerAddresses.
/**
* {@inheritDoc}
*/
@Override
public void registerAddresses(Collection<InetSocketAddress> addrs) throws IgniteSpiException {
assert !F.isEmpty(addrs);
init();
Connection conn = null;
PreparedStatement stmtUnreg = null;
PreparedStatement stmtReg = null;
boolean committed = false;
try {
conn = dataSrc.getConnection();
conn.setAutoCommit(false);
conn.setTransactionIsolation(TRANSACTION_READ_COMMITTED);
stmtUnreg = conn.prepareStatement(unregAddrQry);
stmtReg = conn.prepareStatement(regAddrQry);
for (InetSocketAddress addr : addrs) {
stmtUnreg.setString(1, addr.getAddress().getHostAddress());
stmtUnreg.setInt(2, addr.getPort());
stmtUnreg.addBatch();
stmtReg.setString(1, addr.getAddress().getHostAddress());
stmtReg.setInt(2, addr.getPort());
stmtReg.addBatch();
}
stmtUnreg.executeBatch();
stmtUnreg.close();
stmtReg.executeBatch();
stmtReg.close();
conn.commit();
committed = true;
} catch (SQLException e) {
U.rollbackConnectionQuiet(conn);
throw new IgniteSpiException("Failed to register addresses: " + addrs, e);
} finally {
if (!committed)
U.rollbackConnectionQuiet(conn);
U.closeQuiet(stmtUnreg);
U.closeQuiet(stmtReg);
U.closeQuiet(conn);
}
}
use of org.apache.ignite.spi.IgniteSpiException in project ignite by apache.
the class TcpDiscoveryJdbcIpFinder method init.
/**
* Checks configuration validity.
*
* @throws org.apache.ignite.spi.IgniteSpiException If any error occurs.
*/
private void init() throws IgniteSpiException {
if (initGuard.compareAndSet(false, true)) {
if (dataSrc == null)
throw new IgniteSpiException("Data source is null (you must configure it via setDataSource(..)" + " configuration property)");
if (!initSchema) {
initLatch.countDown();
checkSchema();
return;
}
Connection conn = null;
boolean committed = false;
try {
conn = dataSrc.getConnection();
conn.setAutoCommit(false);
conn.setTransactionIsolation(TRANSACTION_READ_COMMITTED);
DatabaseMetaData dbm = conn.getMetaData();
// so we do not use it.
try (ResultSet tables = dbm.getTables(null, null, addrTableName, null)) {
if (!tables.next()) {
// Create tbl_addrs.
try (Statement stmt = conn.createStatement()) {
stmt.executeUpdate(createAddrsTableQry);
conn.commit();
} catch (SQLException e) {
// see if the table has been created.
try (ResultSet tablesAgain = dbm.getTables(null, null, addrTableName, null)) {
if (!tablesAgain.next())
throw e;
}
}
}
}
committed = true;
if (log.isDebugEnabled())
log.debug("DB schema has been initialized.");
} catch (SQLException e) {
U.rollbackConnectionQuiet(conn);
throw new IgniteSpiException("Failed to initialize DB schema.", e);
} finally {
if (!committed)
U.rollbackConnectionQuiet(conn);
U.closeQuiet(conn);
initLatch.countDown();
}
} else
checkSchema();
}
use of org.apache.ignite.spi.IgniteSpiException in project ignite by apache.
the class TcpDiscoveryJdbcIpFinder method checkSchema.
/**
* Checks correctness of existing DB schema.
*
* @throws org.apache.ignite.spi.IgniteSpiException If schema wasn't properly initialized.
*/
private void checkSchema() throws IgniteSpiException {
try {
U.await(initLatch);
} catch (IgniteInterruptedCheckedException e) {
throw new IgniteSpiException("Thread has been interrupted.", e);
}
Connection conn = null;
Statement stmt = null;
try {
conn = dataSrc.getConnection();
conn.setTransactionIsolation(TRANSACTION_READ_COMMITTED);
// Check if tbl_addrs exists and database initialized properly.
stmt = conn.createStatement();
stmt.execute(chkQry);
} catch (SQLException e) {
throw new IgniteSpiException("IP finder has not been properly initialized.", e);
} finally {
U.closeQuiet(stmt);
U.closeQuiet(conn);
}
}
use of org.apache.ignite.spi.IgniteSpiException in project ignite by apache.
the class KeystoreEncryptionSpi method doEncryption.
/**
* @param data Plain data.
* @param cipher Cipher.
* @param key Encryption key.
*/
private void doEncryption(ByteBuffer data, Cipher cipher, Serializable key, ByteBuffer res) {
assert key instanceof KeystoreEncryptionKey;
ensureStarted();
try {
SecretKeySpec keySpec = new SecretKeySpec(((KeystoreEncryptionKey) key).key().getEncoded(), CIPHER_ALGO);
byte[] iv = initVector(cipher);
res.put(iv);
cipher.init(ENCRYPT_MODE, keySpec, new IvParameterSpec(iv));
cipher.doFinal(data, res);
} catch (ShortBufferException | InvalidAlgorithmParameterException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
throw new IgniteSpiException(e);
}
}
use of org.apache.ignite.spi.IgniteSpiException in project ignite by apache.
the class KeystoreEncryptionSpi method decryptNoPadding.
/**
* {@inheritDoc}
*/
@Override
public void decryptNoPadding(ByteBuffer data, Serializable key, ByteBuffer res) {
assert key instanceof KeystoreEncryptionKey;
ensureStarted();
try {
SecretKeySpec keySpec = new SecretKeySpec(((KeystoreEncryptionKey) key).key().getEncoded(), CIPHER_ALGO);
Cipher cipher = aesWithoutPadding.get();
byte[] iv = new byte[cipher.getBlockSize()];
data.get(iv);
cipher.init(DECRYPT_MODE, keySpec, new IvParameterSpec(iv));
cipher.doFinal(data, res);
} catch (InvalidAlgorithmParameterException | InvalidKeyException | IllegalBlockSizeException | ShortBufferException | BadPaddingException e) {
throw new IgniteSpiException(e);
}
}
Aggregations