Search in sources :

Example 36 with DBPConnectionConfiguration

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();
    }
}
Also used : DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) InvocationTargetException(java.lang.reflect.InvocationTargetException) DataSourceDescriptor(org.jkiss.dbeaver.registry.DataSourceDescriptor)

Example 37 with DBPConnectionConfiguration

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);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) Properties(java.util.Properties) DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) Map(java.util.Map)

Example 38 with DBPConnectionConfiguration

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;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) File(java.io.File)

Aggregations

DBPConnectionConfiguration (org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration)38 DBException (org.jkiss.dbeaver.DBException)7 DataSourceDescriptor (org.jkiss.dbeaver.registry.DataSourceDescriptor)4 ArrayList (java.util.ArrayList)3 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)3 SelectionEvent (org.eclipse.swt.events.SelectionEvent)3 GridData (org.eclipse.swt.layout.GridData)3 Button (org.eclipse.swt.widgets.Button)3 Group (org.eclipse.swt.widgets.Group)3 Label (org.eclipse.swt.widgets.Label)3 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)3 EncryptionException (org.jkiss.dbeaver.registry.encode.EncryptionException)3 SecuredPasswordEncrypter (org.jkiss.dbeaver.registry.encode.SecuredPasswordEncrypter)3 BaseAuthDialog (org.jkiss.dbeaver.ui.dialogs.connection.BaseAuthDialog)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 DBPConnectionBootstrap (org.jkiss.dbeaver.model.connection.DBPConnectionBootstrap)2 DBPConnectionEventType (org.jkiss.dbeaver.model.connection.DBPConnectionEventType)2 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)2 DBRRunnableWithProgress (org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress)2 DriverDescriptor (org.jkiss.dbeaver.registry.driver.DriverDescriptor)2