Search in sources :

Example 6 with DriverDescriptor

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

the class DriverDownloadManualPage method createControl.

@Override
public void createControl(Composite parent) {
    final DriverDescriptor driver = getWizard().getDriver();
    setMessage(NLS.bind(CoreMessages.dialog_driver_download_manual_page_download_config_driver_file, driver.getFullName()));
    Composite composite = UIUtils.createPlaceholder(parent, 1);
    composite.setLayoutData(new GridData(GridData.FILL_BOTH));
    Link infoText = new Link(composite, SWT.NONE);
    infoText.setText(NLS.bind(CoreMessages.dialog_driver_download_manual_page_driver_file_missing_text, driver.getFullName()));
    infoText.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            getWizard().getContainer().buttonPressed(DriverDownloadDialog.EDIT_DRIVER_BUTTON_ID);
        }
    });
    GridData gd = new GridData(GridData.FILL_HORIZONTAL);
    infoText.setLayoutData(gd);
    Group filesGroup = UIUtils.createControlGroup(composite, CoreMessages.dialog_driver_download_manual_page_driver_file, 1, -1, -1);
    gd = new GridData(GridData.FILL_HORIZONTAL);
    gd.verticalIndent = 10;
    filesGroup.setLayoutData(gd);
    final Combo sourceCombo = new Combo(filesGroup, SWT.DROP_DOWN | SWT.READ_ONLY);
    for (DriverFileSource source : driver.getDriverFileSources()) {
        sourceCombo.add(source.getName());
    }
    final Link driverLink = new Link(filesGroup, SWT.NONE);
    driverLink.setText("<a>" + driver.getDriverFileSources().get(0).getUrl() + "</a>");
    driverLink.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    driverLink.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            WebUtils.openWebBrowser(driver.getDriverFileSources().get(sourceCombo.getSelectionIndex()).getUrl());
        }
    });
    filesTable = new Table(filesGroup, SWT.BORDER | SWT.FULL_SELECTION);
    filesTable.setHeaderVisible(true);
    filesTable.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    UIUtils.createTableColumn(filesTable, SWT.LEFT, CoreMessages.dialog_driver_download_manual_page_column_file);
    UIUtils.createTableColumn(filesTable, SWT.LEFT, CoreMessages.dialog_driver_download_manual_page_column_required);
    UIUtils.createTableColumn(filesTable, SWT.LEFT, CoreMessages.dialog_driver_download_manual_page_column_description);
    sourceCombo.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            selectFileSource(driver.getDriverFileSources().get(sourceCombo.getSelectionIndex()));
            driverLink.setText("<a>" + driver.getDriverFileSources().get(sourceCombo.getSelectionIndex()).getUrl() + "</a>");
        }
    });
    sourceCombo.select(0);
    selectFileSource(driver.getDriverFileSources().get(0));
    UIUtils.packColumns(filesTable, true);
    createLinksPanel(composite);
    composite.setTabList(ArrayUtils.remove(Control.class, composite.getTabList(), infoText));
    setControl(composite);
}
Also used : DriverFileSource(org.jkiss.dbeaver.registry.driver.DriverFileSource) DriverDescriptor(org.jkiss.dbeaver.registry.driver.DriverDescriptor) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent)

Example 7 with DriverDescriptor

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

the class DriverManagerDialog method editDriver.

private void editDriver() {
    DriverDescriptor driver = selectedDriver;
    if (driver != null) {
        // driver.validateFilesPresence(this);
        DriverEditDialog dialog = new DriverEditDialog(getShell(), driver);
        if (dialog.open() == IDialogConstants.OK_ID) {
        // Do nothing
        }
        treeControl.getViewer().refresh(driver);
    }
}
Also used : DriverDescriptor(org.jkiss.dbeaver.registry.driver.DriverDescriptor)

