Search in sources :

Example 6 with ConnectionException

use of io.jans.orm.exception.operation.ConnectionException in project jans by JanssenProject.

the class LdapOperationServiceImpl method deleteRecursivelyImpl.

protected boolean deleteRecursivelyImpl(String dn) {
    try {
        final DeleteRequest deleteRequest = new DeleteRequest(dn);
        deleteRequest.addControl(new SubtreeDeleteRequestControl());
        LDAPResult result = getConnectionPool().delete(deleteRequest);
        return ResultCode.SUCCESS.equals(result.getResultCode());
    } catch (Exception ex) {
        throw new ConnectionException("Failed to delete entry", ex);
    }
}
Also used : LDAPResult(com.unboundid.ldap.sdk.LDAPResult) DeleteRequest(com.unboundid.ldap.sdk.DeleteRequest) SubtreeDeleteRequestControl(com.unboundid.ldap.sdk.controls.SubtreeDeleteRequestControl) MappingException(io.jans.orm.exception.MappingException) LDAPSearchException(com.unboundid.ldap.sdk.LDAPSearchException) DuplicateEntryException(io.jans.orm.exception.operation.DuplicateEntryException) ConnectionException(io.jans.orm.exception.operation.ConnectionException) InvalidSimplePageControlException(io.jans.orm.ldap.exception.InvalidSimplePageControlException) LDAPException(com.unboundid.ldap.sdk.LDAPException) SearchException(io.jans.orm.exception.operation.SearchException) AuthenticationException(io.jans.orm.exception.AuthenticationException) ConnectionException(io.jans.orm.exception.operation.ConnectionException)

Example 7 with ConnectionException

use of io.jans.orm.exception.operation.ConnectionException in project jans by JanssenProject.

the class SpannerConnectionProvider method loadTableMetaData.

private void loadTableMetaData() {
    LOG.info("Scanning DB metadata...");
    long takes = System.currentTimeMillis();
    try (ResultSet resultSet = executeQuery(QUERY_PARENT_TABLE)) {
        if (resultSet.next()) {
            int tableNameIdx = resultSet.getColumnIndex("TABLE_NAME");
            int parentTableNameIdx = resultSet.getColumnIndex("PARENT_TABLE_NAME");
            do {
                String parentTableName = resultSet.getString(parentTableNameIdx);
                String tableName = resultSet.getString(tableNameIdx);
                Set<String> childAttributes;
                if (tableChildAttributesMap.containsKey(parentTableName)) {
                    childAttributes = tableChildAttributesMap.get(parentTableName);
                } else {
                    childAttributes = new HashSet<>();
                    tableChildAttributesMap.put(parentTableName, childAttributes);
                }
                if (tableName.startsWith(parentTableName + "_")) {
                    tableName = tableName.substring(parentTableName.length() + 1);
                }
                childAttributes.add(tableName);
            } while (resultSet.next());
        }
    } catch (SpannerException ex) {
        throw new ConnectionException("Failed to get database metadata", ex);
    }
    LOG.debug("Build child attributes map: '{}'.", tableChildAttributesMap);
    HashMap<String, Type> typeMap = buildSpannerTypesMap();
    try (ResultSet resultSet = executeQuery(QUERY_TABLE_SCHEMA)) {
        if (resultSet.next()) {
            int tableNameIdx = resultSet.getColumnIndex("TABLE_NAME");
            int columnNameIdx = resultSet.getColumnIndex("COLUMN_NAME");
            int spannerTypeIdx = resultSet.getColumnIndex("SPANNER_TYPE");
            int isNullableIdx = resultSet.getColumnIndex("IS_NULLABLE");
            do {
                String tableName = resultSet.getString(tableNameIdx);
                String columnName = resultSet.getString(columnNameIdx);
                String spannerType = resultSet.getString(spannerTypeIdx);
                String isNullable = resultSet.getString(isNullableIdx);
                // Load table schema
                Map<String, StructField> tableColumns;
                if (tableColumnsMap.containsKey(tableName)) {
                    tableColumns = tableColumnsMap.get(tableName);
                } else {
                    tableColumns = new HashMap<>();
                    tableColumnsMap.put(tableName, tableColumns);
                }
                String comparebleType = toComparableType(spannerType);
                Type type = typeMap.get(comparebleType);
                if (type == null) {
                    throw new ConnectionException(String.format("Failed to parse SPANNER_TYPE: '%s'", spannerType));
                }
                tableColumns.put(columnName.toLowerCase(), StructField.of(columnName, type));
                // Check if column nullable
                Set<String> nullableColumns;
                if (tableNullableColumnsSet.containsKey(tableName)) {
                    nullableColumns = tableNullableColumnsSet.get(tableName);
                } else {
                    nullableColumns = new HashSet<>();
                    tableNullableColumnsSet.put(tableName, nullableColumns);
                }
                boolean nullable = "yes".equalsIgnoreCase(isNullable);
                if (nullable) {
                    nullableColumns.add(columnName.toLowerCase());
                }
            } while (resultSet.next());
        }
    } catch (SpannerException ex) {
        throw new ConnectionException("Failed to get database metadata", ex);
    }
    LOG.debug("Build table columns map: '{}'.", tableColumnsMap);
    takes = System.currentTimeMillis() - takes;
    LOG.info("Metadata scan finisehd in {} milliseconds", takes);
}
Also used : Type(com.google.cloud.spanner.Type) StructField(com.google.cloud.spanner.Type.StructField) ResultSet(com.google.cloud.spanner.ResultSet) SpannerException(com.google.cloud.spanner.SpannerException) ConnectionException(io.jans.orm.exception.operation.ConnectionException)

