Skip to content

Commit

Permalink
CB-5390 wip
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-skoblikov committed Sep 27, 2024
1 parent 58fc9cb commit f8f2778
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,32 +54,37 @@ public Gson getGson() {


@NotNull
protected synchronized Path initWorkspacePath() throws DBException {
protected synchronized void initWorkspacePath() throws DBException {
if (workspacePath != null) {
log.warn("Workspace directory already initialized: " + workspacePath);
return workspacePath;
return;
}
String workspaceLocation = getServerConfiguration().getWorkspaceLocation();
String workspaceLocation = getWorkspaceLocation();
URI workspaceUri = URI.create(workspaceLocation);
if (workspaceUri.getScheme() == null) {
// default filesystem
return getHomeDirectory().resolve(workspaceLocation);
}

var externalFsProvider =
FileSystemProviderRegistry.getInstance().getFileSystemProviderBySchema(workspaceUri.getScheme());
if (externalFsProvider == null) {
throw new DBException("File system not found for scheme: " + workspaceUri.getScheme());
}
ClassLoader fsClassloader = externalFsProvider.getInstance().getClass().getClassLoader();
try (FileSystem externalFileSystem = FileSystems.newFileSystem(workspaceUri, System.getenv(), fsClassloader);) {
this.workspacePath = externalFileSystem.provider().getPath(workspaceUri);
return workspacePath;
} catch (Exception e) {
throw new DBException("Failed to initialize workspace path: " + workspaceUri, e);
this.workspacePath = 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());
}
ClassLoader fsClassloader = externalFsProvider.getInstance().getClass().getClassLoader();
try (FileSystem externalFileSystem = FileSystems.newFileSystem(workspaceUri,
System.getenv(),
fsClassloader);) {
this.workspacePath = externalFileSystem.provider().getPath(workspaceUri);
} catch (Exception e) {
throw new DBException("Failed to initialize workspace path: " + workspaceUri, e);
}
}
log.info("Workspace path initialized: " + workspacePath);
}

@NotNull
protected abstract String getWorkspaceLocation();

