Search in sources :

Example 1 with ImportDriverInfo

use of org.jkiss.dbeaver.ext.import_config.wizards.ImportDriverInfo in project dbeaver by serge-rider.

the class ConfigImportWizardPageCustomConnections method loadConnections.

@Override
protected void loadConnections(ImportData importData) throws DBException {
    setErrorMessage(null);
    ConfigImportWizardCustom wizard = (ConfigImportWizardCustom) getWizard();
    final DBPDriver driver = wizard.getDriver();
    ImportDriverInfo driverInfo = new ImportDriverInfo(driver.getId(), driver.getName(), driver.getSampleURL(), driver.getDriverClassName());
    importData.addDriver(driverInfo);
    File inputFile = wizard.getInputFile();
    try (InputStream is = new FileInputStream(inputFile)) {
        try (Reader reader = new InputStreamReader(is, wizard.getInputFileEncoding())) {
            if (wizard.getImportType() == ConfigImportWizardCustom.ImportType.CSV) {
                importCSV(importData, driverInfo, reader);
            } else {
                importXML(importData, driverInfo, reader);
            }
        }
    } catch (Exception e) {
        setErrorMessage(e.getMessage());
    }
}
Also used : DBPDriver(org.jkiss.dbeaver.model.connection.DBPDriver) ImportDriverInfo(org.jkiss.dbeaver.ext.import_config.wizards.ImportDriverInfo) CSVReader(au.com.bytecode.opencsv.CSVReader) XMLException(org.jkiss.utils.xml.XMLException) DBException(org.jkiss.dbeaver.DBException)

Example 2 with ImportDriverInfo

use of org.jkiss.dbeaver.ext.import_config.wizards.ImportDriverInfo in project dbeaver by serge-rider.

the class ConfigImportWizardPageDbvis method loadConnections.

