Search in sources :

Example 21 with DriverDescriptor

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

the class ProjectExportWizard method exportProjects.

public void exportProjects(DBRProgressMonitor monitor, final ProjectExportData exportData) throws IOException, CoreException, InterruptedException {
    if (!exportData.getOutputFolder().exists()) {
        if (!exportData.getOutputFolder().mkdirs()) {
            // $NON-NLS-2$
            throw new IOException("Cannot create directory '" + exportData.getOutputFolder().getAbsolutePath() + "'");
        }
    }
    String archiveName = exportData.getArchiveFileName() + ExportConstants.ARCHIVE_FILE_EXT;
    File archiveFile = new File(exportData.getOutputFolder(), archiveName);
    FileOutputStream exportStream = new FileOutputStream(archiveFile);
    try {
        ByteArrayOutputStream metaBuffer = new ByteArrayOutputStream(10000);
        ZipOutputStream archiveStream = new ZipOutputStream(exportStream);
        // Start meta
        XMLBuilder meta = new XMLBuilder(metaBuffer, GeneralUtils.UTF8_ENCODING);
        meta.startElement(ExportConstants.TAG_ARCHIVE);
        meta.addAttribute(ExportConstants.ATTR_VERSION, ExportConstants.ARCHIVE_VERSION_CURRENT);
        exportData.initExport(DBeaverCore.getInstance().getProjectRegistry(), meta, archiveStream);
        {
            // Export source info
            meta.startElement(ExportConstants.TAG_SOURCE);
            meta.addAttribute(ExportConstants.ATTR_TIME, Long.valueOf(System.currentTimeMillis()));
            meta.addAttribute(ExportConstants.ATTR_ADDRESS, InetAddress.getLocalHost().getHostAddress());
            meta.addAttribute(ExportConstants.ATTR_HOST, InetAddress.getLocalHost().getHostName());
            meta.endElement();
        }
        Map<IProject, Integer> resCountMap = new HashMap<>();
        monitor.beginTask(CoreMessages.dialog_project_export_wizard_monitor_collect_info, exportData.getProjectsToExport().size());
        for (IProject project : exportData.getProjectsToExport()) {
            // Add used drivers to export data
            final DataSourceRegistry dataSourceRegistry = exportData.projectRegistry.getDataSourceRegistry(project);
            if (dataSourceRegistry != null) {
                for (DataSourceDescriptor dataSourceDescriptor : dataSourceRegistry.getDataSources()) {
                    exportData.usedDrivers.add(dataSourceDescriptor.getDriver());
                }
            }
            resCountMap.put(project, getChildCount(exportData, project));
            monitor.worked(1);
        }
        monitor.done();
        {
            // Export drivers meta
            monitor.beginTask(CoreMessages.dialog_project_export_wizard_monitor_export_driver_info, 1);
            exportData.meta.startElement(RegistryConstants.TAG_DRIVERS);
            for (DriverDescriptor driver : exportData.usedDrivers) {
                driver.serialize(exportData.meta, true);
            }
            exportData.meta.endElement();
            monitor.done();
        }
        {
            // Export projects
            exportData.meta.startElement(ExportConstants.TAG_PROJECTS);
            for (IProject project : exportData.getProjectsToExport()) {
                monitor.beginTask(NLS.bind(CoreMessages.dialog_project_export_wizard_monitor_export_project, project.getName()), resCountMap.get(project));
                try {
                    exportProject(monitor, exportData, project);
                } finally {
                    monitor.done();
                }
            }
            exportData.meta.endElement();
        }
        if (exportData.isExportDrivers()) {
            // Export driver libraries
            Set<File> libFiles = new HashSet<>();
            Map<String, File> libPathMap = new HashMap<>();
            for (DriverDescriptor driver : exportData.usedDrivers) {
                for (DBPDriverLibrary fileDescriptor : driver.getDriverLibraries()) {
                    final File libraryFile = fileDescriptor.getLocalFile();
                    if (libraryFile != null && !fileDescriptor.isDisabled() && libraryFile.exists()) {
                        libFiles.add(libraryFile);
                        libPathMap.put(fileDescriptor.getPath(), libraryFile);
                    }
                }
            }
            if (!libFiles.isEmpty()) {
                monitor.beginTask(CoreMessages.dialog_project_export_wizard_monitor_export_libraries, libFiles.size());
                // $NON-NLS-1$
                final ZipEntry driversFolder = new ZipEntry(ExportConstants.DIR_DRIVERS + "/");
                // $NON-NLS-1$
                driversFolder.setComment("Database driver libraries");
                exportData.archiveStream.putNextEntry(driversFolder);
                exportData.archiveStream.closeEntry();
                exportData.meta.startElement(ExportConstants.TAG_LIBRARIES);
                Set<String> libFileNames = new HashSet<>();
                for (String libPath : libPathMap.keySet()) {
                    final File libFile = libPathMap.get(libPath);
                    // Check for file name duplications
                    final String libFileName = libFile.getName();
                    if (libFileNames.contains(libFileName)) {
                        // $NON-NLS-1$
                        log.warn("Duplicate driver library file name: " + libFileName);
                        continue;
                    }
                    libFileNames.add(libFileName);
                    monitor.subTask(libFileName);
                    exportData.meta.startElement(RegistryConstants.TAG_FILE);
                    exportData.meta.addAttribute(ExportConstants.ATTR_PATH, libPath);
                    // $NON-NLS-1$
                    exportData.meta.addAttribute(ExportConstants.ATTR_FILE, "drivers/" + libFileName);
                    exportData.meta.endElement();
                    // $NON-NLS-1$
                    final ZipEntry driverFile = new ZipEntry(ExportConstants.DIR_DRIVERS + "/" + libFileName);
                    // $NON-NLS-1$
                    driverFile.setComment("Driver library");
                    exportData.archiveStream.putNextEntry(driverFile);
                    try (InputStream is = new FileInputStream(libFile)) {
                        IOUtils.copyStream(is, exportData.archiveStream, COPY_BUFFER_SIZE);
                    }
                    exportData.archiveStream.closeEntry();
                    monitor.worked(1);
                }
                exportData.meta.endElement();
                monitor.done();
            }
        }
        // Add meta to archive
        {
            exportData.meta.endElement();
            exportData.meta.flush();
            archiveStream.putNextEntry(new ZipEntry(ExportConstants.META_FILENAME));
            archiveStream.write(metaBuffer.toByteArray());
            archiveStream.closeEntry();
        }
        // Finish archive creation
        archiveStream.finish();
    } finally {
        ContentUtils.close(exportStream);
    }
}
Also used : DriverDescriptor(org.jkiss.dbeaver.registry.driver.DriverDescriptor) HashMap(java.util.HashMap) ZipEntry(java.util.zip.ZipEntry) XMLBuilder(org.jkiss.utils.xml.XMLBuilder) DataSourceRegistry(org.jkiss.dbeaver.registry.DataSourceRegistry) ZipOutputStream(java.util.zip.ZipOutputStream) DBPDriverLibrary(org.jkiss.dbeaver.model.connection.DBPDriverLibrary) DataSourceDescriptor(org.jkiss.dbeaver.registry.DataSourceDescriptor) HashSet(java.util.HashSet)

