Search in sources :

Example 31 with DriverDescriptor

use of org.jkiss.dbeaver.registry.driver.DriverDescriptor in project dbeaver by serge-rider.

the class ProjectImportWizard method importDriver.

private DriverDescriptor importDriver(DBRProgressMonitor monitor, Element driverElement, ZipFile zipFile, Map<String, String> libMap, Map<String, String> driverMap) throws IOException, DBException {
    String providerId = driverElement.getAttribute(RegistryConstants.ATTR_PROVIDER);
    String driverId = driverElement.getAttribute(RegistryConstants.ATTR_ID);
    boolean isCustom = CommonUtils.getBoolean(driverElement.getAttribute(RegistryConstants.ATTR_CUSTOM));
    String driverCategory = driverElement.getAttribute(RegistryConstants.ATTR_CATEGORY);
    String driverName = driverElement.getAttribute(RegistryConstants.ATTR_NAME);
    String driverClass = driverElement.getAttribute(RegistryConstants.ATTR_CLASS);
    String driverURL = driverElement.getAttribute(RegistryConstants.ATTR_URL);
    String driverDefaultPort = driverElement.getAttribute(RegistryConstants.ATTR_PORT);
    String driverDescription = driverElement.getAttribute(RegistryConstants.ATTR_DESCRIPTION);
    DataSourceProviderDescriptor dataSourceProvider = DataSourceProviderRegistry.getInstance().getDataSourceProvider(providerId);
    if (dataSourceProvider == null) {
        throw new DBException("Cannot find data source provider '" + providerId + "' for driver '" + driverName + "'");
    }
    monitor.subTask(CoreMessages.dialog_project_import_wizard_monitor_load_driver + driverName);
    DriverDescriptor driver = null;
    if (!isCustom) {
        // Get driver by ID
        driver = dataSourceProvider.getDriver(driverId);
        if (driver == null) {
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            log.warn("Driver '" + driverId + "' not found in data source provider '" + dataSourceProvider.getName() + "'");
        }
    }
    if (driver == null) {
        // Try to find existing driver by class name
        List<DriverDescriptor> matchedDrivers = new ArrayList<>();
        for (DriverDescriptor tmpDriver : dataSourceProvider.getEnabledDrivers()) {
            if (CommonUtils.equalObjects(tmpDriver.getDriverClassName(), driverClass)) {
                matchedDrivers.add(tmpDriver);
            }
        }
        if (matchedDrivers.size() == 1) {
            driver = matchedDrivers.get(0);
        } else if (!matchedDrivers.isEmpty()) {
            // Multiple drivers with the same class - tru to find driver with the same sample URL or with the same name
            for (DriverDescriptor tmpDriver : matchedDrivers) {
                if (CommonUtils.equalObjects(tmpDriver.getSampleURL(), driverURL) || CommonUtils.equalObjects(tmpDriver.getName(), driverName)) {
                    driver = tmpDriver;
                    break;
                }
            }
            if (driver == null) {
                // Not found - lets use first one
                // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                log.warn("Ambiguous driver '" + driverName + "' - multiple drivers with class '" + driverClass + "' found. First one will be used");
                driver = matchedDrivers.get(0);
            }
        }
    }
    if (driver == null) {
        // Create new driver
        driver = dataSourceProvider.createDriver();
        driver.setName(driverName);
        driver.setCategory(driverCategory);
        driver.setDescription(driverDescription);
        driver.setDriverClassName(driverClass);
        if (!CommonUtils.isEmpty(driverDefaultPort)) {
            driver.setDriverDefaultPort(driverDefaultPort);
        }
        driver.setSampleURL(driverURL);
        driver.setModified(true);
        dataSourceProvider.addDriver(driver);
    }
    // Parameters and properties
    for (Element libElement : XMLUtils.getChildElementList(driverElement, RegistryConstants.TAG_PARAMETER)) {
        driver.setDriverParameter(libElement.getAttribute(RegistryConstants.ATTR_NAME), libElement.getAttribute(RegistryConstants.ATTR_VALUE), false);
    }
    for (Element libElement : XMLUtils.getChildElementList(driverElement, RegistryConstants.TAG_PROPERTY)) {
        driver.setConnectionProperty(libElement.getAttribute(RegistryConstants.ATTR_NAME), libElement.getAttribute(RegistryConstants.ATTR_VALUE));
    }
    // Add libraries (only for managable drivers with empty library list)
    if (CommonUtils.isEmpty(driver.getDriverLibraries())) {
        List<String> libraryList = new ArrayList<>();
        for (Element libElement : XMLUtils.getChildElementList(driverElement, RegistryConstants.TAG_FILE)) {
            libraryList.add(libElement.getAttribute(RegistryConstants.ATTR_PATH));
        }
        for (String libPath : libraryList) {
            File libFile = new File(libPath);
            if (libFile.exists()) {
                // Just use path as-is (may be it is local re-import or local environments equal to export environment)
                driver.addDriverLibrary(libPath, DBPDriverLibrary.FileType.jar);
            } else {
                // Get driver library from archive
                String archiveLibEntry = libMap.get(libPath);
                if (archiveLibEntry != null) {
                    ZipEntry libEntry = zipFile.getEntry(archiveLibEntry);
                    if (libEntry != null) {
                        // Extract driver to "drivers" folder
                        String libName = libFile.getName();
                        File contribFolder = DriverDescriptor.getDriversContribFolder();
                        if (!contribFolder.exists()) {
                            if (!contribFolder.mkdir()) {
                                // $NON-NLS-1$ //$NON-NLS-2$
                                log.error("Cannot create drivers folder '" + contribFolder.getAbsolutePath() + "'");
                                continue;
                            }
                        }
                        File importLibFile = new File(contribFolder, libName);
                        if (!importLibFile.exists()) {
                            try (FileOutputStream os = new FileOutputStream(importLibFile)) {
                                try (InputStream is = zipFile.getInputStream(libEntry)) {
                                    IOUtils.copyStream(is, os);
                                }
                            }
                        }
                        // Make relative path
                        String contribPath = contribFolder.getAbsolutePath();
                        String libAbsolutePath = importLibFile.getAbsolutePath();
                        String relativePath = libAbsolutePath.substring(contribPath.length());
                        while (relativePath.charAt(0) == '/' || relativePath.charAt(0) == '\\') {
                            relativePath = relativePath.substring(1);
                        }
                        driver.addDriverLibrary(relativePath, DBPDriverLibrary.FileType.jar);
                    }
                }
            }
        }
    }
    // Update driver map
    driverMap.put(driverId, driver.getId());
    return driver;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DriverDescriptor(org.jkiss.dbeaver.registry.driver.DriverDescriptor) Element(org.w3c.dom.Element) ZipEntry(java.util.zip.ZipEntry) DataSourceProviderDescriptor(org.jkiss.dbeaver.registry.DataSourceProviderDescriptor) ZipFile(java.util.zip.ZipFile)

Example 32 with DriverDescriptor

use of org.jkiss.dbeaver.registry.driver.DriverDescriptor in project dbeaver by serge-rider.

the class NewConnectionWizard method performFinish.

/**
 * This method is called when 'Finish' button is pressed in
 * the wizard. We will create an operation and run it
 * using wizard as execution context.
 */
@Override
public boolean performFinish() {
    DriverDescriptor driver = (DriverDescriptor) getSelectedDriver();
    ConnectionPageSettings pageSettings = getPageSettings();
    DataSourceDescriptor dataSourceTpl = pageSettings == null ? getActiveDataSource() : pageSettings.getActiveDataSource();
    DBPDataSourceRegistry dataSourceRegistry = getDataSourceRegistry();
    DataSourceDescriptor dataSourceNew = new DataSourceDescriptor(dataSourceRegistry, dataSourceTpl.getId(), driver, dataSourceTpl.getConnectionConfiguration());
    dataSourceNew.copyFrom(dataSourceTpl);
    saveSettings(dataSourceNew);
    dataSourceRegistry.addDataSource(dataSourceNew);
    return true;
}
Also used : DriverDescriptor(org.jkiss.dbeaver.registry.driver.DriverDescriptor) DBPDataSourceRegistry(org.jkiss.dbeaver.model.app.DBPDataSourceRegistry) DataSourceDescriptor(org.jkiss.dbeaver.registry.DataSourceDescriptor)

Example 33 with DriverDescriptor

use of org.jkiss.dbeaver.registry.driver.DriverDescriptor in project dbeaver by serge-rider.

the class DriverManagerDialog method undeleteDrivers.

private boolean undeleteDrivers() {
    List<DriverDescriptor> drivers = new ArrayList<>();
    BaseDialog dialog = new BaseDialog(getShell(), "Restore deleted driver(s)", null) {

        @Override
        protected Composite createDialogArea(Composite parent) {
            final Composite composite = super.createDialogArea(parent);
            Table driverTable = new Table(composite, SWT.CHECK | SWT.FULL_SELECTION | SWT.BORDER);
            driverTable.setLayoutData(new GridData(GridData.FILL_BOTH));
            for (DBPDataSourceProviderDescriptor dspd : DataSourceProviderRegistry.getInstance().getEnabledDataSourceProviders()) {
                for (DBPDriver dd : dspd.getDrivers()) {
                    if (dd.isDisabled()) {
                        TableItem item = new TableItem(driverTable, SWT.NONE);
                        item.setImage(DBeaverIcons.getImage(dd.getIcon()));
                        item.setText(dd.getName());
                        item.setData(dd);
                    }
                }
            }
            driverTable.addSelectionListener(new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    if (((TableItem) e.item).getChecked()) {
                        drivers.add((DriverDescriptor) e.item.getData());
                    } else {
                        drivers.remove((DriverDescriptor) e.item.getData());
                    }
                }
            });
            return super.createDialogArea(parent);
        }
    };
    if (dialog.open() == IDialogConstants.OK_ID) {
        for (DriverDescriptor dd : drivers) {
            dd.setDisabled(false);
            dd.getProviderDescriptor().getRegistry().saveDrivers();
        }
        return true;
    }
    return false;
}
Also used : BaseDialog(org.jkiss.dbeaver.ui.dialogs.BaseDialog) DriverDescriptor(org.jkiss.dbeaver.registry.driver.DriverDescriptor) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) ArrayList(java.util.ArrayList) DBPDriver(org.jkiss.dbeaver.model.connection.DBPDriver) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) DBPDataSourceProviderDescriptor(org.jkiss.dbeaver.model.connection.DBPDataSourceProviderDescriptor)

