Search in sources :

Example 31 with DBWHandlerConfiguration

use of org.jkiss.dbeaver.model.net.DBWHandlerConfiguration in project dbeaver by dbeaver.

the class DataSourceUtils method getDataSourceBySpec.

public static DBPDataSourceContainer getDataSourceBySpec(@NotNull DBPProject project, @NotNull String connectionSpec, @Nullable GeneralUtils.IParameterHandler parameterHandler, boolean searchByParameters, boolean createNewDataSource) {
    String driverName = null, url = null, host = null, port = null, server = null, database = null, user = null, password = null, authModelId = null;
    boolean showSystemObjects = false, showUtilityObjects = false, showOnlyEntities = false, hideFolders = false, hideSchemas = false, mergeEntities = false, savePassword = true;
    Boolean autoCommit = null;
    Map<String, String> conProperties = new HashMap<>();
    Map<String, Map<String, String>> handlerProps = new HashMap<>();
    Map<String, String> authProperties = new HashMap<>();
    DBPDataSourceFolder folder = null;
    String dsId = null, dsName = null;
    DBPDataSourceRegistry dsRegistry = project == null ? null : project.getDataSourceRegistry();
    if (dsRegistry == null) {
        log.debug("No datasource registry for project '" + project.getName() + "'");
        return null;
    }
    String[] conParams = connectionSpec.split("\\|");
    for (String cp : conParams) {
        int divPos = cp.indexOf('=');
        if (divPos == -1) {
            continue;
        }
        String paramName = cp.substring(0, divPos);
        String paramValue = cp.substring(divPos + 1);
        switch(paramName) {
            case PARAM_ID:
                dsId = paramValue;
                break;
            case PARAM_DRIVER:
                driverName = paramValue;
                break;
            case PARAM_NAME:
                dsName = paramValue;
                break;
            case PARAM_URL:
                url = paramValue;
                break;
            case PARAM_HOST:
                host = paramValue;
                break;
            case PARAM_PORT:
                port = paramValue;
                break;
            case PARAM_SERVER:
                server = paramValue;
                break;
            case PARAM_DATABASE:
                database = paramValue;
                break;
            case PARAM_USER:
                user = paramValue;
                break;
            case PARAM_PASSWORD:
                password = paramValue;
                break;
            case PARAM_AUTH_MODEL:
                authModelId = paramValue;
                break;
            case PARAM_SAVE_PASSWORD:
                savePassword = CommonUtils.toBoolean(paramValue);
                break;
            case PARAM_SHOW_SYSTEM_OBJECTS:
                showSystemObjects = CommonUtils.toBoolean(paramValue);
                break;
            case PARAM_SHOW_UTILITY_OBJECTS:
                showUtilityObjects = CommonUtils.toBoolean(paramValue);
                break;
            case PARAM_SHOW_ONLY_ENTITIES:
                showOnlyEntities = CommonUtils.toBoolean(paramValue);
                break;
            case PARAM_HIDE_FOLDERS:
                hideFolders = CommonUtils.toBoolean(paramValue);
                break;
            case PARAM_HIDE_SCHEMAS:
                hideSchemas = CommonUtils.toBoolean(paramValue);
                break;
            case PARAM_MERGE_ENTITIES:
                mergeEntities = CommonUtils.toBoolean(paramValue);
                break;
            case PARAM_FOLDER:
                folder = dsRegistry.getFolder(paramValue);
                break;
            case PARAM_AUTO_COMMIT:
                autoCommit = CommonUtils.toBoolean(paramValue);
                break;
            default:
                boolean handled = false;
                if (paramName.length() > PREFIX_PROP.length() && paramName.startsWith(PREFIX_PROP)) {
                    paramName = paramName.substring(PREFIX_PROP.length());
                    conProperties.put(paramName, paramValue);
                    handled = true;
                } else if (paramName.length() > PREFIX_AUTH_PROP.length() && paramName.startsWith(PREFIX_AUTH_PROP)) {
                    paramName = paramName.substring(PREFIX_AUTH_PROP.length());
                    authProperties.put(paramName, paramValue);
                    handled = true;
                } else if (paramName.length() > PREFIX_HANDLER.length() && paramName.startsWith(PREFIX_HANDLER)) {
                    // network handler prop
                    paramName = paramName.substring(PREFIX_HANDLER.length());
                    divPos = paramName.indexOf('.');
                    if (divPos == -1) {
                        log.debug("Wrong handler parameter: '" + paramName + "'");
                        continue;
                    }
                    String handlerId = paramName.substring(0, divPos);
                    paramName = paramName.substring(divPos + 1);
                    Map<String, String> handlerPopMap = handlerProps.computeIfAbsent(handlerId, k -> new HashMap<>());
                    handlerPopMap.put(paramName, paramValue);
                    handled = true;
                } else if (parameterHandler != null) {
                    handled = parameterHandler.setParameter(paramName, paramValue);
                }
                if (!handled) {
                    log.debug("Unknown connection parameter '" + paramName + "'");
                }
        }
    }
    DBPDataSourceContainer dataSource = null;
    if (dsId != null) {
        dataSource = dsRegistry.getDataSource(dsId);
    }
    if (dsName != null) {
        dataSource = dsRegistry.findDataSourceByName(dsName);
    }
    if (dataSource != null) {
        DBPConnectionConfiguration connConfig = dataSource.getConnectionConfiguration();
        if (!CommonUtils.isEmpty(database))
            connConfig.setDatabaseName(database);
        if (!CommonUtils.isEmpty(user))
            connConfig.setUserName(user);
        if (!CommonUtils.isEmpty(password))
            connConfig.setUserPassword(password);
        if (!CommonUtils.isEmpty(conProperties))
            connConfig.setProperties(conProperties);
        if (!CommonUtils.isEmpty(authProperties))
            connConfig.setAuthProperties(authProperties);
        if (!CommonUtils.isEmpty(authModelId))
            connConfig.setAuthModelId(authModelId);
        return dataSource;
    }
    if (searchByParameters) {
        // Try to find by parameters / handler props
        if (url != null) {
            for (DBPDataSourceContainer ds : dsRegistry.getDataSources()) {
                if (url.equals(ds.getConnectionConfiguration().getUrl())) {
                    if (user == null || user.equals(ds.getConnectionConfiguration().getUserName())) {
                        return ds;
                    }
                }
            }
        } else {
            for (DBPDataSourceContainer ds : dsRegistry.getDataSources()) {
                DBPConnectionConfiguration cfg = ds.getConnectionConfiguration();
                if (server != null && !server.equals(cfg.getServerName()) || host != null && !host.equals(cfg.getHostName()) || port != null && !port.equals(cfg.getHostPort()) || database != null && !database.equals(cfg.getDatabaseName()) || user != null && !user.equals(cfg.getUserName())) {
                    continue;
                }
                boolean matched = true;
                if (!conProperties.isEmpty()) {
                    for (Map.Entry<String, String> prop : conProperties.entrySet()) {
                        if (!CommonUtils.equalObjects(cfg.getProperty(prop.getKey()), prop.getValue())) {
                            matched = false;
                            break;
                        }
                    }
                    if (!matched) {
                        continue;
                    }
                }
                if (!handlerProps.isEmpty()) {
                    for (Map.Entry<String, Map<String, String>> handlerProp : handlerProps.entrySet()) {
                        DBWHandlerConfiguration handler = cfg.getHandler(handlerProp.getKey());
                        if (handler == null) {
                            matched = false;
                            break;
                        }
                        for (Map.Entry<String, String> prop : handlerProp.getValue().entrySet()) {
                            if (!CommonUtils.equalObjects(handler.getProperty(prop.getKey()), prop.getValue())) {
                                matched = false;
                                break;
                            }
                        }
                        if (!matched) {
                            break;
                        }
                    }
                    if (!matched) {
                        continue;
                    }
                }
                return ds;
            }
        }
    }
    if (!createNewDataSource) {
        return null;
    }
    if (driverName == null) {
        log.error("Driver name not specified - can't create new datasource");
        return null;
    }
    DBPDriver driver = DBWorkbench.getPlatform().getDataSourceProviderRegistry().findDriver(driverName);
    if (driver == null) {
        log.error("Driver '" + driverName + "' not found");
        return null;
    }
    // Create new datasource with specified parameters
    if (dsName == null) {
        dsName = "Ext: " + driver.getName();
        if (database != null) {
            dsName += " - " + database;
        } else if (server != null) {
            dsName += " - " + server;
        }
    }
    DBPConnectionConfiguration connConfig = new DBPConnectionConfiguration();
    connConfig.setUrl(url);
    connConfig.setHostName(host);
    connConfig.setHostPort(port);
    connConfig.setServerName(server);
    connConfig.setDatabaseName(database);
    connConfig.setUserName(user);
    connConfig.setUserPassword(password);
    connConfig.setProperties(conProperties);
    if (!CommonUtils.isEmpty(authProperties)) {
        connConfig.setAuthProperties(authProperties);
    }
    if (!CommonUtils.isEmpty(authModelId)) {
        connConfig.setAuthModelId(authModelId);
    }
    if (autoCommit != null) {
        connConfig.getBootstrap().setDefaultAutoCommit(autoCommit);
    }
    DBPDataSourceContainer newDS = dsRegistry.createDataSource(driver, connConfig);
    newDS.setName(dsName);
    ((DataSourceDescriptor) newDS).setTemporary(true);
    if (savePassword) {
        newDS.setSavePassword(true);
    }
    if (folder != null) {
        newDS.setFolder(folder);
    }
    DataSourceNavigatorSettings navSettings = ((DataSourceDescriptor) newDS).getNavigatorSettings();
    navSettings.setShowSystemObjects(showSystemObjects);
    navSettings.setShowUtilityObjects(showUtilityObjects);
    navSettings.setShowOnlyEntities(showOnlyEntities);
    navSettings.setHideSchemas(hideSchemas);
    navSettings.setHideFolders(hideFolders);
    navSettings.setMergeEntities(mergeEntities);
    // ds.set
    dsRegistry.addDataSource(newDS);
    return newDS;
}
Also used : HashMap(java.util.HashMap) DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) DBWHandlerConfiguration(org.jkiss.dbeaver.model.net.DBWHandlerConfiguration) DBPDriver(org.jkiss.dbeaver.model.connection.DBPDriver) DBPDataSourceFolder(org.jkiss.dbeaver.model.DBPDataSourceFolder) DBPDataSourceRegistry(org.jkiss.dbeaver.model.app.DBPDataSourceRegistry) HashMap(java.util.HashMap) Map(java.util.Map) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Example 32 with DBWHandlerConfiguration