Example 8 with ConnectionException

use of io.jans.orm.exception.operation.ConnectionException in project jans by JanssenProject.

the class SpannerConnectionProvider method openWithWaitImpl.

private void openWithWaitImpl() throws Exception {
    long connectionMaxWaitTimeMillis = StringHelper.toLong(props.getProperty("connection.client.create-max-wait-time-millis"), 30 * 1000L);
    LOG.debug("Using connection timeout: '{}'", connectionMaxWaitTimeMillis);
    Exception lastException = null;
    int attempt = 0;
    long currentTime = System.currentTimeMillis();
    long maxWaitTime = currentTime + connectionMaxWaitTimeMillis;
    do {
        attempt++;
        if (attempt > 0) {
            LOG.info("Attempting to create client connection: '{}'", attempt);
        }
        try {
            open();
            if (isConnected()) {
                break;
            } else {
                LOG.info("Failed to connect to Spanner");
                destroy();
                throw new ConnectionException("Failed to create client connection");
            }
        } catch (Exception ex) {
            lastException = ex;
        }
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
            LOG.error("Exception happened in sleep", ex);
            return;
        }
        currentTime = System.currentTimeMillis();
    } while (maxWaitTime > currentTime);
    if (lastException != null) {
        throw lastException;
    }
}
Also used : KeyConversionException(io.jans.orm.exception.KeyConversionException) MappingException(io.jans.orm.exception.MappingException) ConnectionException(io.jans.orm.exception.operation.ConnectionException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SpannerException(com.google.cloud.spanner.SpannerException) ConfigurationException(io.jans.orm.exception.operation.ConfigurationException) ConnectionException(io.jans.orm.exception.operation.ConnectionException)

Example 9 with ConnectionException

use of io.jans.orm.exception.operation.ConnectionException in project jans by JanssenProject.

the class SqlConnectionProvider method init.

protected void init() throws Exception {
    if (!props.containsKey("db.schema.name")) {
        throw new ConfigurationException("Property 'db.schema.name' is mandatory!");
    }
    this.schemaName = props.getProperty("db.schema.name");
    if (!props.containsKey("connection.uri")) {
        throw new ConfigurationException("Property 'connection.uri' is mandatory!");
    }
    this.connectionUri = props.getProperty("connection.uri");
    Properties filteredDriverProperties = PropertiesHelper.findProperties(props, DRIVER_PROPERTIES_PREFIX, ".");
    this.connectionProperties = new Properties();
    for (Entry<Object, Object> driverPropertyEntry : filteredDriverProperties.entrySet()) {
        String key = StringHelper.toString(driverPropertyEntry.getKey()).substring(DRIVER_PROPERTIES_PREFIX.length() + 1);
        String value = StringHelper.toString(driverPropertyEntry.getValue());
        connectionProperties.put(key, value);
    }
    String userName = props.getProperty("auth.userName");
    String userPassword = props.getProperty("auth.userPassword");
    connectionProperties.setProperty("user", userName);
    connectionProperties.setProperty("password", userPassword);
    this.objectPoolConfig = new GenericObjectPoolConfig<>();
    Integer cpMaxTotal = StringHelper.toInteger(props.getProperty("connection.pool.max-total"), null);
    if (cpMaxTotal != null) {
        objectPoolConfig.setMaxTotal(cpMaxTotal);
    }
    Integer cpMaxIdle = StringHelper.toInteger(props.getProperty("connection.pool.max-idle"), null);
    if (cpMaxIdle != null) {
        objectPoolConfig.setMaxIdle(cpMaxIdle);
    }
    Integer cpMinIdle = StringHelper.toInteger(props.getProperty("connection.pool.min-idle"), null);
    if (cpMinIdle != null) {
        objectPoolConfig.setMinIdle(cpMinIdle);
    }
    Integer cpMaxWaitTimeMillis = StringHelper.toInteger(props.getProperty("connection.pool.max-wait-time-millis"), null);
    if (cpMaxWaitTimeMillis != null) {
        objectPoolConfig.setMaxWaitMillis(cpMaxWaitTimeMillis);
    }
    Integer cpMinEvictableIdleTimeMillis = StringHelper.toInteger(props.getProperty("connection.pool.min-evictable-idle-time-millis"), null);
    if (cpMaxWaitTimeMillis != null) {
        objectPoolConfig.setMinEvictableIdleTimeMillis(cpMinEvictableIdleTimeMillis);
    }
    openWithWaitImpl();
    LOG.info("Created connection pool");
    if (props.containsKey("password.encryption.method")) {
        this.passwordEncryptionMethod = PasswordEncryptionMethod.getMethod(props.getProperty("password.encryption.method"));
    } else {
        this.passwordEncryptionMethod = PasswordEncryptionMethod.HASH_METHOD_SHA256;
    }
    this.binaryAttributes = new ArrayList<String>();
    if (props.containsKey("binaryAttributes")) {
        String[] binaryAttrs = StringHelper.split(props.get("binaryAttributes").toString().toLowerCase(), ",");
        this.binaryAttributes.addAll(Arrays.asList(binaryAttrs));
    }
    LOG.debug("Using next binary attributes: '{}'", binaryAttributes);
    this.certificateAttributes = new ArrayList<String>();
    if (props.containsKey("certificateAttributes")) {
        String[] binaryAttrs = StringHelper.split(props.get("certificateAttributes").toString().toLowerCase(), ",");
        this.certificateAttributes.addAll(Arrays.asList(binaryAttrs));
    }
    LOG.debug("Using next binary certificateAttributes: '{}'", certificateAttributes);
    try (Connection con = this.poolingDataSource.getConnection()) {
        DatabaseMetaData databaseMetaData = con.getMetaData();
        this.dbType = databaseMetaData.getDatabaseProductName().toLowerCase();
        this.dbVersion = databaseMetaData.getDatabaseProductVersion().toLowerCase();
        if ((this.dbVersion != null) && this.dbVersion.toLowerCase().contains("mariadb")) {
            this.mariaDb = true;
        }
        LOG.debug("Database product name: '{}'", dbType);
        loadTableMetaData(databaseMetaData, con);
    } catch (Exception ex) {
        throw new ConnectionException("Failed to detect database product name and load metadata", ex);
    }
    this.creationResultCode = ResultCode.SUCCESS_INT_VALUE;
}
Also used : Connection(java.sql.Connection) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) Properties(java.util.Properties) DatabaseMetaData(java.sql.DatabaseMetaData) KeyConversionException(io.jans.orm.exception.KeyConversionException) SQLException(java.sql.SQLException) ConnectionException(io.jans.orm.exception.operation.ConnectionException) ConfigurationException(io.jans.orm.exception.operation.ConfigurationException) ConfigurationException(io.jans.orm.exception.operation.ConfigurationException) ConnectionException(io.jans.orm.exception.operation.ConnectionException)