Example 8 with DriverDescriptor

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

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 (tmpDriver.getDriverClassName().equals(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) ZipFile(java.util.zip.ZipFile)

Example 9 with DriverDescriptor

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

the class DBeaverInstanceServer method openDatabaseConnection.

@Override
public void openDatabaseConnection(String connectionSpec) throws RemoteException {
    log.debug("Open external database connection [" + connectionSpec + "]");
    final IWorkbenchWindow workbenchWindow = DBeaverUI.getActiveWorkbenchWindow();
    DataSourceRegistry dsRegistry = DBeaverCore.getInstance().getProjectRegistry().getActiveDataSourceRegistry();
    String driverName = null, url = null, host = null, port = null, server = null, database = null, user = null, password = null;
    boolean makeConnect = true, openConsole = false, savePassword = true;
    Boolean autoCommit = null;
    Map<String, String> conProperties = new HashMap<>();
    DBPDataSourceFolder folder = null;
    String dsName = 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 "driver":
                driverName = paramValue;
                break;
            case "name":
                dsName = paramValue;
                break;
            case "url":
                url = paramValue;
                break;
            case "host":
                host = paramValue;
                break;
            case "port":
                port = paramValue;
                break;
            case "server":
                server = paramValue;
                break;
            case "database":
                database = paramValue;
                break;
            case "user":
                user = paramValue;
                break;
            case "password":
                password = paramValue;
                break;
            case "savePassword":
                savePassword = CommonUtils.toBoolean(paramValue);
                break;
            case "connect":
                makeConnect = CommonUtils.toBoolean(paramValue);
                break;
            case "openConsole":
                openConsole = CommonUtils.toBoolean(paramValue);
                break;
            case "folder":
                folder = dsRegistry.getFolder(paramValue);
                break;
            case "autoCommit":
                autoCommit = CommonUtils.toBoolean(paramValue);
                break;
            default:
                if (paramName.length() > 5 && paramName.startsWith("prop.")) {
                    paramName = paramName.substring(5);
                    conProperties.put(paramName, paramValue);
                }
        }
    }
    if (driverName == null) {
        log.error("Driver name not specified");
        return;
    }
    DriverDescriptor driver = DataSourceProviderRegistry.getInstance().findDriver(driverName);
    if (driver == null) {
        log.error("Driver '" + driverName + "' not found");
        return;
    }
    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 (autoCommit != null) {
        connConfig.getBootstrap().setDefaultAutoCommit(autoCommit);
    }
    final DataSourceDescriptor ds = new DataSourceDescriptor(dsRegistry, DataSourceDescriptor.generateNewId(driver), driver, connConfig);
    ds.setName(dsName);
    ds.setTemporary(true);
    if (savePassword) {
        ds.setSavePassword(true);
    }
    if (folder != null) {
        ds.setFolder(folder);
    }
    // ds.set
    dsRegistry.addDataSource(ds);
    if (openConsole) {
        DBeaverUI.syncExec(() -> {
            OpenHandler.openSQLConsole(workbenchWindow, ds, ds.getName(), "");
            workbenchWindow.getShell().forceActive();
        });
    } else if (makeConnect) {
        DataSourceHandler.connectToDataSource(null, ds, null);
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) DriverDescriptor(org.jkiss.dbeaver.registry.driver.DriverDescriptor) HashMap(java.util.HashMap) DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) DataSourceRegistry(org.jkiss.dbeaver.registry.DataSourceRegistry) DBPDataSourceFolder(org.jkiss.dbeaver.model.DBPDataSourceFolder) DataSourceDescriptor(org.jkiss.dbeaver.registry.DataSourceDescriptor)

Example 10 with DriverDescriptor

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

the class DataSourceProviderRegistry method saveDrivers.

public void saveDrivers() {
    File driversConfig = DBeaverActivator.getConfigurationFile(RegistryConstants.DRIVERS_FILE_NAME);
    try {
        OutputStream os = new FileOutputStream(driversConfig);
        XMLBuilder xml = new XMLBuilder(os, GeneralUtils.UTF8_ENCODING);
        xml.setButify(true);
        xml.startElement(RegistryConstants.TAG_DRIVERS);
        for (DataSourceProviderDescriptor provider : this.dataSourceProviders) {
            xml.startElement(RegistryConstants.TAG_PROVIDER);
            xml.addAttribute(RegistryConstants.ATTR_ID, provider.getId());
            for (DriverDescriptor driver : provider.getDrivers()) {
                if (driver.isModified()) {
                    driver.serialize(xml, false);
                }
            }
            xml.endElement();
        }
        xml.endElement();
        xml.flush();
        os.close();
    } catch (Exception ex) {
        log.warn("Error saving drivers", ex);
    }
}
Also used : DriverDescriptor(org.jkiss.dbeaver.registry.driver.DriverDescriptor) XMLBuilder(org.jkiss.utils.xml.XMLBuilder) XMLException(org.jkiss.utils.xml.XMLException)

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