use of org.jkiss.dbeaver.model.net.DBWHandlerConfiguration in project dbeaver by dbeaver.

the class SQLServerDataSource method getAllConnectionProperties.

@Override
protected Properties getAllConnectionProperties(@NotNull DBRProgressMonitor monitor, JDBCExecutionContext context, String purpose, DBPConnectionConfiguration connectionInfo) throws DBCException {
    Properties properties = super.getAllConnectionProperties(monitor, context, purpose, connectionInfo);
    if (!getContainer().getPreferenceStore().getBoolean(ModelPreferences.META_CLIENT_NAME_DISABLE)) {
        // App name
        properties.put(SQLServerUtils.isDriverJtds(getContainer().getDriver()) ? SQLServerConstants.APPNAME_CLIENT_PROPERTY : SQLServerConstants.APPLICATION_NAME_CLIENT_PROPERTY, CommonUtils.truncateString(DBUtils.getClientApplicationName(getContainer(), context, purpose), 64));
    }
    fillConnectionProperties(connectionInfo, properties);
    SQLServerAuthentication authSchema = SQLServerUtils.detectAuthSchema(connectionInfo);
    authSchema.getInitializer().initializeAuthentication(connectionInfo, properties);
    final DBWHandlerConfiguration sslConfig = getContainer().getActualConnectionConfiguration().getHandler(SQLServerConstants.HANDLER_SSL);
    if (sslConfig != null && sslConfig.isEnabled()) {
        initSSL(monitor, properties, sslConfig);
    }
    return properties;
}
Also used : DBWHandlerConfiguration(org.jkiss.dbeaver.model.net.DBWHandlerConfiguration) Properties(java.util.Properties)