Example 22 with DriverDescriptor

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

the class DriverEditDialog method createDialogArea.

@Override
protected Control createDialogArea(Composite parent) {
    if (newDriver) {
        getShell().setText(CoreMessages.dialog_edit_driver_title_create_driver);
    } else {
        // $NON-NLS-2$
        getShell().setText(CoreMessages.dialog_edit_driver_title_edit_driver + driver.getName() + "'");
        getShell().setImage(DBeaverIcons.getImage(driver.getPlainIcon()));
    }
    boolean isReadOnly = !provider.isDriversManagable();
    int advStyle = isReadOnly ? SWT.READ_ONLY : SWT.NONE;
    final Composite group = (Composite) super.createDialogArea(parent);
    GridData gd = new GridData(GridData.FILL_BOTH);
    gd.widthHint = 500;
    group.setLayoutData(gd);
    {
        Group propsGroup = UIUtils.createControlGroup(group, CoreMessages.dialog_edit_driver_setting, 4, -1, -1);
        propsGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        gd = new GridData(GridData.FILL_HORIZONTAL);
        driverNameText = UIUtils.createLabelText(propsGroup, CoreMessages.dialog_edit_driver_label_driver_name + "*", driver.getName(), SWT.BORDER | advStyle, gd);
        driverNameText.addModifyListener(new ModifyListener() {

            @Override
            public void modifyText(ModifyEvent e) {
                onChangeProperty();
            }
        });
        UIUtils.createControlLabel(propsGroup, CoreMessages.dialog_edit_driver_type_label);
        final CSmartCombo<DataSourceProviderDescriptor> providerCombo = new CSmartCombo<>(propsGroup, SWT.BORDER | SWT.READ_ONLY | SWT.DROP_DOWN, new LabelProvider() {

            @Override
            public Image getImage(Object element) {
                return DBeaverIcons.getImage(((DataSourceProviderDescriptor) element).getIcon());
            }

            @Override
            public String getText(Object element) {
                return ((DataSourceProviderDescriptor) element).getName();
            }
        });
        providerCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        if (newDriver) {
            for (DataSourceProviderDescriptor provider : DataSourceProviderRegistry.getInstance().getDataSourceProviders()) {
                if (provider.isDriversManagable()) {
                    providerCombo.addItem(provider);
                }
            }
            providerCombo.select(provider);
            providerCombo.addSelectionListener(new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    provider = providerCombo.getItem(providerCombo.getSelectionIndex());
                    driver = provider.createDriver();
                }
            });
        } else {
            providerCombo.addItem(provider);
            providerCombo.select(provider);
        }
        gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalSpan = 3;
        driverClassText = UIUtils.createLabelText(propsGroup, CoreMessages.dialog_edit_driver_label_class_name + "*", CommonUtils.notEmpty(driver.getDriverClassName()), SWT.BORDER | advStyle, gd);
        driverClassText.addModifyListener(new ModifyListener() {

            @Override
            public void modifyText(ModifyEvent e) {
                onChangeProperty();
            }
        });
        driverURLText = UIUtils.createLabelText(propsGroup, CoreMessages.dialog_edit_driver_label_sample_url, CommonUtils.notEmpty(driver.getSampleURL()), SWT.BORDER | advStyle, gd);
        driverURLText.addModifyListener(new ModifyListener() {

            @Override
            public void modifyText(ModifyEvent e) {
                onChangeProperty();
            }
        });
        gd = new GridData(GridData.FILL_HORIZONTAL);
        driverPortText = UIUtils.createLabelText(propsGroup, CoreMessages.dialog_edit_driver_label_default_port, driver.getDefaultPort() == null ? "" : driver.getDefaultPort(), SWT.BORDER | advStyle, gd);
        driverPortText.setLayoutData(new GridData(SWT.NONE));
        driverPortText.addModifyListener(new ModifyListener() {

            @Override
            public void modifyText(ModifyEvent e) {
                onChangeProperty();
            }
        });
        gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalSpan = 2;
        embeddedDriverCheck = UIUtils.createCheckbox(propsGroup, CoreMessages.dialog_edit_driver_embedded_label, driver.isEmbedded());
        embeddedDriverCheck.setLayoutData(gd);
    }
    {
        Group infoGroup = UIUtils.createControlGroup(group, CoreMessages.dialog_edit_driver_description, 4, -1, -1);
        infoGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        driverCategoryCombo = UIUtils.createLabelCombo(infoGroup, CoreMessages.dialog_edit_driver_label_category, SWT.BORDER | SWT.DROP_DOWN | advStyle);
        driverCategoryCombo.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
        if (isReadOnly) {
            driverCategoryCombo.setEnabled(false);
        }
        Set<String> categories = new TreeSet<>();
        for (DataSourceProviderDescriptor provider : DataSourceProviderRegistry.getInstance().getDataSourceProviders()) {
            for (DriverDescriptor drv : provider.getEnabledDrivers()) {
                if (!CommonUtils.isEmpty(drv.getCategory())) {
                    categories.add(drv.getCategory());
                }
            }
        }
        for (String category : categories) {
            driverCategoryCombo.add(category);
        }
        if (!CommonUtils.isEmpty(driver.getCategory())) {
            driverCategoryCombo.setText(driver.getCategory());
        } else if (!CommonUtils.isEmpty(defaultCategory)) {
            driverCategoryCombo.setText(defaultCategory);
        }
        {
            gd = new GridData(GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING);
            Text idText = UIUtils.createLabelText(infoGroup, "ID", driver.getId(), SWT.BORDER | SWT.READ_ONLY, gd);
            idText.setToolTipText("Internal driver ID.\nCan be used in connections configurations and CLI commands.");
        }
        driverDescText = UIUtils.createLabelText(infoGroup, CoreMessages.dialog_edit_driver_label_description, CommonUtils.notEmpty(driver.getDescription()), SWT.BORDER | advStyle);
        gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalSpan = 3;
        driverDescText.setLayoutData(gd);
        if (!CommonUtils.isEmpty(driver.getWebURL())) {
            UIUtils.createControlLabel(infoGroup, CoreMessages.dialog_edit_driver_label_website);
            Link urlLabel = UIUtils.createLink(infoGroup, "<a>" + driver.getWebURL() + "</a>", new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    UIUtils.launchProgram(driver.getWebURL());
                }
            });
            gd = new GridData(GridData.FILL_HORIZONTAL);
            gd.horizontalSpan = 3;
            driverDescText.setLayoutData(gd);
            urlLabel.setLayoutData(gd);
        }
    }
    {
        TabFolder tabFolder = new TabFolder(group, SWT.NONE);
        tabFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
        if (provider.isDriversManagable()) {
            createLibrariesTab(tabFolder);
        }
        createConnectionPropertiesTab(tabFolder);
        createParametersTab(tabFolder);
        // Client homes
        if (driver.getClientManager() != null) {
            createClientHomesTab(tabFolder);
        }
        final String license = driver.getLicense();
        if (license != null) {
            createLicenseTab(tabFolder, license);
        }
        tabFolder.setSelection(0);
    }
    loadSettings(false);
    if (showAddFiles) {
        getShell().getDisplay().asyncExec(new Runnable() {

            @Override
            public void run() {
                addLibraryFiles();
            }
        });
    }
    driverNameText.setFocus();
    return group;
}
Also used : DriverDescriptor(org.jkiss.dbeaver.registry.driver.DriverDescriptor) ModifyListener(org.eclipse.swt.events.ModifyListener) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) ModifyEvent(org.eclipse.swt.events.ModifyEvent) CSmartCombo(org.jkiss.dbeaver.ui.controls.CSmartCombo) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) DataSourceProviderDescriptor(org.jkiss.dbeaver.registry.DataSourceProviderDescriptor)