Example 34 with DriverDescriptor

use of org.jkiss.dbeaver.registry.driver.DriverDescriptor in project dbeaver by serge-rider.

the class DriverDownloadAutoPage method downloadLibraryFiles.

private void downloadLibraryFiles(final DBRProgressMonitor monitor) throws InterruptedException {
    if (!acceptDriverLicenses()) {
        return;
    }
    boolean processUnsecure = false;
    List<DBPDriverDependencies.DependencyNode> nodes = getWizard().getDependencies().getLibraryList();
    for (int i = 0, filesSize = nodes.size(); i < filesSize; ) {
        final DBPDriverLibrary lib = nodes.get(i).library;
        if (!processUnsecure && !lib.isSecureDownload(monitor)) {
            boolean process = new UIConfirmation() {

                @Override
                protected Boolean runTask() {
                    MessageBox messageBox = new MessageBox(getShell(), SWT.ICON_WARNING | SWT.YES | SWT.NO);
                    messageBox.setText(UIConnectionMessages.dialog_driver_download_auto_page_driver_security_warning);
                    messageBox.setMessage(NLS.bind(UIConnectionMessages.dialog_driver_download_auto_page_driver_security_warning_msg, lib.getDisplayName(), lib.getExternalURL(monitor)));
                    int response = messageBox.open();
                    return (response == SWT.YES);
                }
            }.execute();
            if (process) {
                processUnsecure = true;
            } else {
                break;
            }
        }
        int result = IDialogConstants.OK_ID;
        try {
            lib.downloadLibraryFile(monitor, getWizard().isForceDownload(), NLS.bind(UIConnectionMessages.dialog_driver_download_auto_page_download_rate, (i + 1), filesSize));
        } catch (final IOException e) {
            if (lib.getType() == DBPDriverLibrary.FileType.license) {
                result = IDialogConstants.OK_ID;
            } else {
                result = new UITask<Integer>() {

                    @Override
                    protected Integer runTask() {
                        DownloadErrorDialog dialog = new DownloadErrorDialog(null, lib.getDisplayName(), UIConnectionMessages.dialog_driver_download_auto_page_download_failed_msg, e);
                        return dialog.open();
                    }
                }.execute();
            }
        }
        switch(result) {
            case IDialogConstants.CANCEL_ID:
            case IDialogConstants.ABORT_ID:
                return;
            case IDialogConstants.RETRY_ID:
                continue;
            case IDialogConstants.OK_ID:
            case IDialogConstants.IGNORE_ID:
                i++;
                break;
        }
    }
    ((DriverDescriptor) getWizard().getDriver()).setModified(true);
// DataSourceProviderRegistry.getInstance().saveDrivers();
}
Also used : DriverDescriptor(org.jkiss.dbeaver.registry.driver.DriverDescriptor) UIConfirmation(org.jkiss.dbeaver.ui.UIConfirmation) IOException(java.io.IOException) DBPDriverLibrary(org.jkiss.dbeaver.model.connection.DBPDriverLibrary)