Example 10 with ConnectionException

use of io.jans.orm.exception.operation.ConnectionException in project jans by JanssenProject.

the class SqlConnectionProvider method openWithWaitImpl.

private void openWithWaitImpl() throws Exception {
    long connectionMaxWaitTimeMillis = StringHelper.toLong(props.getProperty("connection.pool.create-max-wait-time-millis"), 30 * 1000L);
    LOG.debug("Using connection timeout: '{}'", connectionMaxWaitTimeMillis);
    Exception lastException = null;
    int attempt = 0;
    long currentTime = System.currentTimeMillis();
    long maxWaitTime = currentTime + connectionMaxWaitTimeMillis;
    do {
        attempt++;
        if (attempt > 0) {
            LOG.info("Attempting to create connection pool: '{}'", attempt);
        }
        try {
            open();
            if (isConnected()) {
                break;
            } else {
                LOG.info("Failed to connect to DB");
                destroy();
                throw new ConnectionException("Failed to create connection pool");
            }
        } catch (Exception ex) {
            lastException = ex;
        }
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
            LOG.error("Exception happened in sleep", ex);
            return;
        }
        currentTime = System.currentTimeMillis();
    } while (maxWaitTime > currentTime);
    if (lastException != null) {
        throw lastException;
    }
}
Also used : KeyConversionException(io.jans.orm.exception.KeyConversionException) SQLException(java.sql.SQLException) ConnectionException(io.jans.orm.exception.operation.ConnectionException) ConfigurationException(io.jans.orm.exception.operation.ConfigurationException) ConnectionException(io.jans.orm.exception.operation.ConnectionException)

Aggregations

ConnectionException (io.jans.orm.exception.operation.ConnectionException)10 MappingException (io.jans.orm.exception.MappingException)5 SearchException (io.jans.orm.exception.operation.SearchException)5 AuthenticationException (io.jans.orm.exception.AuthenticationException)4 LocalizedString (io.jans.orm.model.base.LocalizedString)4 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)3 KeyConversionException (io.jans.orm.exception.KeyConversionException)3 ConfigurationException (io.jans.orm.exception.operation.ConfigurationException)3 SearchScopeException (io.jans.orm.exception.operation.SearchScopeException)3 SpannerException (com.google.cloud.spanner.SpannerException)2 EntryDeleteException (io.jans.orm.exception.EntryDeleteException)2 SQLException (java.sql.SQLException)2 ParseException (java.text.ParseException)2 ResultSet (com.google.cloud.spanner.ResultSet)1 Type (com.google.cloud.spanner.Type)1 StructField (com.google.cloud.spanner.Type.StructField)1 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)1 DeleteRequest (com.unboundid.ldap.sdk.DeleteRequest)1 LDAPException (com.unboundid.ldap.sdk.LDAPException)1 LDAPResult (com.unboundid.ldap.sdk.LDAPResult)1