Search in sources :

Example 11 with NetworkHandlerDescriptor

use of org.jkiss.dbeaver.registry.network.NetworkHandlerDescriptor in project dbeaver by serge-rider.

the class PrefPageProjectNetworkProfiles method createContents.

@Override
protected Control createContents(final Composite parent) {
    CustomSashForm divider = UIUtils.createPartDivider(null, parent, SWT.HORIZONTAL);
    {
        Composite profilesGroup = new Composite(divider, SWT.BORDER);
        GridLayout gl = new GridLayout(1, false);
        gl.marginWidth = 0;
        gl.marginHeight = 0;
        profilesGroup.setLayout(gl);
        GridData gd = new GridData(GridData.FILL_BOTH);
        profilesGroup.setLayoutData(gd);
        {
            ToolBar toolbar = new ToolBar(profilesGroup, SWT.HORIZONTAL | SWT.RIGHT);
            UIUtils.createToolItem(toolbar, "Create", "Create new profile", UIIcon.ROW_ADD, new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    String profileName = "";
                    while (true) {
                        profileName = EnterNameDialog.chooseName(getShell(), "Profile name", profileName);
                        if (CommonUtils.isEmptyTrimmed(profileName)) {
                            return;
                        }
                        if (projectMeta.getDataSourceRegistry().getNetworkProfile(profileName) != null) {
                            UIUtils.showMessageBox(getShell(), "Wrong profile name", "Profile '" + profileName + "' already exist in project '" + projectMeta.getName() + "'", SWT.ICON_ERROR);
                            continue;
                        }
                        break;
                    }
                    DBWNetworkProfile newProfile = new DBWNetworkProfile();
                    newProfile.setProfileName(profileName);
                    projectMeta.getDataSourceRegistry().updateNetworkProfile(newProfile);
                    projectMeta.getDataSourceRegistry().flushConfig();
                    TableItem item = new TableItem(profilesTable, SWT.NONE);
                    item.setText(newProfile.getProfileName());
                    item.setImage(DBeaverIcons.getImage(DBIcon.TYPE_DOCUMENT));
                    item.setData(newProfile);
                    if (profilesTable.getItemCount() == 1) {
                        selectedProfile = newProfile;
                        profilesTable.select(0);
                        updateControlsState();
                    }
                }
            });
            UIUtils.createToolItem(toolbar, "Delete", "Delete profile", UIIcon.ROW_DELETE, new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    if (selectedProfile != null) {
                        List<? extends DBPDataSourceContainer> usedBy = projectMeta.getDataSourceRegistry().getDataSourcesByProfile(selectedProfile);
                        if (!usedBy.isEmpty()) {
                            UIUtils.showMessageBox(getShell(), "Can't delete profile", "Configuration profile '" + selectedProfile.getProfileName() + "' used by " + usedBy.size() + " connections:\n" + usedBy, SWT.ICON_ERROR);
                            return;
                        }
                        if (!UIUtils.confirmAction(getShell(), "Delete profile", "Are you sure you want to delete configuration profile '" + selectedProfile.getProfileName() + "'?")) {
                            return;
                        }
                        projectMeta.getDataSourceRegistry().removeNetworkProfile(selectedProfile);
                        projectMeta.getDataSourceRegistry().flushConfig();
                        profilesTable.remove(profilesTable.getSelectionIndex());
                        selectedProfile = null;
                        updateControlsState();
                    } else {
                        UIUtils.showMessageBox(getShell(), "No profile", "Select profile first", SWT.ICON_ERROR);
                    }
                }
            });
        }
        profilesTable = new Table(profilesGroup, SWT.SINGLE);
        gd = new GridData(GridData.FILL_BOTH);
        gd.minimumWidth = 150;
        profilesTable.setLayoutData(gd);
        profilesTable.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                saveHandlerSettings();
                TableItem[] selection = profilesTable.getSelection();
                if (ArrayUtils.isEmpty(selection)) {
                    selectedProfile = null;
                } else {
                    selectedProfile = (DBWNetworkProfile) selection[0].getData();
                }
                updateControlsState();
            }
        });
    }
    {
        handlersFolder = new TabFolder(divider, SWT.TOP | SWT.FLAT);
        handlersFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
        for (NetworkHandlerDescriptor nhd : NetworkHandlerRegistry.getInstance().getDescriptors()) {
            if (!nhd.hasObjectTypes()) {
                createHandlerTab(nhd);
            }
        }
        handlersFolder.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                updateControlsState();
            }
        });
    }
    divider.setWeights(new int[] { 300, 700 });
    performDefaults();
    return divider;
}
Also used : DBWNetworkProfile(org.jkiss.dbeaver.model.net.DBWNetworkProfile) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) NetworkHandlerDescriptor(org.jkiss.dbeaver.registry.network.NetworkHandlerDescriptor) GridLayout(org.eclipse.swt.layout.GridLayout) CustomSashForm(org.jkiss.dbeaver.ui.controls.CustomSashForm) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) List(java.util.List) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Example 12 with NetworkHandlerDescriptor