Example 23 with DriverDescriptor

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

the class DriverTreeViewer method collectDrivers.

private Collection<Object> collectDrivers() {
    for (DriverCategory category : categories.values()) {
        category.drivers.clear();
    }
    driverList.clear();
    for (DataSourceProviderDescriptor provider : providers) {
        List<DriverDescriptor> drivers = provider.getEnabledDrivers();
        for (DriverDescriptor driver : drivers) {
            String category = driver.getCategory();
            if (CommonUtils.isEmpty(category)) {
                driverList.add(driver);
            } else {
                DriverCategory driverCategory = categories.get(category);
                if (driverCategory == null) {
                    driverCategory = new DriverCategory(category);
                    categories.put(category, driverCategory);
                }
                if (!driverList.contains(driverCategory)) {
                    driverList.add(driverCategory);
                }
                driverCategory.drivers.add(driver);
            }
        }
    }
    Collections.sort(driverList, (o1, o2) -> {
        int count1 = getConnectionCount(o1);
        int count2 = getConnectionCount(o2);
        if (sortByName || count1 == count2) {
            String name1 = o1 instanceof DriverDescriptor ? ((DriverDescriptor) o1).getName() : ((DriverCategory) o1).getName();
            String name2 = o2 instanceof DriverDescriptor ? ((DriverDescriptor) o2).getName() : ((DriverCategory) o2).getName();
            return name1.compareToIgnoreCase(name2);
        } else {
            return count2 - count1;
        }
    });
    for (DriverCategory category : categories.values()) {
        category.drivers.sort((o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName()));
    }
    return driverList;
}
Also used : DriverDescriptor(org.jkiss.dbeaver.registry.driver.DriverDescriptor) DataSourceProviderDescriptor(org.jkiss.dbeaver.registry.DataSourceProviderDescriptor)

