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;
}
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);
}
}
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;
}
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);
}
}
Aggregations