Search in sources :

Example 16 with DataSourceProviderDescriptor

use of org.jkiss.dbeaver.registry.DataSourceProviderDescriptor 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 17 with DataSourceProviderDescriptor

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

the class DriverManagerDialog method createDriver.

private void createDriver() {
    if (onlyManagableProvider != null || selectedProvider != null) {
        DataSourceProviderDescriptor provider = selectedProvider;
        if (provider == null || !provider.isDriversManagable()) {
            provider = onlyManagableProvider;
        }
        DriverEditDialog dialog = new DriverEditDialog(getShell(), provider, selectedCategory);
        if (dialog.open() == IDialogConstants.OK_ID) {
            treeControl.refresh();
            treeControl.setSelection(new StructuredSelection(dialog.getDriver()));
        }
    }
}
Also used : DataSourceProviderDescriptor(org.jkiss.dbeaver.registry.DataSourceProviderDescriptor) DBPDataSourceProviderDescriptor(org.jkiss.dbeaver.model.connection.DBPDataSourceProviderDescriptor)

Example 18 with DataSourceProviderDescriptor

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

the class DriverSelectViewer method collectDrivers.

private Object[] collectDrivers(List<DataSourceProviderDescriptor> provs) {
    List<DBPDriver> drivers = new ArrayList<>();
    if (provs != null) {
        for (DataSourceProviderDescriptor provider : provs) {
            drivers.addAll(provider.getEnabledDrivers());
        }
    }
    drivers.sort((o1, o2) -> {
        return o1.getName().compareToIgnoreCase(o2.getName());
    });
    return drivers.toArray(new Object[0]);
}
Also used : DBPDriver(org.jkiss.dbeaver.model.connection.DBPDriver) ArrayList(java.util.ArrayList) DataSourceProviderDescriptor(org.jkiss.dbeaver.registry.DataSourceProviderDescriptor) DBPDataSourceProviderDescriptor(org.jkiss.dbeaver.model.connection.DBPDataSourceProviderDescriptor)

Aggregations

DataSourceProviderDescriptor (org.jkiss.dbeaver.registry.DataSourceProviderDescriptor)18 DriverDescriptor (org.jkiss.dbeaver.registry.driver.DriverDescriptor)10 GridData (org.eclipse.swt.layout.GridData)5 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)4 SelectionEvent (org.eclipse.swt.events.SelectionEvent)4 DBPDataSourceProviderDescriptor (org.jkiss.dbeaver.model.connection.DBPDataSourceProviderDescriptor)4 DBPDriver (org.jkiss.dbeaver.model.connection.DBPDriver)4 ArrayList (java.util.ArrayList)3 GridLayout (org.eclipse.swt.layout.GridLayout)3 DBException (org.jkiss.dbeaver.DBException)3 DBPConnectionConfiguration (org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration)2 DataSourceDescriptor (org.jkiss.dbeaver.registry.DataSourceDescriptor)2 DataSourceProviderRegistry (org.jkiss.dbeaver.registry.DataSourceProviderRegistry)2 File (java.io.File)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 java.util (java.util)1 HashSet (java.util.HashSet)1 List (java.util.List)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1