Example 24 with DriverDescriptor

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

the class DatabaseTab method extractDriverImage.

protected DBPImage extractDriverImage() {
    if (driverText == null || driverText.isDisposed()) {
        return null;
    }
    String driverName = driverText.getText();
    DriverDescriptor driver = DataSourceProviderRegistry.getInstance().findDriver(driverName);
    if (driver == null) {
        return null;
    }
    return driver.getIcon();
}
Also used : DriverDescriptor(org.jkiss.dbeaver.registry.driver.DriverDescriptor)

Example 25 with DriverDescriptor

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

the class ConnectionWizard method getActiveDataSource.

@NotNull
public DataSourceDescriptor getActiveDataSource() {
    DriverDescriptor driver = getSelectedDriver();
    DataSourceDescriptor info = infoMap.get(driver);
    if (info == null) {
        DBPConnectionConfiguration connectionInfo = new DBPConnectionConfiguration();
        info = new DataSourceDescriptor(getDataSourceRegistry(), DataSourceDescriptor.generateNewId(getSelectedDriver()), getSelectedDriver(), connectionInfo);
        info.getConnectionConfiguration().setClientHomeId(driver.getDefaultClientHomeId());
        infoMap.put(driver, info);
    }
    return info;
}
Also used : DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) DriverDescriptor(org.jkiss.dbeaver.registry.driver.DriverDescriptor) DataSourceDescriptor(org.jkiss.dbeaver.registry.DataSourceDescriptor) NotNull(org.jkiss.code.NotNull)

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