Skip to content

Commit

Permalink
CB-6051 move out applicable drivers from platform class
Browse files Browse the repository at this point in the history
  • Loading branch information
yagudin10 committed Dec 26, 2024
1 parent 8f78a09 commit 690d71d
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,20 @@
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.connection.DBPDataSourceProviderDescriptor;
import org.jkiss.dbeaver.model.connection.DBPDriver;
import org.jkiss.dbeaver.model.connection.DBPDriverLibrary;
import org.jkiss.dbeaver.registry.DataSourceProviderRegistry;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class WebDriverRegistry {

Expand All @@ -42,6 +51,7 @@ public synchronized static WebDriverRegistry getInstance() {
return instance;
}

private final List<DBPDriver> applicableDrivers = new ArrayList<>();
private final Set<String> webDrivers = new HashSet<>();

protected WebDriverRegistry() {
Expand All @@ -59,13 +69,45 @@ private void loadExtensions(IExtensionRegistry registry) {
}
}

public boolean isDriverEnabled(DBPDriver driver) {
return webDrivers.contains(driver.getFullId());
public List<DBPDriver> getApplicableDrivers() {
return applicableDrivers;
}

public void refreshApplicableDrivers() {

Check warning on line 76 in server/bundles/io.cloudbeaver.model/src/io/cloudbeaver/registry/WebDriverRegistry.java

View workflow job for this annotation

GitHub Actions / Server / Lint

[checkstyle] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./server/bundles/io.cloudbeaver.model/src/io/cloudbeaver/registry/WebDriverRegistry.java:76:5: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)

Check warning on line 76 in server/bundles/io.cloudbeaver.model/src/io/cloudbeaver/registry/WebDriverRegistry.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Java Report

server/bundles/io.cloudbeaver.model/src/io/cloudbeaver/registry/WebDriverRegistry.java#L76

Missing a Javadoc comment.
this.applicableDrivers.clear();
this.applicableDrivers.addAll(
DataSourceProviderRegistry.getInstance().getEnabledDataSourceProviders().stream()
.map(DBPDataSourceProviderDescriptor::getEnabledDrivers)
.flatMap(List::stream)
.filter(this::isDriverApplicable)
.toList());
log.info("Available drivers: " + applicableDrivers.stream().map(DBPDriver::getFullName).collect(Collectors.joining(",")));
}

public boolean validateDriverFilesAvailability() {
return true;
protected boolean isDriverApplicable(@NotNull DBPDriver driver) {
List<? extends DBPDriverLibrary> libraries = driver.getDriverLibraries();
if (!webDrivers.contains(driver.getFullId())) {
return false;
}
boolean hasAllFiles = true;
for (DBPDriverLibrary lib : libraries) {
if (!isDriverLibraryFilePresent(lib)) {
hasAllFiles = false;
log.error("\tDriver '" + driver.getId() + "' is missing library '" + lib.getDisplayName() + "'");
} else {
if (lib.getType() == DBPDriverLibrary.FileType.jar) {
return true;
}
}
}
return hasAllFiles;
}

private boolean isDriverLibraryFilePresent(@NotNull DBPDriverLibrary lib) {
if (lib.getType() == DBPDriverLibrary.FileType.license) {
return true;
}
Path localFile = lib.getLocalFile();
return localFile != null && Files.exists(localFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -735,10 +735,10 @@ protected void sendConfigChangedEvent(SMCredentialsProvider credentialsProvider)
public abstract CBServerConfigurationController<T> getServerConfigurationController();

private void refreshDisabledDriversConfig() {
CBPlatform.getInstance().refreshApplicableDrivers();
getDriverRegistry().refreshApplicableDrivers();
CBAppConfig config = getAppConfiguration();
Set<String> disabledDrivers = new LinkedHashSet<>(Arrays.asList(config.getDisabledDrivers()));
for (DBPDriver driver : CBPlatform.getInstance().getApplicableDrivers()) {
for (DBPDriver driver : getDriverRegistry().getApplicableDrivers()) {
if (!driver.isEmbedded() || config.isDriverForceEnabled(driver.getFullId())) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package io.cloudbeaver.server;

import io.cloudbeaver.auth.NoAuthCredentialsProvider;
import io.cloudbeaver.registry.WebDriverRegistry;
import io.cloudbeaver.server.jobs.SessionStateJob;
import io.cloudbeaver.server.jobs.WebDataSourceMonitorJob;
import io.cloudbeaver.server.jobs.WebSessionMonitorJob;
Expand All @@ -28,22 +27,13 @@
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBFileController;
import org.jkiss.dbeaver.model.connection.DBPDataSourceProviderDescriptor;
import org.jkiss.dbeaver.model.connection.DBPDriver;
import org.jkiss.dbeaver.model.connection.DBPDriverLibrary;
import org.jkiss.dbeaver.model.preferences.DBPPreferenceStore;
import org.jkiss.dbeaver.model.runtime.AbstractJob;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.registry.DataSourceProviderRegistry;
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.utils.IOUtils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* CBPlatform
Expand All @@ -59,7 +49,6 @@ public class CBPlatform extends BaseWebPlatform {
private static CBApplication<?> application = null;

private WebServerPreferenceStore preferenceStore;
protected final List<DBPDriver> applicableDrivers = new ArrayList<>();

public static CBPlatform getInstance() {
return (CBPlatform) DBWorkbench.getPlatform();
Expand Down Expand Up @@ -124,10 +113,6 @@ public CBApplication<?> getApplication() {
return application;
}

public List<DBPDriver> getApplicableDrivers() {
return applicableDrivers;
}


@NotNull
@Override
Expand All @@ -140,47 +125,6 @@ public boolean isShuttingDown() {
return false;
}

public void refreshApplicableDrivers() {
this.applicableDrivers.clear();
assert application != null;
WebDriverRegistry driverRegistry = application.getDriverRegistry();

for (DBPDataSourceProviderDescriptor dspd : DataSourceProviderRegistry.getInstance().getEnabledDataSourceProviders()) {
for (DBPDriver driver : dspd.getEnabledDrivers()) {
List<? extends DBPDriverLibrary> libraries = driver.getDriverLibraries();
{
if (!driverRegistry.isDriverEnabled(driver)) {
continue;
}
boolean hasAllFiles = true, hasJars = false;
for (DBPDriverLibrary lib : libraries) {
if (driverRegistry.validateDriverFilesAvailability() && !isDriverLibraryFilePresent(lib))
{
hasAllFiles = false;
log.error("\tDriver '" + driver.getId() + "' is missing library '" + lib.getDisplayName() + "'");
} else {
if (lib.getType() == DBPDriverLibrary.FileType.jar) {
hasJars = true;
}
}
}
if (hasAllFiles || hasJars) {
applicableDrivers.add(driver);
}
}
}
}
log.info("Available drivers: " + applicableDrivers.stream().map(DBPDriver::getFullName).collect(Collectors.joining(",")));
}

private boolean isDriverLibraryFilePresent(@NotNull DBPDriverLibrary lib) {
if (lib.getType() == DBPDriverLibrary.FileType.license) {
return true;
}
Path localFile = lib.getLocalFile();
return localFile != null && Files.exists(localFile);
}

@NotNull
@Override
public DBFileController createFileController() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ public static List<WebAuthProviderDescriptor> getEnabledAuthProviders() {
*/
@NotNull
public static Set<String> getApplicableDriversIds() {
return WebAppUtils.getWebPlatform().getApplicableDrivers().stream()
return WebAppUtils.getWebApplication().getDriverRegistry().getApplicableDrivers().stream()
.map(DBPDriver::getId)
.collect(Collectors.toSet());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.jkiss.dbeaver.model.DBConstants;
import org.jkiss.dbeaver.model.app.DBACertificateStorage;
import org.jkiss.dbeaver.model.app.DBPWorkspace;
import org.jkiss.dbeaver.model.connection.DBPDriver;
import org.jkiss.dbeaver.model.impl.app.DefaultCertificateStorage;
import org.jkiss.dbeaver.model.qm.QMRegistry;
import org.jkiss.dbeaver.model.qm.QMUtils;
Expand All @@ -41,7 +40,6 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

public abstract class BaseWebPlatform extends BasePlatformImpl {
private static final Log log = Log.getLog(BaseWebPlatform.class);
Expand Down Expand Up @@ -166,5 +164,4 @@ public QMRegistry getQueryManager() {
return queryManager;
}

public abstract List<DBPDriver> getApplicableDrivers();
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public WebServerConfig getServerConfig() {
@Override
public List<WebDatabaseDriverInfo> getDriverList(@NotNull WebSession webSession, String driverId) {
List<WebDatabaseDriverInfo> result = new ArrayList<>();
for (DBPDriver driver : WebAppUtils.getWebPlatform().getApplicableDrivers()) {
for (DBPDriver driver : WebAppUtils.getWebApplication().getDriverRegistry().getApplicableDrivers()) {
if (driverId == null || driverId.equals(driver.getFullId())) {
result.add(new WebDatabaseDriverInfo(webSession, driver));
}
Expand Down Expand Up @@ -141,7 +141,7 @@ public List<WebDataSourceConfig> getTemplateDataSources() throws DBWebException

for (DBPDataSourceContainer ds : dsRegistry.getDataSources()) {
if (ds.isTemplate()) {
if (WebAppUtils.getWebPlatform().getApplicableDrivers().contains(ds.getDriver())) {
if (WebAppUtils.getWebApplication().getDriverRegistry().getApplicableDrivers().contains(ds.getDriver())) {
result.add(new WebDataSourceConfig(ds));
} else {
log.debug("Template datasource '" + ds.getName() + "' ignored - driver is not applicable");
Expand Down Expand Up @@ -180,7 +180,7 @@ private void getTemplateConnectionsFromProject(
for (DBPDataSourceContainer ds : registry.getDataSources()) {
if (ds.isTemplate() &&
project.getDataSourceFilter().filter(ds) &&
WebAppUtils.getWebPlatform().getApplicableDrivers().contains(ds.getDriver())) {
WebAppUtils.getWebApplication().getDriverRegistry().getApplicableDrivers().contains(ds.getDriver())) {
result.add(new WebConnectionInfo(webSession, ds));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.cloudbeaver.model.utils.ConfigurationUtils;
import io.cloudbeaver.server.CBApplication;
import io.cloudbeaver.server.CBPlatform;
import io.cloudbeaver.server.WebAppUtils;
import io.cloudbeaver.service.admin.AdminConnectionSearchInfo;
import org.jkiss.dbeaver.model.connection.DBPDriver;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
Expand All @@ -44,12 +45,10 @@ public class ConnectionSearcher implements DBRRunnableWithProgress {
private final WebSession webSession;
private final String[] hostNames;
private final List<AdminConnectionSearchInfo> foundConnections = new ArrayList<>();
private List<DBPDriver> availableDrivers = new ArrayList<>();

public ConnectionSearcher(WebSession webSession, String[] hostNames) {
this.webSession = webSession;
this.hostNames = hostNames;
this.availableDrivers.addAll(CBPlatform.getInstance().getApplicableDrivers());
}

public List<AdminConnectionSearchInfo> getFoundConnections() {

Check warning on line 54 in server/bundles/io.cloudbeaver.service.admin/src/io/cloudbeaver/service/admin/impl/ConnectionSearcher.java

View workflow job for this annotation

GitHub Actions / Server / Lint

[checkstyle] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./server/bundles/io.cloudbeaver.service.admin/src/io/cloudbeaver/service/admin/impl/ConnectionSearcher.java:54:5: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)
Expand Down Expand Up @@ -107,7 +106,7 @@ private void searchConnections(DBRProgressMonitor monitor, String hostName, Stri
int checkTimeout = 150;
Map<Integer, AdminConnectionSearchInfo> portCache = new HashMap<>();

for (DBPDriver driver : availableDrivers) {
for (DBPDriver driver : WebAppUtils.getWebApplication().getDriverRegistry().getApplicableDrivers()) {
monitor.subTask("Check '" + driver.getName() + "' on '" + hostName + "'");
if (!CommonUtils.isEmpty(driver.getDefaultPort()) && !isPortInBlockList(CommonUtils.toInt(driver.getDefaultPort()))) {
updatePortInfo(portCache, hostName, displayName, driver, checkTimeout);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import io.cloudbeaver.registry.*;
import io.cloudbeaver.server.CBApplication;
import io.cloudbeaver.server.CBConstants;
import io.cloudbeaver.server.CBPlatform;
import io.cloudbeaver.server.WebAppUtils;
import io.cloudbeaver.service.DBWServiceServerConfigurator;
import io.cloudbeaver.service.admin.*;
import io.cloudbeaver.service.security.SMUtils;
Expand Down Expand Up @@ -633,7 +633,7 @@ public boolean configureServer(WebSession webSession, Map<String, Object> params
// Just reload session state
webSession.refreshUserData();
}
CBPlatform.getInstance().refreshApplicableDrivers();
WebAppUtils.getWebApplication().getDriverRegistry().refreshApplicableDrivers();
} catch (Throwable e) {
throw new DBWebException("Error configuring server", e);
}
Expand Down

0 comments on commit 690d71d

Please sign in to comment.