Example 33 with DBWHandlerConfiguration

use of org.jkiss.dbeaver.model.net.DBWHandlerConfiguration in project dbeaver by dbeaver.

the class MySQLDataSource method getInternalConnectionProperties.

@Override
protected Map<String, String> getInternalConnectionProperties(DBRProgressMonitor monitor, DBPDriver driver, JDBCExecutionContext context, String purpose, DBPConnectionConfiguration connectionInfo) throws DBCException {
    Map<String, String> props = new LinkedHashMap<>(MySQLDataSourceProvider.getConnectionsProps());
    final DBWHandlerConfiguration sslConfig = getContainer().getActualConnectionConfiguration().getHandler(MySQLConstants.HANDLER_SSL);
    if (sslConfig != null && sslConfig.isEnabled()) {
        try {
            initSSL(monitor, props, sslConfig);
        } catch (Exception e) {
            throw new DBCException("Error configuring SSL certificates", e);
        }
    } else {
        // Newer MySQL servers/connectors requires explicit SSL disable
        props.put("useSSL", "false");
    }
    String serverTZ = connectionInfo.getProviderProperty(MySQLConstants.PROP_SERVER_TIMEZONE);
    if (CommonUtils.isEmpty(serverTZ) && inServerTimezoneHandle) /*&& getContainer().getDriver().getId().equals(MySQLConstants.DRIVER_ID_MYSQL8)*/
    {
        serverTZ = "UTC";
    }
    if (!CommonUtils.isEmpty(serverTZ)) {
        props.put("serverTimezone", serverTZ);
    }
    if (!isMariaDB()) {
        // Hacking different MySQL drivers zeroDateTimeBehavior property (#4103)
        String zeroDateTimeBehavior = connectionInfo.getProperty(MySQLConstants.PROP_ZERO_DATETIME_BEHAVIOR);
        if (zeroDateTimeBehavior == null) {
            try {
                Driver driverInstance = (Driver) driver.getDriverInstance(monitor);
                if (driverInstance != null) {
                    if (driverInstance.getMajorVersion() >= 8) {
                        props.put(MySQLConstants.PROP_ZERO_DATETIME_BEHAVIOR, "CONVERT_TO_NULL");
                    } else {
                        props.put(MySQLConstants.PROP_ZERO_DATETIME_BEHAVIOR, "convertToNull");
                    }
                }
            } catch (Exception e) {
                log.debug("Error setting MySQL " + MySQLConstants.PROP_ZERO_DATETIME_BEHAVIOR + " property default");
            }
        }
    }
    return props;
}
Also used : DBWHandlerConfiguration(org.jkiss.dbeaver.model.net.DBWHandlerConfiguration) DBPDriver(org.jkiss.dbeaver.model.connection.DBPDriver) Driver(java.sql.Driver) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBCException(org.jkiss.dbeaver.model.exec.DBCException) SQLException(java.sql.SQLException) MalformedURLException(java.net.MalformedURLException) DBException(org.jkiss.dbeaver.DBException)

