Search in sources :

Example 1 with DBAAuthModel

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

the class PostgreDataSourceProvider method getConnectionURL.

@Override
public String getConnectionURL(DBPDriver driver, DBPConnectionConfiguration connectionInfo) {
    DBAAuthModel authModel = connectionInfo.getAuthModel();
    if (authModel instanceof DBPDataSourceURLProvider) {
        return ((DBPDataSourceURLProvider) authModel).getConnectionURL(driver, connectionInfo);
    }
    PostgreServerType serverType = PostgreUtils.getServerType(driver);
    if (serverType.supportsCustomConnectionURL()) {
        return JDBCURL.generateUrlByTemplate(driver, connectionInfo);
    }
    StringBuilder url = new StringBuilder();
    url.append("jdbc:postgresql://");
    url.append(connectionInfo.getHostName());
    if (!CommonUtils.isEmpty(connectionInfo.getHostPort())) {
        url.append(":").append(connectionInfo.getHostPort());
    }
    url.append("/");
    if (!CommonUtils.isEmpty(connectionInfo.getDatabaseName())) {
        url.append(connectionInfo.getDatabaseName());
    }
    // }
    return url.toString();
}
Also used : PostgreServerType(org.jkiss.dbeaver.ext.postgresql.model.impls.PostgreServerType) DBPDataSourceURLProvider(org.jkiss.dbeaver.model.DBPDataSourceURLProvider) DBAAuthModel(org.jkiss.dbeaver.model.auth.DBAAuthModel)

Example 2 with DBAAuthModel

use of org.jkiss.dbeaver.model.auth.DBAAuthModel 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 DBAAuthModel

use of org.jkiss.dbeaver.model.auth.DBAAuthModel 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)

Aggregations

DBAAuthModel (org.jkiss.dbeaver.model.auth.DBAAuthModel)3 NotNull (org.jkiss.code.NotNull)2 DBAAuthCredentials (org.jkiss.dbeaver.model.auth.DBAAuthCredentials)2 SocketException (java.net.SocketException)1 java.sql (java.sql)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Properties (java.util.Properties)1 IAdaptable (org.eclipse.core.runtime.IAdaptable)1 Nullable (org.jkiss.code.Nullable)1 DBException (org.jkiss.dbeaver.DBException)1 Log (org.jkiss.dbeaver.Log)1 ModelPreferences (org.jkiss.dbeaver.ModelPreferences)1 PostgreServerType (org.jkiss.dbeaver.ext.postgresql.model.impls.PostgreServerType)1 org.jkiss.dbeaver.model (org.jkiss.dbeaver.model)1 DBPDataSourceURLProvider (org.jkiss.dbeaver.model.DBPDataSourceURLProvider)1 DBPConnectionConfiguration (org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration)1 DBPDriver (org.jkiss.dbeaver.model.connection.DBPDriver)1 org.jkiss.dbeaver.model.exec (org.jkiss.dbeaver.model.exec)1