dbeaver/pro#3978 init workspace before configuration, support temp fo… (#3236)

* dbeaver/pro#3978 init workspace before configuration, support temp folder in services

* dbeaver/pro#3978 fix tests

---------

Co-authored-by: kseniaguzeeva <112612526+kseniaguzeeva@users.noreply.github.com>
This commit is contained in:
Alexander Skoblikov
2025-02-10 17:06:57 +01:00
committed by GitHub
co-authored by kseniaguzeeva
parent 9e9ec13db0
commit c85974c9f5
9 changed files with 128 additions and 103 deletions
-1
View File
@@ -2,7 +2,6 @@
server: {
serverPort: "${CLOUDBEAVER_WEB_SERVER_PORT:8978}",
workspaceLocation: "${CLOUDBEAVER_WORKSPACE_LOCATION:workspace}",
contentRoot: "web",
driversLocation: "drivers",
@@ -1,6 +1,6 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2024 DBeaver Corp and others
* Copyright (C) 2010-2025 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,10 +19,10 @@ package io.cloudbeaver.model.app;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.DBException;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.registry.fs.FileSystemProviderRegistry;
import org.jkiss.utils.IOUtils;
import org.jkiss.utils.CommonUtils;
import java.net.URI;
import java.nio.file.FileSystem;
@@ -38,12 +38,13 @@ public abstract class BaseServerConfigurationController<T extends ServletServerC
@NotNull
private final Path homeDirectory;
protected Path workspacePath;
@NotNull
protected final Path workspacePath;
protected BaseServerConfigurationController(@NotNull Path homeDirectory) {
this.homeDirectory = homeDirectory;
//default workspaceLocation
this.workspacePath = homeDirectory.resolve("workspace");
this.workspacePath = initWorkspacePath();
log.info("Workspace path initialized: " + workspacePath.toAbsolutePath());
}
@NotNull
@@ -58,35 +59,40 @@ public abstract class BaseServerConfigurationController<T extends ServletServerC
@NotNull
protected synchronized void initWorkspacePath() throws DBException {
if (workspacePath != null && !IOUtils.isFileFromDefaultFS(workspacePath)) {
log.warn("Workspace directory already initialized: " + workspacePath);
protected synchronized Path initWorkspacePath() {
Path defaultWorkspaceLocation = homeDirectory.resolve("workspace");
String workspaceLocation = getWorkspaceLocationFromEnv();
if (CommonUtils.isEmpty(workspaceLocation)) {
return defaultWorkspaceLocation;
}
String workspaceLocation = getWorkspaceLocation();
URI workspaceUri = URI.create(workspaceLocation);
if (workspaceUri.getScheme() == null) {
// default filesystem
this.workspacePath = getHomeDirectory().resolve(workspaceLocation);
return getHomeDirectory().resolve(workspaceLocation);
} else {
var externalFsProvider =
FileSystemProviderRegistry.getInstance().getFileSystemProviderBySchema(workspaceUri.getScheme());
if (externalFsProvider == null) {
throw new DBException("File system not found for scheme: " + workspaceUri.getScheme());
log.error("File system not found for scheme: " + workspaceUri.getScheme() + " default workspace " +
"location will be used");
return defaultWorkspaceLocation;
}
ClassLoader fsClassloader = externalFsProvider.getInstance().getClass().getClassLoader();
try (FileSystem externalFileSystem = FileSystems.newFileSystem(workspaceUri,
System.getenv(),
fsClassloader);) {
this.workspacePath = externalFileSystem.provider().getPath(workspaceUri);
fsClassloader)) {
log.info("Path from external filesystem used for workspace: " + workspaceUri);
return externalFileSystem.provider().getPath(workspaceUri);
} catch (Exception e) {
throw new DBException("Failed to initialize workspace path: " + workspaceUri, e);
log.error("Failed to initialize workspace path: " + workspaceUri + " default workspace " +
"location will be used", e);
}
}
log.info("Workspace path initialized: " + workspacePath);
return defaultWorkspaceLocation;
}
@NotNull
protected abstract String getWorkspaceLocation();
@Nullable
protected abstract String getWorkspaceLocationFromEnv();
@NotNull
protected Path getHomeDirectory() {
@@ -96,9 +102,6 @@ public abstract class BaseServerConfigurationController<T extends ServletServerC
@NotNull
@Override
public Path getWorkspacePath() {
if (workspacePath == null) {
throw new RuntimeException("Workspace path not initialized");
}
return workspacePath;
}
}
@@ -0,0 +1,84 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2025 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.cloudbeaver.server;
import io.cloudbeaver.DBWConstants;
import io.cloudbeaver.model.app.ServletApplication;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.registry.BasePlatformImpl;
import org.jkiss.dbeaver.utils.ContentUtils;
import org.jkiss.utils.CommonUtils;
import org.jkiss.utils.StandardConstants;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public abstract class BaseServletPlatform extends BasePlatformImpl {
private static final Log log = Log.getLog(BaseServletPlatform.class);
public static final String BASE_TEMP_DIR = "dbeaver";
protected volatile Path tempFolder;
@NotNull
public Path getTempFolder(@NotNull DBRProgressMonitor monitor, @NotNull String name) {
if (tempFolder == null) {
synchronized (this) {
if (tempFolder == null) {
initTempFolder(monitor);
}
}
}
Path folder = tempFolder.resolve(name);
if (!Files.exists(folder)) {
try {
Files.createDirectories(folder);
} catch (IOException e) {
log.error("Error creating temp folder '" + folder.toAbsolutePath() + "'", e);
}
}
return folder;
}
public abstract ServletApplication getApplication();
private void initTempFolder(@NotNull DBRProgressMonitor monitor) {
// Make temp folder
monitor.subTask("Create temp folder");
String sysTempFolder = System.getProperty(StandardConstants.ENV_TMP_DIR);
if (CommonUtils.isNotEmpty(sysTempFolder)) {
tempFolder = Path.of(sysTempFolder).resolve(BASE_TEMP_DIR).resolve(DBWConstants.WORK_DATA_FOLDER_NAME);
} else {
//we do not use workspace because it can be in external file system
tempFolder = getApplication().getHomeDirectory().resolve(DBWConstants.WORK_DATA_FOLDER_NAME);
}
}
public synchronized void dispose() {
// Remove temp folder
if (tempFolder != null) {
if (!ContentUtils.deleteFileRecursive(tempFolder.toFile())) {
log.warn("Can't delete temp folder '" + tempFolder.toAbsolutePath() + "'");
}
tempFolder = null;
}
}
}
@@ -1,6 +1,6 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2024 DBeaver Corp and others
* Copyright (C) 2010-2025 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -39,7 +39,6 @@ public class CBServerConfig implements WebServerConfiguration {
private String rootURI = CBConstants.DEFAULT_ROOT_URI;
private String serviceURI = CBConstants.DEFAULT_SERVICES_URI;
private String workspaceLocation = CBConstants.DEFAULT_WORKSPACE_LOCATION;
private String driversLocation = CBConstants.DEFAULT_DRIVERS_LOCATION;
@SerializedName("expireSessionAfterPeriod")
private long maxSessionIdleTime = CBAuthConstants.MAX_SESSION_IDLE_TIME;
@@ -85,10 +84,6 @@ public class CBServerConfig implements WebServerConfiguration {
return serviceURI;
}
public String getWorkspaceLocation() {
return workspaceLocation;
}
public String getDriversLocation() {
return driversLocation;
}
@@ -133,10 +128,6 @@ public class CBServerConfig implements WebServerConfiguration {
this.serviceURI = servicesURI;
}
public void setWorkspaceLocation(String workspaceLocation) {
this.workspaceLocation = workspaceLocation;
}
public void setDriversLocation(String driversLocation) {
this.driversLocation = driversLocation;
}
@@ -240,11 +240,11 @@ public abstract class CBApplication<T extends CBServerConfig> extends
URL wsLocationURL = new URL(
"file", //$NON-NLS-1$
null,
getServerConfiguration().getWorkspaceLocation());
getWorkspaceDirectory().toAbsolutePath().toString());
instanceLoc.set(wsLocationURL, true);
}
} catch (Exception e) {
log.error("Error setting workspace location to " + getServerConfiguration().getWorkspaceLocation(), e);
log.error("Error setting workspace location to " + getWorkspaceDirectory().toAbsolutePath(), e);
return;
}
@@ -440,13 +440,16 @@ public abstract class CBApplication<T extends CBServerConfig> extends
@NotNull
public Path getDataDirectory(boolean create) {
File dataDir = new File(getServerConfiguration().getWorkspaceLocation(), CBConstants.RUNTIME_DATA_DIR_NAME);
if (create && !dataDir.exists()) {
if (!dataDir.mkdirs()) {
log.error("Can't create data directory '" + dataDir.getAbsolutePath() + "'");
Path dataDir = getWorkspaceDirectory().resolve(CBConstants.RUNTIME_DATA_DIR_NAME);
if (create && !Files.exists(dataDir)) {
try {
Files.createDirectories(dataDir);
} catch (IOException e) {
log.error("Can't create data directory '" + dataDir.toAbsolutePath() + "'");
}
}
return dataDir.toPath();
return dataDir;
}
private void initializeSecurityController() throws DBException {
@@ -1,6 +1,6 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2024 DBeaver Corp and others
* Copyright (C) 2010-2025 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -91,8 +91,6 @@ public abstract class CBServerConfigurationController<T extends CBServerConfig>
loadConfiguration(configPath);
}
initWorkspacePath();
// Try to load configuration from runtime app config file
Path runtimeConfigPath = getRuntimeAppConfigPath();
if (Files.exists(runtimeConfigPath)) {
@@ -106,10 +104,11 @@ public abstract class CBServerConfigurationController<T extends CBServerConfig>
validateFinalServerConfiguration();
}
@NotNull
@Nullable
@Override
protected String getWorkspaceLocation() {
return getServerConfiguration().getWorkspaceLocation();
public String getWorkspaceLocationFromEnv() {
String envValue = System.getenv("CLOUDBEAVER_WORKSPACE_LOCATION");
return CommonUtils.nullIfEmpty(envValue);
}
public void loadConfiguration(Path configPath) throws DBException {
@@ -16,38 +16,24 @@
*/
package io.cloudbeaver.server;
import io.cloudbeaver.DBWConstants;
import io.cloudbeaver.server.websockets.WebSocketPingPongJob;
import org.eclipse.core.runtime.Plugin;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.Log;
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.impl.app.DefaultCertificateStorage;
import org.jkiss.dbeaver.model.qm.QMRegistry;
import org.jkiss.dbeaver.model.qm.QMUtils;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.registry.BasePlatformImpl;
import org.jkiss.dbeaver.registry.DataSourceProviderRegistry;
import org.jkiss.dbeaver.runtime.SecurityProviderUtils;
import org.jkiss.dbeaver.runtime.qm.QMLogFileWriter;
import org.jkiss.dbeaver.runtime.qm.QMRegistryImpl;
import org.jkiss.dbeaver.utils.ContentUtils;
import org.jkiss.utils.CommonUtils;
import org.jkiss.utils.StandardConstants;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public abstract class BaseWebPlatform extends BasePlatformImpl {
private static final Log log = Log.getLog(BaseWebPlatform.class);
public static final String BASE_TEMP_DIR = "dbeaver";
public abstract class BaseWebPlatform extends BaseServletPlatform {
public static final String TEMP_FILE_FOLDER = "temp-sql-upload-files";
public static final String TEMP_FILE_IMPORT_FOLDER = "temp-import-files";
private Path tempFolder;
private QMRegistryImpl queryManager;
private QMLogFileWriter qmLogWriter;
@@ -96,39 +82,6 @@ public abstract class BaseWebPlatform extends BasePlatformImpl {
return workspace;
}
@NotNull
public Path getTempFolder(@NotNull DBRProgressMonitor monitor, @NotNull String name) {
if (tempFolder == null) {
synchronized (this) {
if (tempFolder == null) {
initTempFolder(monitor);
}
}
}
Path folder = tempFolder.resolve(name);
if (!Files.exists(folder)) {
try {
Files.createDirectories(folder);
} catch (IOException e) {
log.error("Error creating temp folder '" + folder.toAbsolutePath() + "'", e);
}
}
return folder;
}
private void initTempFolder(@NotNull DBRProgressMonitor monitor) {
// Make temp folder
monitor.subTask("Create temp folder");
String sysTempFolder = System.getProperty(StandardConstants.ENV_TMP_DIR);
if (CommonUtils.isNotEmpty(sysTempFolder)) {
tempFolder = Path.of(sysTempFolder).resolve(BASE_TEMP_DIR).resolve(DBWConstants.WORK_DATA_FOLDER_NAME);
} else {
//we do not use workspace because it can be in external file system
tempFolder = getApplication().getHomeDirectory().resolve(DBWConstants.WORK_DATA_FOLDER_NAME);
}
}
@NotNull
public abstract WebApplication getApplication();
@@ -149,15 +102,6 @@ public abstract class BaseWebPlatform extends BasePlatformImpl {
//queryManager = null;
}
DataSourceProviderRegistry.dispose();
// Remove temp folder
if (tempFolder != null) {
if (!ContentUtils.deleteFileRecursive(tempFolder.toFile())) {
log.warn("Can't delete temp folder '" + tempFolder.toAbsolutePath() + "'");
}
tempFolder = null;
}
}
@NotNull
@@ -3,7 +3,6 @@
serverPort: "${CLOUDBEAVER_TEST_PORT:18978}",
serverName: "${CLOUDBEAVER_SERVER_NAME:CloudBeaver CE Test Server}",
workspaceLocation: "${CLOUDBEAVER_WORKSPACE_LOCATION:workspace}",
contentRoot: "workspace/web",
driversLocation: "../../../deploy/",
+3
View File
@@ -60,6 +60,9 @@
<testRuntime>default</testRuntime>
<testClass>io.cloudbeaver.test.platform.CEServerTestSuite</testClass>
<appArgLine>-web-config workspace/conf/cloudbeaver.conf</appArgLine>
<environmentVariables>
<CLOUDBEAVER_WORKSPACE_LOCATION>target/workspace</CLOUDBEAVER_WORKSPACE_LOCATION>
</environmentVariables>
<argLine>-Dlogback.configurationFile=workspace/conf/logback.xml ${debugArgs}</argLine>
</configuration>
</plugin>