Example 35 with DriverDescriptor

use of org.jkiss.dbeaver.registry.driver.DriverDescriptor in project dbeaver by serge-rider.

the class DriverEditDialog method okPressed.

@Override
protected void okPressed() {
    // Set props
    driver.setName(driverNameText.getText());
    driver.setCategory(driverCategoryCombo.getText());
    driver.setDescription(CommonUtils.notEmpty(driverDescText.getText()));
    driver.setDriverClassName(driverClassText.getText());
    driver.setSampleURL(driverURLText.getText());
    driver.setDriverDefaultPort(driverPortText.getText());
    driver.setDriverDefaultDatabase(driverDatabaseText.getText());
    driver.setDriverDefaultUser(driverUserText.getText());
    driver.setEmbedded(embeddedDriverCheck.getSelection());
    driver.setAnonymousAccess(anonymousDriverCheck.getSelection());
    driver.setAllowsEmptyPassword(allowsEmptyPasswordCheck.getSelection());
    driver.setInstantiable(!nonInstantiableCheck.getSelection());
    // driver.setAnonymousAccess(anonymousCheck.getSelection());
    driver.setModified(true);
    driver.setDriverParameters(driverPropertySource.getPropertiesWithDefaults());
    driver.setConnectionProperties(connectionPropertySource.getPropertyValues());
    // Store client homes
    if (clientHomesPanel != null) {
        driver.setNativeClientLocations(clientHomesPanel.getLocalLocations());
    }
    DriverDescriptor oldDriver = provider.getDriverByName(driver.getCategory(), driver.getName());
    if (oldDriver != null && oldDriver != driver && !oldDriver.isDisabled() && oldDriver.getReplacedBy() == null) {
        UIUtils.showMessageBox(getShell(), UIConnectionMessages.dialog_edit_driver_dialog_save_exists_title, NLS.bind(UIConnectionMessages.dialog_edit_driver_dialog_save_exists_message, driver.getName()), SWT.ICON_ERROR);
        return;
    }
    // Finish
    if (provider.getDriver(driver.getId()) == null) {
        provider.addDriver(driver);
    }
    provider.getRegistry().saveDrivers();
    super.okPressed();
}
Also used : DriverDescriptor(org.jkiss.dbeaver.registry.driver.DriverDescriptor)