@Override
protected void loadConnections(ImportData importData) throws DBException {
    File homeFolder = RuntimeUtils.getUserHomeDir();
    File dbvisConfigHome = new File(homeFolder, DBVIS_HOME_FOLDER);
    if (!dbvisConfigHome.exists()) {
        throw new DBException("DBVisualizer installation not found");
    }
    File configFolder = new File(dbvisConfigHome, DBVIS_CONFIG70_FOLDER);
    if (!configFolder.exists()) {
        throw new DBException("Only DBVisualizer 7.x version is supported");
    }
    File configFile = new File(configFolder, DBVIS_CONFIG_FILE);
    if (!configFile.exists()) {
        throw new DBException("DBVisualizer configuration file not found");
    }
    try {
        Document configDocument = XMLUtils.parseDocument(configFile);
        Element driversElement = XMLUtils.getChildElement(configDocument.getDocumentElement(), "Drivers");
        if (driversElement != null) {
            for (Element driverElement : XMLUtils.getChildElementList(driversElement, "Driver")) {
                String name = XMLUtils.getChildElementBody(driverElement, "Name");
                String sampleURL = XMLUtils.getChildElementBody(driverElement, "URLFormat");
                String driverClass = XMLUtils.getChildElementBody(driverElement, "DefaultClass");
                String lastName = XMLUtils.getChildElementBody(driverElement, "LastName");
                //String lastVersion = XMLUtils.getChildElementBody(driverElement, "LastVersion");
                if (!CommonUtils.isEmpty(name) && !CommonUtils.isEmpty(sampleURL) && !CommonUtils.isEmpty(driverClass)) {
                    ImportDriverInfo driver = new ImportDriverInfo(null, name, sampleURL, driverClass);
                    if (!CommonUtils.isEmpty(lastName)) {
                        driver.setDescription(lastName);
                    }
                    adaptSampleUrl(driver);
                    // Parse libraries
                    Element locationsElement = XMLUtils.getChildElement(driverElement, "Locations");
                    if (locationsElement != null) {
                        for (Element locationElement : XMLUtils.getChildElementList(locationsElement, "Location")) {
                            String path = XMLUtils.getChildElementBody(locationElement, "Path");
                            if (!CommonUtils.isEmpty(path)) {
                                driver.addLibrary(path);
                            }
                        }
                    }
                    importData.addDriver(driver);
                }
            }
        }
        Element databasesElement = XMLUtils.getChildElement(configDocument.getDocumentElement(), "Databases");
        if (databasesElement != null) {
            for (Element dbElement : XMLUtils.getChildElementList(databasesElement, "Database")) {
                String alias = XMLUtils.getChildElementBody(dbElement, "Alias");
                String url = XMLUtils.getChildElementBody(dbElement, "Url");
                String driverName = XMLUtils.getChildElementBody(dbElement, "Driver");
                String user = XMLUtils.getChildElementBody(dbElement, "Userid");
                String password = null;
                String passwordEncoded = XMLUtils.getChildElementBody(dbElement, "Password");
                /*
                    if (!CommonUtils.isEmpty(passwordEncoded)) {
                        try {
                            password = new String(Base64.decode(passwordEncoded), ContentUtils.DEFAULT_ENCODING);
                        } catch (UnsupportedEncodingException e) {
                            // Ignore
                        }
                    }
*/
                String hostName = null, port = null, database = null;
                Element urlVarsElement = XMLUtils.getChildElement(dbElement, "UrlVariables");
                if (urlVarsElement != null) {
                    Element driverElement = XMLUtils.getChildElement(urlVarsElement, "Driver");
                    if (driverElement != null) {
                        for (Element urlVarElement : XMLUtils.getChildElementList(driverElement, "UrlVariable")) {
                            final String varName = urlVarElement.getAttribute("UrlVariableName");
                            final String varValue = XMLUtils.getElementBody(urlVarElement);
                            if ("Server".equals(varName)) {
                                hostName = varValue;
                            } else if ("Port".equals(varName)) {
                                port = varValue;
                            } else if ("Database".equals(varName)) {
                                database = varValue;
                            }
                        }
                    }
                }
                if (!CommonUtils.isEmpty(alias) && !CommonUtils.isEmpty(driverName) && (!CommonUtils.isEmpty(url) || !CommonUtils.isEmpty(hostName))) {
                    ImportDriverInfo driver = importData.getDriver(driverName);
                    if (driver != null) {
                        ImportConnectionInfo connectionInfo = new ImportConnectionInfo(driver, dbElement.getAttribute("id"), alias, url, hostName, port, database, user, password);
                        importData.addConnection(connectionInfo);
                    }
                }
            }
        }
    } catch (XMLException e) {
        throw new DBException("Configuration parse error: " + e.getMessage());
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) XMLException(org.jkiss.utils.xml.XMLException) ImportConnectionInfo(org.jkiss.dbeaver.ext.import_config.wizards.ImportConnectionInfo) Element(org.w3c.dom.Element) ImportDriverInfo(org.jkiss.dbeaver.ext.import_config.wizards.ImportDriverInfo) Document(org.w3c.dom.Document) File(java.io.File)

Example 3 with ImportDriverInfo

use of org.jkiss.dbeaver.ext.import_config.wizards.ImportDriverInfo in project dbeaver by serge-rider.

the class ConfigImportWizardPageSquirrel method loadConnections.

@Override
protected void loadConnections(ImportData importData) throws DBException {
    File homeFolder = RuntimeUtils.getUserHomeDir();
    File sqlConfigHome = new File(homeFolder, SQL_HOME_FOLDER);
    if (!sqlConfigHome.exists()) {
        throw new DBException("SQL Squirrel installation not found");
    }
    File driversFile = new File(sqlConfigHome, SQL_DRIVERS_FILE);
    if (!driversFile.exists()) {
        throw new DBException("SQL Squirrel drivers configuration file not found. Possibly corrupted installation.");
    }
    File aliasesFile = new File(sqlConfigHome, SQL_ALIASES_FILE);
    if (!aliasesFile.exists()) {
        throw new DBException("SQL Squirrel configuration file not found. Possibly version older than 2.3 is installed.");
    }
    try {
        // Parse drivers
        Document driversDocument = XMLUtils.parseDocument(driversFile);
        for (Element driverElement : XMLUtils.getChildElementList(driversDocument.getDocumentElement(), "Bean")) {
            if (!"net.sourceforge.squirrel_sql.fw.sql.SQLDriver".equals(driverElement.getAttribute("Class"))) {
                continue;
            }
            final Element driverIdentifier = XMLUtils.getChildElement(driverElement, "identifier");
            String driverId = driverIdentifier == null ? null : XMLUtils.getChildElementBody(driverIdentifier, "string");
            if (CommonUtils.isEmpty(driverId)) {
                continue;
            }
            String name = XMLUtils.getChildElementBody(driverElement, "name");
            String driverClass = XMLUtils.getChildElementBody(driverElement, "driverClassName");
            String sampleURL = XMLUtils.getChildElementBody(driverElement, "url");
            if (!CommonUtils.isEmpty(name) && !CommonUtils.isEmpty(sampleURL) && !CommonUtils.isEmpty(driverClass)) {
                ImportDriverInfo driver = new ImportDriverInfo(driverId, name, sampleURL, driverClass);
                adaptSampleUrl(driver);
                // Parse libraries
                final Element jarFileNames = XMLUtils.getChildElement(driverElement, "jarFileNames");
                if (jarFileNames != null) {
                    for (Element locationElement : XMLUtils.getChildElementList(jarFileNames, "Bean")) {
                        String path = XMLUtils.getChildElementBody(locationElement, "string");
                        if (!CommonUtils.isEmpty(path)) {
                            driver.addLibrary(path);
                        }
                    }
                }
                importData.addDriver(driver);
            }
        }
        // Parse aliases
        Document aliasesDocument = XMLUtils.parseDocument(aliasesFile);
        for (Element aliasElement : XMLUtils.getChildElementList(aliasesDocument.getDocumentElement(), "Bean")) {
            if (!"net.sourceforge.squirrel_sql.client.gui.db.SQLAlias".equals(aliasElement.getAttribute("Class"))) {
                continue;
            }
            final Element driverIdentifier = XMLUtils.getChildElement(aliasElement, "driverIdentifier");
            String driverId = driverIdentifier == null ? null : XMLUtils.getChildElementBody(driverIdentifier, "string");
            if (CommonUtils.isEmpty(driverId)) {
                continue;
            }
            final ImportDriverInfo driverInfo = importData.getDriverByID(driverId);
            if (driverInfo == null) {
                continue;
            }
            String name = XMLUtils.getChildElementBody(aliasElement, "name");
            String url = XMLUtils.getChildElementBody(aliasElement, "url");
            String userName = XMLUtils.getChildElementBody(aliasElement, "userName");
            String password = XMLUtils.getChildElementBody(aliasElement, "password");
            ImportConnectionInfo connectionInfo = new ImportConnectionInfo(driverInfo, null, name, url, null, null, null, userName, password);
            importData.addConnection(connectionInfo);
        }
    } catch (XMLException e) {
        throw new DBException("Configuration parse error: " + e.getMessage());
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) XMLException(org.jkiss.utils.xml.XMLException) ImportConnectionInfo(org.jkiss.dbeaver.ext.import_config.wizards.ImportConnectionInfo) Element(org.w3c.dom.Element) ImportDriverInfo(org.jkiss.dbeaver.ext.import_config.wizards.ImportDriverInfo) Document(org.w3c.dom.Document) File(java.io.File)

Aggregations

DBException (org.jkiss.dbeaver.DBException)3 ImportDriverInfo (org.jkiss.dbeaver.ext.import_config.wizards.ImportDriverInfo)3 XMLException (org.jkiss.utils.xml.XMLException)3 File (java.io.File)2 ImportConnectionInfo (org.jkiss.dbeaver.ext.import_config.wizards.ImportConnectionInfo)2 Document (org.w3c.dom.Document)2 Element (org.w3c.dom.Element)2 CSVReader (au.com.bytecode.opencsv.CSVReader)1 DBPDriver (org.jkiss.dbeaver.model.connection.DBPDriver)1