Example 34 with DBWHandlerConfiguration

use of org.jkiss.dbeaver.model.net.DBWHandlerConfiguration in project dbeaver by serge-rider.

the class PostgreDataSource method getInternalConnectionProperties.

@Override
protected Map<String, String> getInternalConnectionProperties(DBRProgressMonitor monitor, String purpose) throws DBCException {
    Map<String, String> props = new LinkedHashMap<>(PostgreDataSourceProvider.getConnectionsProps());
    final DBWHandlerConfiguration sslConfig = getContainer().getActualConnectionConfiguration().getDeclaredHandler(PostgreConstants.HANDLER_SSL);
    if (sslConfig != null && sslConfig.isEnabled()) {
        try {
            initSSL(props, sslConfig);
        } catch (Exception e) {
            throw new DBCException("Error configuring SSL certificates", e);
        }
    }
    return props;
}
Also used : DBWHandlerConfiguration(org.jkiss.dbeaver.model.net.DBWHandlerConfiguration) SQLException(java.sql.SQLException) DBException(org.jkiss.dbeaver.DBException)

Example 35 with DBWHandlerConfiguration

use of org.jkiss.dbeaver.model.net.DBWHandlerConfiguration in project dbeaver by serge-rider.

the class ConnectionPageNetwork method saveConfigurations.

void saveConfigurations(DataSourceDescriptor dataSource) {
    boolean foundHandlers = false;
    java.util.List<DBWHandlerConfiguration> handlers = new ArrayList<>();
    for (HandlerBlock handlerBlock : configurations.values()) {
        DBWHandlerConfiguration configuration = handlerBlock.loadedConfigs.get(dataSource.getId());
        if (configuration != null) {
            foundHandlers = true;
            handlerBlock.configurator.saveSettings(configuration);
            handlers.add(configuration);
        }
    }
    if (foundHandlers) {
        dataSource.getConnectionConfiguration().setHandlers(handlers);
    }
}
Also used : DBWHandlerConfiguration(org.jkiss.dbeaver.model.net.DBWHandlerConfiguration) ArrayList(java.util.ArrayList)

Aggregations

DBWHandlerConfiguration (org.jkiss.dbeaver.model.net.DBWHandlerConfiguration)64 DBException (org.jkiss.dbeaver.DBException)20 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)12 DBPConnectionConfiguration (org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration)12 NetworkHandlerDescriptor (org.jkiss.dbeaver.registry.network.NetworkHandlerDescriptor)12 SQLException (java.sql.SQLException)8 DBWNetworkProfile (org.jkiss.dbeaver.model.net.DBWNetworkProfile)8 DBRShellCommand (org.jkiss.dbeaver.model.runtime.DBRShellCommand)8 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)6 SelectionEvent (org.eclipse.swt.events.SelectionEvent)6 GridData (org.eclipse.swt.layout.GridData)6 GridLayout (org.eclipse.swt.layout.GridLayout)6 Nullable (org.jkiss.code.Nullable)6 DBPDataSourceOrigin (org.jkiss.dbeaver.model.DBPDataSourceOrigin)6 DBPDriver (org.jkiss.dbeaver.model.connection.DBPDriver)6 SimplePreferenceStore (org.jkiss.dbeaver.model.impl.preferences.SimplePreferenceStore)6 UIPropertyConfiguratorDescriptor (org.jkiss.dbeaver.registry.configurator.UIPropertyConfiguratorDescriptor)6 TypeToken (com.google.gson.reflect.TypeToken)4 MalformedURLException (java.net.MalformedURLException)4 ArrayList (java.util.ArrayList)4