Aggregations

DriverDescriptor (org.jkiss.dbeaver.registry.driver.DriverDescriptor)37 DataSourceDescriptor (org.jkiss.dbeaver.registry.DataSourceDescriptor)11 DataSourceProviderDescriptor (org.jkiss.dbeaver.registry.DataSourceProviderDescriptor)9 DBException (org.jkiss.dbeaver.DBException)7 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)5 SelectionEvent (org.eclipse.swt.events.SelectionEvent)5 GridData (org.eclipse.swt.layout.GridData)5 DBPConnectionConfiguration (org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration)5 ArrayList (java.util.ArrayList)4 DBPDataSourceRegistry (org.jkiss.dbeaver.model.app.DBPDataSourceRegistry)4 DBPDriver (org.jkiss.dbeaver.model.connection.DBPDriver)4 ZipEntry (java.util.zip.ZipEntry)3 NotNull (org.jkiss.code.NotNull)3 DBPDriverLibrary (org.jkiss.dbeaver.model.connection.DBPDriverLibrary)3 DBWHandlerConfiguration (org.jkiss.dbeaver.model.net.DBWHandlerConfiguration)3 TypeToken (com.google.gson.reflect.TypeToken)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 ZipFile (java.util.zip.ZipFile)2 Composite (org.eclipse.swt.widgets.Composite)2