Search in sources :

Example 6 with OSDescriptor

use of org.jkiss.dbeaver.model.runtime.OSDescriptor in project dbeaver by dbeaver.

the class MySQLDataSourceProvider method findLocalClients.

public static synchronized void findLocalClients() {
    if (localServers != null) {
        return;
    }
    localServers = new LinkedHashMap<>();
    // read from path
    String path = System.getenv("PATH");
    if (path != null && GeneralUtils.isWindows()) {
        for (String token : path.split(System.getProperty(StandardConstants.ENV_PATH_SEPARATOR))) {
            token = CommonUtils.removeTrailingSlash(token);
            File mysqlFile = new File(token, MySQLUtils.getMySQLConsoleBinaryName());
            if (mysqlFile.exists()) {
                // .getName()
                File binFolder = mysqlFile.getAbsoluteFile().getParentFile();
                if (binFolder.getName().equalsIgnoreCase("bin")) {
                    String homeId = CommonUtils.removeTrailingSlash(binFolder.getParentFile().getAbsolutePath());
                    localServers.put(homeId, new MySQLServerHome(homeId, null));
                }
            }
        }
    }
    // find homes in Windows registry
    OSDescriptor localSystem = DBWorkbench.getPlatform().getLocalSystem();
    if (localSystem.isWindows()) {
        try {
            // Search MySQL entries
            {
                final String registryRoot = localSystem.is64() ? REGISTRY_ROOT_MYSQL_64 : REGISTRY_ROOT_MYSQL_32;
                if (Advapi32Util.registryKeyExists(WinReg.HKEY_LOCAL_MACHINE, registryRoot)) {
                    String[] homeKeys = Advapi32Util.registryGetKeys(WinReg.HKEY_LOCAL_MACHINE, registryRoot);
                    if (homeKeys != null) {
                        for (String homeKey : homeKeys) {
                            Map<String, Object> valuesMap = Advapi32Util.registryGetValues(WinReg.HKEY_LOCAL_MACHINE, registryRoot + "\\" + homeKey);
                            for (String key : valuesMap.keySet()) {
                                if (SERER_LOCATION_KEY.equalsIgnoreCase(key)) {
                                    String serverPath = CommonUtils.removeTrailingSlash(CommonUtils.toString(valuesMap.get(key)));
                                    if (new File(serverPath, "bin").exists()) {
                                        localServers.put(serverPath, new MySQLServerHome(serverPath, homeKey));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            // Search MariaDB entries
            if (Advapi32Util.registryKeyExists(WinReg.HKEY_LOCAL_MACHINE, REGISTRY_ROOT_MARIADB)) {
                String[] homeKeys = Advapi32Util.registryGetKeys(WinReg.HKEY_LOCAL_MACHINE, REGISTRY_ROOT_MARIADB);
                if (homeKeys != null) {
                    for (String homeKey : homeKeys) {
                        Map<String, Object> valuesMap = Advapi32Util.registryGetValues(WinReg.HKEY_LOCAL_MACHINE, REGISTRY_ROOT_MARIADB + "\\" + homeKey);
                        for (String key : valuesMap.keySet()) {
                            if (INSTALLDIR_KEY.equalsIgnoreCase(key)) {
                                String serverPath = CommonUtils.removeTrailingSlash(CommonUtils.toString(valuesMap.get(key)));
                                if (new File(serverPath, "bin").exists()) {
                                    localServers.put(serverPath, new MySQLServerHome(serverPath, homeKey));
                                }
                            }
                        }
                    }
                }
            }
        } catch (Throwable e) {
            log.warn("Error reading Windows registry", e);
        }
    } else if (GeneralUtils.isMacOS()) {
        Collection<File> mysqlDirs = new ArrayList<>();
        Collections.addAll(mysqlDirs, // clients installed via installer downloaded from mysql site
        NativeClientLocationUtils.getSubdirectoriesWithNamesStartingWith("mysql", new File(NativeClientLocationUtils.USR_LOCAL)));
        Collections.addAll(mysqlDirs, NativeClientLocationUtils.getSubdirectories(NativeClientLocationUtils.getSubdirectoriesWithNamesStartingWith("mysql", new File(NativeClientLocationUtils.HOMEBREW_FORMULAE_LOCATION))));
        Collections.addAll(mysqlDirs, NativeClientLocationUtils.getSubdirectories(NativeClientLocationUtils.getSubdirectoriesWithNamesStartingWith("mariadb", new File(NativeClientLocationUtils.HOMEBREW_FORMULAE_LOCATION))));
        for (File dir : mysqlDirs) {
            File bin = new File(dir, NativeClientLocationUtils.BIN);
            File binary = new File(bin, MySQLUtils.getMySQLConsoleBinaryName());
            if (!bin.exists() || !bin.isDirectory() || !binary.exists() || !binary.canExecute()) {
                continue;
            }
            String version = getFullServerVersion(dir);
            if (version == null) {
                continue;
            }
            String canonicalPath = NativeClientLocationUtils.getCanonicalPath(dir);
            if (canonicalPath.isEmpty()) {
                continue;
            }
            MySQLServerHome home = new MySQLServerHome(canonicalPath, "MySQL " + version);
            localServers.put(canonicalPath, home);
        }
    }
}
Also used : OSDescriptor(org.jkiss.dbeaver.model.runtime.OSDescriptor)

Aggregations

OSDescriptor (org.jkiss.dbeaver.model.runtime.OSDescriptor)6 File (java.io.File)2 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)2 AbstractPreferenceStore (org.jkiss.dbeaver.model.impl.preferences.AbstractPreferenceStore)2 DBNModel (org.jkiss.dbeaver.model.navigator.DBNModel)2 DBPPreferenceStore (org.jkiss.dbeaver.model.preferences.DBPPreferenceStore)2 IPluginService (org.jkiss.dbeaver.runtime.IPluginService)2 KeepAliveListenerJob (org.jkiss.dbeaver.runtime.jobs.KeepAliveListenerJob)2