@NotNull
protected Path getHomeDirectory() {
return homeDirectory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
package io.cloudbeaver.model.app;

import org.jkiss.code.NotNull;

/**
* Web server configuration.
* Contains only server configuration properties.
Expand All @@ -29,9 +27,4 @@ default String getRootURI() {
return "";
}

@NotNull
default String getWorkspaceLocation() {
return "workspace";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ public String moveResource(
throw new DBException("Resource '" + oldTargetPath + "' doesn't exists");
}
Path newTargetPath = getTargetPath(projectId, normalizedNewResourcePath);
validateResourcePath(newTargetPath.toString());
validateResourcePath(rootPath.relativize(newTargetPath).toString());
if (Files.exists(newTargetPath)) {
throw new DBException("Resource with name %s already exists".formatted(newTargetPath.getFileName()));
}
Expand Down Expand Up @@ -881,7 +881,7 @@ private Path getTargetPath(@NotNull String projectId, @NotNull String resourcePa
if (!targetPath.startsWith(projectPath)) {
throw new DBException("Invalid resource path");
}
return WebAppUtils.getWebApplication().getHomeDirectory().relativize(targetPath);
return targetPath;
} catch (InvalidPathException e) {
throw new DBException("Resource path contains invalid characters");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.UUID;
Expand Down Expand Up @@ -73,18 +74,23 @@ public RMFileLockController(WebApplication application, int maxLockTime) throws
* @return - lock
*/
@NotNull
public RMLock lockProject(@NotNull String projectId,@NotNull String operationName) throws DBException {
public RMLock lockProject(@NotNull String projectId, @NotNull String operationName) throws DBException {
synchronized (RMFileLockController.class) {
try {
createLockFolderIfNeeded();
createProjectFolder(projectId);
Path projectLockFile = getProjectLockFilePath(projectId);

RMLockInfo lockInfo = new RMLockInfo.Builder(projectId, UUID.randomUUID().toString())
.setApplicationId(applicationId)
.setOperationName(operationName)
.setOperationStartTime(System.currentTimeMillis())
.build();
Path projectLockFile = getProjectLockFilePath(projectId);

if (!lockFolderPath.getFileSystem().equals(FileSystems.getDefault())) {
// fake lock for external file system?
return new RMLock(projectLockFile);
}
createLockFolderIfNeeded();
createProjectFolder(projectId);

createLockFile(projectLockFile, lockInfo);
return new RMLock(projectLockFile);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,8 @@ public Path getTempFolder(@NotNull DBRProgressMonitor monitor, @NotNull String n
if (tempFolder == null) {
// Make temp folder
monitor.subTask("Create temp folder");
tempFolder = workspace.getAbsolutePath().resolve(DBWConstants.WORK_DATA_FOLDER_NAME);
}
if (!Files.exists(tempFolder)) {
try {
Files.createDirectories(tempFolder);
} catch (IOException e) {
log.error("Can't create temp directory " + tempFolder, e);
}
//we do not use workspace because it can be in external file system
tempFolder = getApplication().getHomeDirectory().resolve(DBWConstants.WORK_DATA_FOLDER_NAME);
}
Path folder = tempFolder.resolve(name);
if (!Files.exists(folder)) {
Expand All @@ -116,6 +110,9 @@ public Path getTempFolder(@NotNull DBRProgressMonitor monitor, @NotNull String n
return folder;
}

@NotNull

Check warning on line 113 in server/bundles/io.cloudbeaver.model/src/io/cloudbeaver/server/BaseGQLPlatform.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Java Report

server/bundles/io.cloudbeaver.model/src/io/cloudbeaver/server/BaseGQLPlatform.java#L113

Missing a Javadoc comment.
public abstract WebApplication getApplication();

@Override
public synchronized void dispose() {
super.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

import java.io.File;
import java.io.PrintStream;

/**
Expand All @@ -33,7 +32,6 @@ public class WebPlatformActivator extends Plugin {

// The shared instance
private static WebPlatformActivator instance;
private static File configDir;
private PrintStream debugWriter;
private DBPPreferenceStore preferences;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.ModelPreferences;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.app.DBPPlatform;
import org.jkiss.dbeaver.model.auth.AuthInfo;
Expand All @@ -53,19 +54,20 @@
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.runtime.ui.DBPPlatformUI;
import org.jkiss.dbeaver.utils.GeneralUtils;
import org.jkiss.dbeaver.utils.PrefUtils;
import org.jkiss.dbeaver.utils.SystemVariablesResolver;
import org.jkiss.utils.ArrayUtils;
import org.jkiss.utils.CommonUtils;
import org.jkiss.utils.StandardConstants;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.URL;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -201,13 +203,20 @@ protected void startServer() {
if (!loadServerConfiguration()) {
return;
}

if (CommonUtils.isEmpty(this.getAppConfiguration().getDefaultUserTeam())) {
throw new DBException("Default user team must be specified");
}
} catch (DBException e) {
log.error(e);
return;
}
// Set default preferences
PrefUtils.setDefaultPreferenceValue(DBWorkbench.getPlatform().getPreferenceStore(),
ModelPreferences.UI_DRIVERS_HOME,
getServerConfiguration().getDriversLocation());
CBPlatform.getInstance().refreshApplicableDrivers();

refreshDisabledDriversConfig();

configurationMode = CommonUtils.isEmpty(getServerConfiguration().getServerName());
Expand Down Expand Up @@ -303,7 +312,7 @@ protected void startServer() {

if (configurationMode) {
// Try to configure automatically
performAutoConfiguration(getMainConfigurationFilePath().toFile().getParentFile());
performAutoConfiguration(getMainConfigurationFilePath().getParent());
} else if (!isMultiNode()) {
var appConfiguration = getServerConfigurationController().getAppConfiguration();
if (appConfiguration.isGrantConnectionsAccessToAnonymousTeam()) {
Expand Down Expand Up @@ -331,7 +340,7 @@ protected void initializeAdditionalConfiguration() {
*
* @param configPath
*/
protected void performAutoConfiguration(File configPath) {
protected void performAutoConfiguration(Path configPath) {
String autoServerName = System.getenv(CBConstants.VAR_AUTO_CB_SERVER_NAME);
String autoServerURL = System.getenv(CBConstants.VAR_AUTO_CB_SERVER_URL);
String autoAdminName = System.getenv(CBConstants.VAR_AUTO_CB_ADMIN_NAME);
Expand All @@ -340,19 +349,19 @@ protected void performAutoConfiguration(File configPath) {
if (CommonUtils.isEmpty(autoServerName) || CommonUtils.isEmpty(autoAdminName) || CommonUtils.isEmpty(
autoAdminPassword)) {
// Try to load from auto config file
if (configPath.exists()) {
File autoConfigFile = new File(configPath, CBConstants.AUTO_CONFIG_FILE_NAME);
if (autoConfigFile.exists()) {
if (Files.exists(configPath)) {
Path autoConfigFile = configPath.resolve(CBConstants.AUTO_CONFIG_FILE_NAME);
if (Files.exists(autoConfigFile)) {
Properties autoProps = new Properties();
try (InputStream is = new FileInputStream(autoConfigFile)) {
try (InputStream is = Files.newInputStream(autoConfigFile)) {
autoProps.load(is);

autoServerName = autoProps.getProperty(CBConstants.VAR_AUTO_CB_SERVER_NAME);
autoServerURL = autoProps.getProperty(CBConstants.VAR_AUTO_CB_SERVER_URL);
autoAdminName = autoProps.getProperty(CBConstants.VAR_AUTO_CB_ADMIN_NAME);
autoAdminPassword = autoProps.getProperty(CBConstants.VAR_AUTO_CB_ADMIN_PASSWORD);
} catch (IOException e) {
log.error("Error loading auto configuration file '" + autoConfigFile.getAbsolutePath() + "'",
log.error("Error loading auto configuration file '" + autoConfigFile + "'",
e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,13 @@
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.ModelPreferences;
import org.jkiss.dbeaver.model.auth.SMCredentialsProvider;
import org.jkiss.dbeaver.model.data.json.JSONUtils;
import org.jkiss.dbeaver.model.navigator.DBNBrowseSettings;
import org.jkiss.dbeaver.model.security.SMAuthProviderCustomConfiguration;
import org.jkiss.dbeaver.registry.DataSourceNavigatorSettings;
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.runtime.IVariableResolver;
import org.jkiss.dbeaver.utils.ContentUtils;
import org.jkiss.dbeaver.utils.PrefUtils;
import org.jkiss.dbeaver.utils.SystemVariablesResolver;
import org.jkiss.utils.CommonUtils;

Expand Down Expand Up @@ -100,12 +97,12 @@ public void loadServerConfiguration(Path configPath) throws DBException {
log.debug("Runtime configuration [" + runtimeConfigPath.toAbsolutePath() + "]");
loadConfiguration(runtimeConfigPath);
}
}

// Set default preferences
PrefUtils.setDefaultPreferenceValue(DBWorkbench.getPlatform().getPreferenceStore(),
ModelPreferences.UI_DRIVERS_HOME,
getServerConfiguration().getDriversLocation());
CBPlatform.getInstance().refreshApplicableDrivers();
@NotNull
@Override
protected String getWorkspaceLocation() {
return getServerConfiguration().getWorkspaceLocation();
}

public void loadConfiguration(Path configPath) throws DBException {
Expand Down

0 comments on commit f8f2778

Please sign in to comment.