use of org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration in project dbeaver by serge-rider.
the class DriverPropertiesDialogPage method setVisible.
@Override
public void setVisible(boolean visible) {
super.setVisible(visible);
// Set props model
if (visible && propsControl != null) {
final DBPDataSourceContainer activeDataSource = site.getActiveDataSource();
if (prevConnectionInfo == activeDataSource.getConnectionConfiguration()) {
return;
}
final DBPConnectionConfiguration tmpConnectionInfo = new DBPConnectionConfiguration();
final DataSourceDescriptor tempDataSource = new DataSourceDescriptor(site.getDataSourceRegistry(), activeDataSource.getId(), (DriverDescriptor) activeDataSource.getDriver(), tmpConnectionInfo);
hostPage.saveSettings(tempDataSource);
tmpConnectionInfo.getProperties().putAll(activeDataSource.getConnectionConfiguration().getProperties());
try {
getSite().getRunnableContext().run(true, true, new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
monitor.beginTask("Loading driver properties", 1);
try {
propertySource = propsControl.makeProperties(monitor, site.getDriver(), tmpConnectionInfo);
} finally {
monitor.done();
}
}
});
} catch (InvocationTargetException e) {
setErrorMessage(e.getTargetException().getMessage());
} catch (InterruptedException e) {
// ignore
}
propsControl.loadProperties(propertySource);
prevConnectionInfo = activeDataSource.getConnectionConfiguration();
tempDataSource.dispose();
}
}
use of org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration in project dbeaver by serge-rider.
the class JDBCDataSource method openConnection.
protected Connection openConnection(@NotNull DBRProgressMonitor monitor, @NotNull String purpose) throws DBCException {
// It MUST be a JDBC driver
Driver driverInstance;
try {
driverInstance = getDriverInstance(monitor);
} catch (DBException e) {
throw new DBCConnectException("Can't create driver instance", e, this);
}
// Set properties
Properties connectProps = new Properties();
{
// Use properties defined by datasource itself
Map<String, String> internalProps = getInternalConnectionProperties(monitor, purpose);
if (internalProps != null) {
connectProps.putAll(internalProps);
}
}
{
// Use driver properties
final Map<Object, Object> driverProperties = container.getDriver().getConnectionProperties();
for (Map.Entry<Object, Object> prop : driverProperties.entrySet()) {
connectProps.setProperty(CommonUtils.toString(prop.getKey()), CommonUtils.toString(prop.getValue()));
}
}
DBPConnectionConfiguration connectionInfo = container.getActualConnectionConfiguration();
for (Map.Entry<String, String> prop : connectionInfo.getProperties().entrySet()) {
connectProps.setProperty(CommonUtils.toString(prop.getKey()), CommonUtils.toString(prop.getValue()));
}
if (!CommonUtils.isEmpty(connectionInfo.getUserName())) {
connectProps.put(DBConstants.DATA_SOURCE_PROPERTY_USER, getConnectionUserName(connectionInfo));
}
if (!CommonUtils.isEmpty(connectionInfo.getUserPassword())) {
connectProps.put(DBConstants.DATA_SOURCE_PROPERTY_PASSWORD, getConnectionUserPassword(connectionInfo));
}
// Obtain connection
try {
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;
if (driverInstance == null) {
connection = DriverManager.getConnection(url, connectProps);
} else {
connection = driverInstance.connect(url, connectProps);
}
if (connection == null) {
throw new DBCException("Null connection returned");
}
// Set read-only flag
if (container.isConnectionReadOnly() && !isConnectionReadOnlyBroken()) {
connection.setReadOnly(true);
}
return connection;
} 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 database", e);
}
}
use of org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration in project dbeaver by serge-rider.
the class SSHTunnelImpl method initializeTunnel.
@Override
public DBPConnectionConfiguration initializeTunnel(DBRProgressMonitor monitor, DBPPlatform platform, DBWHandlerConfiguration configuration, DBPConnectionConfiguration connectionInfo) throws DBException, IOException {
String dbPortString = connectionInfo.getHostPort();
if (CommonUtils.isEmpty(dbPortString)) {
dbPortString = configuration.getDriver().getDefaultPort();
if (CommonUtils.isEmpty(dbPortString)) {
throw new DBException("Database port not specified and no default port number for driver '" + configuration.getDriver().getName() + "'");
}
}
String dbHost = connectionInfo.getHostName();
Map<String, String> properties = configuration.getProperties();
String sshAuthType = properties.get(SSHConstants.PROP_AUTH_TYPE);
String sshHost = properties.get(SSHConstants.PROP_HOST);
String sshPort = properties.get(SSHConstants.PROP_PORT);
String sshUser = configuration.getUserName();
String aliveInterval = properties.get(SSHConstants.PROP_ALIVE_INTERVAL);
String connectTimeoutString = properties.get(SSHConstants.PROP_CONNECT_TIMEOUT);
//String aliveCount = properties.get(SSHConstants.PROP_ALIVE_COUNT);
if (CommonUtils.isEmpty(sshHost)) {
throw new DBException("SSH host not specified");
}
if (CommonUtils.isEmpty(sshPort)) {
throw new DBException("SSH port not specified");
}
if (CommonUtils.isEmpty(sshUser)) {
throw new DBException("SSH user not specified");
}
int sshPortNum;
try {
sshPortNum = Integer.parseInt(sshPort);
} catch (NumberFormatException e) {
throw new DBException("Invalid SSH port: " + sshPort);
}
SSHConstants.AuthType authType = SSHConstants.AuthType.PASSWORD;
if (sshAuthType != null) {
authType = SSHConstants.AuthType.valueOf(sshAuthType);
}
File privKeyFile = null;
String privKeyPath = properties.get(SSHConstants.PROP_KEY_PATH);
if (authType == SSHConstants.AuthType.PUBLIC_KEY) {
if (CommonUtils.isEmpty(privKeyPath)) {
throw new DBException("Private key path is empty");
}
privKeyFile = new File(privKeyPath);
if (!privKeyFile.exists()) {
throw new DBException("Private key file '" + privKeyFile.getAbsolutePath() + "' doesn't exist");
}
}
int connectTimeout;
try {
connectTimeout = Integer.parseInt(connectTimeoutString);
} catch (NumberFormatException e) {
connectTimeout = SSHConstants.DEFAULT_CONNECT_TIMEOUT;
}
monitor.subTask("Initiating tunnel at '" + sshHost + "'");
UserInfo ui = new UIUserInfo(configuration);
int dbPort;
try {
dbPort = Integer.parseInt(dbPortString);
} catch (NumberFormatException e) {
throw new DBException("Bad database port number: " + dbPortString);
}
int localPort = savedLocalPort;
if (platform != null) {
localPort = findFreePort(platform);
}
try {
if (jsch == null) {
jsch = new JSch();
JSch.setLogger(new LoggerProxy());
}
if (privKeyFile != null) {
if (!CommonUtils.isEmpty(ui.getPassphrase())) {
jsch.addIdentity(privKeyFile.getAbsolutePath(), ui.getPassphrase());
} else {
jsch.addIdentity(privKeyFile.getAbsolutePath());
}
}
log.debug("Instantiate SSH tunnel");
session = jsch.getSession(sshUser, sshHost, sshPortNum);
session.setConfig("StrictHostKeyChecking", "no");
//session.setConfig("PreferredAuthentications", "password,publickey,keyboard-interactive");
session.setConfig("PreferredAuthentications", privKeyFile != null ? "publickey" : "password");
session.setConfig("ConnectTimeout", String.valueOf(connectTimeout));
session.setUserInfo(ui);
if (!CommonUtils.isEmpty(aliveInterval)) {
session.setServerAliveInterval(Integer.parseInt(aliveInterval));
}
log.debug("Connect to tunnel host");
session.connect(connectTimeout);
try {
session.setPortForwardingL(localPort, dbHost, dbPort);
} catch (JSchException e) {
closeTunnel(monitor);
throw e;
}
} catch (JSchException e) {
throw new DBException("Cannot establish tunnel", e);
}
savedLocalPort = localPort;
savedConfiguration = configuration;
savedConnectionInfo = connectionInfo;
connectionInfo = new DBPConnectionConfiguration(connectionInfo);
String newPortValue = String.valueOf(localPort);
// Replace database host/port and URL - let's use localhost
connectionInfo.setHostName(LOCALHOST_NAME);
connectionInfo.setHostPort(newPortValue);
String newURL = configuration.getDriver().getDataSourceProvider().getConnectionURL(configuration.getDriver(), connectionInfo);
connectionInfo.setUrl(newURL);
return connectionInfo;
}
Aggregations