use of org.jkiss.dbeaver.registry.network.NetworkHandlerDescriptor in project dbeaver by serge-rider.

the class PrefPageProjectNetworkProfiles method saveHandlerSettings.

/**
 * Saves state of UI controls to handler configuration
 */
private void saveHandlerSettings() {
    if (selectedProfile == null) {
        return;
    }
    for (TabItem handlerTab : handlersFolder.getItems()) {
        NetworkHandlerDescriptor handler = (NetworkHandlerDescriptor) handlerTab.getData();
        HandlerBlock handlerBlock = configurations.get(handler);
        DBWHandlerConfiguration handlerConfiguration = handlerBlock.loadedConfigs.get(selectedProfile);
        if (handlerBlock.useHandlerCheck.getSelection()) {
            if (handlerConfiguration == null) {
                handlerConfiguration = new DBWHandlerConfiguration(handler, null);
            }
            handlerConfiguration.setProperties(Collections.emptyMap());
            handlerBlock.configurator.saveSettings(handlerConfiguration);
        }
    }
}
Also used : DBWHandlerConfiguration(org.jkiss.dbeaver.model.net.DBWHandlerConfiguration) NetworkHandlerDescriptor(org.jkiss.dbeaver.registry.network.NetworkHandlerDescriptor)

Example 13 with NetworkHandlerDescriptor

use of org.jkiss.dbeaver.registry.network.NetworkHandlerDescriptor in project dbeaver by serge-rider.

the class DataSourceSerializerModern method parseNetworkHandlerConfig.

@Nullable
private DBWHandlerConfiguration parseNetworkHandlerConfig(@Nullable DataSourceDescriptor dataSource, @Nullable DBWNetworkProfile profile, @NotNull Map.Entry<String, Map<String, Object>> handlerObject) {
    String handlerId = handlerObject.getKey();
    Map<String, Object> handlerCfg = handlerObject.getValue();
    NetworkHandlerDescriptor handlerDescriptor = NetworkHandlerRegistry.getInstance().getDescriptor(handlerId);
    if (handlerDescriptor == null) {
        log.warn("Can't find network handler '" + handlerId + "'");
        return null;
    } else {
        DBWHandlerConfiguration curNetworkHandler = new DBWHandlerConfiguration(handlerDescriptor, dataSource);
        curNetworkHandler.setEnabled(JSONUtils.getBoolean(handlerCfg, RegistryConstants.ATTR_ENABLED));
        curNetworkHandler.setSavePassword(JSONUtils.getBoolean(handlerCfg, RegistryConstants.ATTR_SAVE_PASSWORD));
        if (!passwordReadCanceled) {
            final SecureCredentials creds = readSecuredCredentials(dataSource, profile, "network/" + handlerId + (profile == null ? "" : "/profile/" + profile.getProfileName()));
            curNetworkHandler.setUserName(creds.getUserName());
            if (curNetworkHandler.isSavePassword()) {
                curNetworkHandler.setPassword(creds.getUserPassword());
            }
        }
        {
            // Still try to read credentials directly from configuration (#6564)
            String userName = JSONUtils.getString(handlerCfg, RegistryConstants.ATTR_USER);
            if (!CommonUtils.isEmpty(userName))
                curNetworkHandler.setUserName(userName);
            String userPassword = JSONUtils.getString(handlerCfg, RegistryConstants.ATTR_PASSWORD);
            if (!CommonUtils.isEmpty(userPassword))
                curNetworkHandler.setPassword(userPassword);
        }
        Map<String, Object> properties = JSONUtils.deserializeProperties(handlerCfg, RegistryConstants.TAG_PROPERTIES);
        if (properties != null) {
            curNetworkHandler.setProperties(properties);
        }
        return curNetworkHandler;
    }
}
Also used : DBWHandlerConfiguration(org.jkiss.dbeaver.model.net.DBWHandlerConfiguration) NetworkHandlerDescriptor(org.jkiss.dbeaver.registry.network.NetworkHandlerDescriptor) Nullable(org.jkiss.code.Nullable)

