Search in sources :

Example 1 with DBAAuthCredentials

use of org.jkiss.dbeaver.model.auth.DBAAuthCredentials in project dbeaver by dbeaver.

the class DataSourceAuthModelDescriptor method createCredentialsSource.

@NotNull
@Override
public DBPPropertySource createCredentialsSource(DBPDataSourceContainer dataSource, DBPConnectionConfiguration configuration) {
    DBAAuthModel instance = getInstance();
    DBAAuthCredentials credentials = dataSource == null || configuration == null ? instance.createCredentials() : instance.loadCredentials(dataSource, configuration);
    PropertyCollector propertyCollector = new PropertyCollector(credentials, false);
    propertyCollector.collectProperties();
    return propertyCollector;
}
Also used : DBAAuthCredentials(org.jkiss.dbeaver.model.auth.DBAAuthCredentials) PropertyCollector(org.jkiss.dbeaver.runtime.properties.PropertyCollector) DBAAuthModel(org.jkiss.dbeaver.model.auth.DBAAuthModel) NotNull(org.jkiss.code.NotNull)

Example 2 with DBAAuthCredentials

use of org.jkiss.dbeaver.model.auth.DBAAuthCredentials in project dbeaver by serge-rider.

the class JDBCDataSource method openConnection.

protected Connection openConnection(@NotNull DBRProgressMonitor monitor, @Nullable JDBCExecutionContext context, @NotNull String purpose) throws DBCException {
    // It MUST be a JDBC driver
    Driver driverInstance = null;
    DBPDriver driver = getContainer().getDriver();
    if (driver.isInstantiable() && !CommonUtils.isEmpty(driver.getDriverClassName())) {
        try {
            driverInstance = getDriverInstance(monitor);
        } catch (DBException e) {
            throw new DBCConnectException("Can't create driver instance", e, this);
        }
    } else {
        if (!CommonUtils.isEmpty(driver.getDriverClassName())) {
            try {
                driver.loadDriver(monitor);
                Class.forName(driver.getDriverClassName(), true, driver.getClassLoader());
            } catch (Exception e) {
                throw new DBCException("Driver class '" + driver.getDriverClassName() + "' not found", e);
            }
        }
    }
    DBPConnectionConfiguration connectionInfo = new DBPConnectionConfiguration(container.getActualConnectionConfiguration());
    Properties connectProps = getAllConnectionProperties(monitor, context, purpose, connectionInfo);
    final JDBCConnectionConfigurer connectionConfigurer = GeneralUtils.adapt(this, JDBCConnectionConfigurer.class);
    DBAAuthModel authModel = connectionInfo.getAuthModel();
    // Obtain connection
    try {
        if (connectionConfigurer != null) {
            connectionConfigurer.beforeConnection(monitor, connectionInfo, connectProps);
        }
        final String url = getConnectionURL(connectionInfo);
        if (driverInstance != null) {
            try {
                if (!driverInstance.acceptsURL(url)) {
                    // Just write a warning in log. Some drivers are poorly coded and always returns false here.
                    log.error("Bad URL: " + url);
                }
            } catch (Throwable e) {
                log.debug("Error in " + driverInstance.getClass().getName() + ".acceptsURL() - " + url, e);
            }
        }
        monitor.subTask("Connecting " + purpose);
        Connection[] connection = new Connection[1];
        Exception[] error = new Exception[1];
        int openTimeout = getContainer().getPreferenceStore().getInt(ModelPreferences.CONNECTION_OPEN_TIMEOUT);
        final Driver driverInstanceFinal = driverInstance;
        try {
            DBAAuthCredentials credentials = authModel.loadCredentials(getContainer(), connectionInfo);
            authModel.initAuthentication(monitor, this, credentials, connectionInfo, connectProps);
        } catch (DBException e) {
            throw new DBCException("Authentication error: " + e.getMessage(), e);
        }
        DBRRunnableWithProgress connectTask = monitor1 -> {
            try {
                if (driverInstanceFinal == null) {
                    connection[0] = DriverManager.getConnection(url, connectProps);
                } else {
                    connection[0] = driverInstanceFinal.connect(url, connectProps);
                }
            } catch (Exception e) {
                error[0] = e;
            } finally {
                if (connectionConfigurer != null) {
                    try {
                        connectionConfigurer.afterConnection(monitor, connectionInfo, connectProps, connection[0], error[0]);
                    } catch (Exception e) {
                        log.debug(e);
                    }
                }
            }
        };
        boolean openTaskFinished;
        try {
            if (openTimeout <= 0) {
                openTaskFinished = true;
                connectTask.run(monitor);
            } else {
                openTaskFinished = RuntimeUtils.runTask(connectTask, "Opening database connection", openTimeout + 2000);
            }
        } finally {
            authModel.endAuthentication(container, connectionInfo, connectProps);
        }
        if (error[0] != null) {
            throw error[0];
        }
        if (!openTaskFinished) {
            throw new DBCException("Connection has timed out");
        }
        if (connection[0] == null) {
            throw new DBCException("Null connection returned");
        }
        // Set read-only flag
        if (container.isConnectionReadOnly() && !isConnectionReadOnlyBroken()) {
            connection[0].setReadOnly(true);
        }
        return connection[0];
    } catch (SQLException ex) {
        throw new DBCConnectException(ex.getMessage(), ex, this);
    } catch (DBCException ex) {
        throw ex;
    } catch (Throwable e) {
        throw new DBCConnectException("Unexpected driver error occurred while connecting to the database", e);
    }
}
Also used : java.sql(java.sql) JDBCFactory(org.jkiss.dbeaver.model.exec.jdbc.JDBCFactory) Property(org.jkiss.dbeaver.model.meta.Property) Nullable(org.jkiss.code.Nullable) NotNull(org.jkiss.code.NotNull) DBSInstanceContainer(org.jkiss.dbeaver.model.struct.DBSInstanceContainer) ModelMessages(org.jkiss.dbeaver.model.messages.ModelMessages) SocketException(java.net.SocketException) JDBCConnectionImpl(org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCConnectionImpl) DBPDriver(org.jkiss.dbeaver.model.connection.DBPDriver) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) Map(java.util.Map) Log(org.jkiss.dbeaver.Log) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) ModelPreferences(org.jkiss.dbeaver.ModelPreferences) org.jkiss.dbeaver.model(org.jkiss.dbeaver.model) IAdaptable(org.eclipse.core.runtime.IAdaptable) DBAAuthModel(org.jkiss.dbeaver.model.auth.DBAAuthModel) GeneralUtils(org.jkiss.dbeaver.utils.GeneralUtils) CommonUtils(org.jkiss.utils.CommonUtils) Properties(java.util.Properties) DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) org.jkiss.dbeaver.model.exec(org.jkiss.dbeaver.model.exec) JDBCFactoryDefault(org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCFactoryDefault) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) JDBCDatabaseMetaData(org.jkiss.dbeaver.model.exec.jdbc.JDBCDatabaseMetaData) List(java.util.List) RuntimeUtils(org.jkiss.dbeaver.utils.RuntimeUtils) DBException(org.jkiss.dbeaver.DBException) SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect) SQLState(org.jkiss.dbeaver.model.sql.SQLState) DBAAuthCredentials(org.jkiss.dbeaver.model.auth.DBAAuthCredentials) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) Collections(java.util.Collections) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) DBException(org.jkiss.dbeaver.DBException) DBAAuthCredentials(org.jkiss.dbeaver.model.auth.DBAAuthCredentials) DBPDriver(org.jkiss.dbeaver.model.connection.DBPDriver) Properties(java.util.Properties) SocketException(java.net.SocketException) DBException(org.jkiss.dbeaver.DBException) DBAAuthModel(org.jkiss.dbeaver.model.auth.DBAAuthModel) DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) DBPDriver(org.jkiss.dbeaver.model.connection.DBPDriver) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress)