Example 14 with NetworkHandlerDescriptor

use of org.jkiss.dbeaver.registry.network.NetworkHandlerDescriptor in project dbeaver by dbeaver.

the class DataSourceSerializerModern method parseNetworkHandlerConfig.

@Nullable
private DBWHandlerConfiguration parseNetworkHandlerConfig(@Nullable DataSourceDescriptor dataSource, @Nullable DBWNetworkProfile profile, @NotNull Map.Entry<String, Map<String, Object>> handlerObject) {
    String handlerId = handlerObject.getKey();
    Map<String, Object> handlerCfg = handlerObject.getValue();
    NetworkHandlerDescriptor handlerDescriptor = NetworkHandlerRegistry.getInstance().getDescriptor(handlerId);
    if (handlerDescriptor == null) {
        log.warn("Can't find network handler '" + handlerId + "'");
        return null;
    } else {
        DBWHandlerConfiguration curNetworkHandler = new DBWHandlerConfiguration(handlerDescriptor, dataSource);
        curNetworkHandler.setEnabled(JSONUtils.getBoolean(handlerCfg, RegistryConstants.ATTR_ENABLED));
        curNetworkHandler.setSavePassword(JSONUtils.getBoolean(handlerCfg, RegistryConstants.ATTR_SAVE_PASSWORD));
        if (!passwordReadCanceled) {
            final SecureCredentials creds = readSecuredCredentials(dataSource, profile, "network/" + handlerId + (profile == null ? "" : "/profile/" + profile.getProfileName()));
            curNetworkHandler.setUserName(creds.getUserName());
            if (curNetworkHandler.isSavePassword()) {
                curNetworkHandler.setPassword(creds.getUserPassword());
            }
        }
        {
            // Still try to read credentials directly from configuration (#6564)
            String userName = JSONUtils.getString(handlerCfg, RegistryConstants.ATTR_USER);
            if (!CommonUtils.isEmpty(userName))
                curNetworkHandler.setUserName(userName);
            String userPassword = JSONUtils.getString(handlerCfg, RegistryConstants.ATTR_PASSWORD);
            if (!CommonUtils.isEmpty(userPassword))
                curNetworkHandler.setPassword(userPassword);
        }
        Map<String, Object> properties = JSONUtils.deserializeProperties(handlerCfg, RegistryConstants.TAG_PROPERTIES);
        if (properties != null) {
            curNetworkHandler.setProperties(properties);
        }
        return curNetworkHandler;
    }
}
Also used : DBWHandlerConfiguration(org.jkiss.dbeaver.model.net.DBWHandlerConfiguration) NetworkHandlerDescriptor(org.jkiss.dbeaver.registry.network.NetworkHandlerDescriptor) Nullable(org.jkiss.code.Nullable)

Aggregations

NetworkHandlerDescriptor (org.jkiss.dbeaver.registry.network.NetworkHandlerDescriptor)14 DBWHandlerConfiguration (org.jkiss.dbeaver.model.net.DBWHandlerConfiguration)10 Nullable (org.jkiss.code.Nullable)4 DBWNetworkProfile (org.jkiss.dbeaver.model.net.DBWNetworkProfile)4 List (java.util.List)2 IDialogPage (org.eclipse.jface.dialogs.IDialogPage)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 GridData (org.eclipse.swt.layout.GridData)2 GridLayout (org.eclipse.swt.layout.GridLayout)2 TabItem (org.eclipse.swt.widgets.TabItem)2 DBException (org.jkiss.dbeaver.DBException)2 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)2 DBPDriver (org.jkiss.dbeaver.model.connection.DBPDriver)2 DataSourceDescriptor (org.jkiss.dbeaver.registry.DataSourceDescriptor)2 NetworkHandlerRegistry (org.jkiss.dbeaver.registry.network.NetworkHandlerRegistry)2 CustomSashForm (org.jkiss.dbeaver.ui.controls.CustomSashForm)2