Example 3 with DBAAuthCredentials

use of org.jkiss.dbeaver.model.auth.DBAAuthCredentials in project dbeaver by serge-rider.

the class DataSourceAuthModelDescriptor method createCredentialsSource.

@NotNull
@Override
public DBPPropertySource createCredentialsSource(DBPDataSourceContainer dataSource, DBPConnectionConfiguration configuration) {
    DBAAuthModel instance = getInstance();
    DBAAuthCredentials credentials = dataSource == null || configuration == null ? instance.createCredentials() : instance.loadCredentials(dataSource, configuration);
    PropertyCollector propertyCollector = new PropertyCollector(credentials, false);
    propertyCollector.collectProperties();
    return propertyCollector;
}
Also used : DBAAuthCredentials(org.jkiss.dbeaver.model.auth.DBAAuthCredentials) PropertyCollector(org.jkiss.dbeaver.runtime.properties.PropertyCollector) DBAAuthModel(org.jkiss.dbeaver.model.auth.DBAAuthModel) NotNull(org.jkiss.code.NotNull)

Example 4 with DBAAuthCredentials

use of org.jkiss.dbeaver.model.auth.DBAAuthCredentials in project dbeaver by dbeaver.

the class JDBCDataSource method openConnection.

protected Connection openConnection(@NotNull DBRProgressMonitor monitor, @Nullable JDBCExecutionContext context, @NotNull String purpose) throws DBCException {
    // It MUST be a JDBC driver
    Driver driverInstance = null;
    DBPDriver driver = getContainer().getDriver();
    if (driver.isInstantiable() && !CommonUtils.isEmpty(driver.getDriverClassName())) {
        try {
            driverInstance = getDriverInstance(monitor);
        } catch (DBException e) {
            throw new DBCConnectException("Can't create driver instance", e, this);
        }
    } else {
        if (!CommonUtils.isEmpty(driver.getDriverClassName())) {
            try {
                driver.loadDriver(monitor);
                Class.forName(driver.getDriverClassName(), true, driver.getClassLoader());
            } catch (Exception e) {
                throw new DBCException("Driver class '" + driver.getDriverClassName() + "' not found", e);
            }
        }
    }
    DBPConnectionConfiguration connectionInfo = new DBPConnectionConfiguration(container.getActualConnectionConfiguration());
    Properties connectProps = getAllConnectionProperties(monitor, context, purpose, connectionInfo);
    final JDBCConnectionConfigurer connectionConfigurer = GeneralUtils.adapt(this, JDBCConnectionConfigurer.class);
    DBAAuthModel authModel = connectionInfo.getAuthModel();
    // Obtain connection
    try {
        if (connectionConfigurer != null) {
            connectionConfigurer.beforeConnection(monitor, connectionInfo, connectProps);
        }
        final String url = getConnectionURL(connectionInfo);
        if (driverInstance != null) {
            try {
                if (!driverInstance.acceptsURL(url)) {
                    // Just write a warning in log. Some drivers are poorly coded and always returns false here.
                    log.error("Bad URL: " + url);
                }
            } catch (Throwable e) {
                log.debug("Error in " + driverInstance.getClass().getName() + ".acceptsURL() - " + url, e);
            }
        }
        monitor.subTask("Connecting " + purpose);
        Connection[] connection = new Connection[1];
        Exception[] error = new Exception[1];
        int openTimeout = getContainer().getPreferenceStore().getInt(ModelPreferences.CONNECTION_OPEN_TIMEOUT);
        final Driver driverInstanceFinal = driverInstance;
        try {
            DBAAuthCredentials credentials = authModel.loadCredentials(getContainer(), connectionInfo);
            authModel.initAuthentication(monitor, this, credentials, connectionInfo, connectProps);
        } catch (DBException e) {
            throw new DBCException("Authentication error: " + e.getMessage(), e);
        }
        DBRRunnableWithProgress connectTask = monitor1 -> {
            try {
                if (driverInstanceFinal == null) {
                    connection[0] = DriverManager.getConnection(url, connectProps);
                } else {
                    connection[0] = driverInstanceFinal.connect(url, connectProps);
                }
            } catch (Exception e) {
                error[0] = e;
            } finally {
                if (connectionConfigurer != null) {
                    try {
                        connectionConfigurer.afterConnection(monitor, connectionInfo, connectProps, connection[0], error[0]);
                    } catch (Exception e) {
                        log.debug(e);
                    }
                }
            }
        };
        boolean openTaskFinished;
        try {
            if (openTimeout <= 0) {
                openTaskFinished = true;
                connectTask.run(monitor);
            } else {
                openTaskFinished = RuntimeUtils.runTask(connectTask, "Opening database connection", openTimeout + 2000);
            }
        } finally {
            authModel.endAuthentication(container, connectionInfo, connectProps);
        }
        if (error[0] != null) {
            throw error[0];
        }
        if (!openTaskFinished) {
            throw new DBCException("Connection has timed out");
        }
        if (connection[0] == null) {
            throw new DBCException("Null connection returned");
        }
        // Set read-only flag
        if (container.isConnectionReadOnly() && !isConnectionReadOnlyBroken()) {
            connection[0].setReadOnly(true);
        }
        return connection[0];
    } catch (SQLException ex) {
        throw new DBCConnectException(ex.getMessage(), ex, this);
    } catch (DBCException ex) {
        throw ex;
    } catch (Throwable e) {
        throw new DBCConnectException("Unexpected driver error occurred while connecting to the database", e);
    }
}
Also used : java.sql(java.sql) JDBCFactory(org.jkiss.dbeaver.model.exec.jdbc.JDBCFactory) Property(org.jkiss.dbeaver.model.meta.Property) Nullable(org.jkiss.code.Nullable) NotNull(org.jkiss.code.NotNull) DBSInstanceContainer(org.jkiss.dbeaver.model.struct.DBSInstanceContainer) ModelMessages(org.jkiss.dbeaver.model.messages.ModelMessages) SocketException(java.net.SocketException) JDBCConnectionImpl(org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCConnectionImpl) DBPDriver(org.jkiss.dbeaver.model.connection.DBPDriver) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBSDataType(org.jkiss.dbeaver.model.struct.DBSDataType) Map(java.util.Map) Log(org.jkiss.dbeaver.Log) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) ModelPreferences(org.jkiss.dbeaver.ModelPreferences) org.jkiss.dbeaver.model(org.jkiss.dbeaver.model) IAdaptable(org.eclipse.core.runtime.IAdaptable) DBAAuthModel(org.jkiss.dbeaver.model.auth.DBAAuthModel) GeneralUtils(org.jkiss.dbeaver.utils.GeneralUtils) CommonUtils(org.jkiss.utils.CommonUtils) Properties(java.util.Properties) DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) org.jkiss.dbeaver.model.exec(org.jkiss.dbeaver.model.exec) JDBCFactoryDefault(org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCFactoryDefault) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) JDBCDatabaseMetaData(org.jkiss.dbeaver.model.exec.jdbc.JDBCDatabaseMetaData) List(java.util.List) RuntimeUtils(org.jkiss.dbeaver.utils.RuntimeUtils) DBException(org.jkiss.dbeaver.DBException) SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect) SQLState(org.jkiss.dbeaver.model.sql.SQLState) DBAAuthCredentials(org.jkiss.dbeaver.model.auth.DBAAuthCredentials) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) Collections(java.util.Collections) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) DBException(org.jkiss.dbeaver.DBException) DBAAuthCredentials(org.jkiss.dbeaver.model.auth.DBAAuthCredentials) DBPDriver(org.jkiss.dbeaver.model.connection.DBPDriver) Properties(java.util.Properties) SocketException(java.net.SocketException) DBException(org.jkiss.dbeaver.DBException) DBAAuthModel(org.jkiss.dbeaver.model.auth.DBAAuthModel) DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) DBPDriver(org.jkiss.dbeaver.model.connection.DBPDriver) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress)

Aggregations

NotNull (org.jkiss.code.NotNull)4 DBAAuthCredentials (org.jkiss.dbeaver.model.auth.DBAAuthCredentials)4 DBAAuthModel (org.jkiss.dbeaver.model.auth.DBAAuthModel)4 SocketException (java.net.SocketException)2 java.sql (java.sql)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 Properties (java.util.Properties)2 IAdaptable (org.eclipse.core.runtime.IAdaptable)2 Nullable (org.jkiss.code.Nullable)2 DBException (org.jkiss.dbeaver.DBException)2 Log (org.jkiss.dbeaver.Log)2 ModelPreferences (org.jkiss.dbeaver.ModelPreferences)2 org.jkiss.dbeaver.model (org.jkiss.dbeaver.model)2 DBPConnectionConfiguration (org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration)2 DBPDriver (org.jkiss.dbeaver.model.connection.DBPDriver)2 org.jkiss.dbeaver.model.exec (org.jkiss.dbeaver.model.exec)2 JDBCDatabaseMetaData (org.jkiss.dbeaver.model.exec.jdbc.JDBCDatabaseMetaData)2 JDBCFactory (org.jkiss.dbeaver.model.exec.jdbc.JDBCFactory)2