Skip to content

Commit

Permalink
Respect distribution-provided properties (#5890)
Browse files Browse the repository at this point in the history
  • Loading branch information
labkey-adam authored Oct 1, 2024
1 parent 5c55ba6 commit 42b18ec
Show file tree
Hide file tree
Showing 16 changed files with 172 additions and 136 deletions.
4 changes: 2 additions & 2 deletions api/src/org/labkey/api/admin/AdminBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.labkey.api.util.Formats;
import org.labkey.api.util.HtmlString;
import org.labkey.api.util.HtmlStringBuilder;
import org.labkey.api.util.MothershipReport;
import org.labkey.api.view.NavTree;
import org.labkey.api.view.ViewContext;

Expand Down Expand Up @@ -70,7 +69,8 @@ public static class RecentUser
public static final String userName = System.getProperty("user.name");
public static final String userHomeDir = System.getProperty("user.home");
public static final String webappDir = ModuleLoader.getServletContext().getRealPath("");
public static final String distribution = MothershipReport.getDistributionName();
public static final String distributionName = AppProps.getInstance().getDistributionName();
public static final String distributionFilename = AppProps.getInstance().getDistributionFilename();
public static final String workingDir = new File("file").getAbsoluteFile().getParent();
public static final String osName = System.getProperty("os.name");
public static final @Nullable String releaseVersion = ModuleLoader.getInstance().getCoreModule().getReleaseVersion();
Expand Down
20 changes: 14 additions & 6 deletions api/src/org/labkey/api/data/DbScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.labkey.api.module.ModuleResourceCache;
import org.labkey.api.module.ModuleResourceCaches;
import org.labkey.api.module.ResourceRootProvider;
import org.labkey.api.module.SupportedDatabase;
import org.labkey.api.query.QueryService;
import org.labkey.api.security.User;
import org.labkey.api.settings.AppProps;
Expand Down Expand Up @@ -316,7 +317,6 @@ public DbScope(String dsName, LabKeyDataSource dataSource) throws ServletExcepti
public static class LabKeyDataSource
{
public static final String LABKEY_DATA_SOURCE = "labkeyDataSource";
public static final String CPAS_DATA_SOURCE = "cpasDataSource";
private static final String DEFAULT_APPLICATION_NAME = "LabKey Server";

private final String _dsName; // DataSource name from application.properties
Expand Down Expand Up @@ -505,17 +505,25 @@ private Class<Driver> initializeDriver()
private static LabKeyDataSource setPrimaryDataSource(Map<String, LabKeyDataSource> dataSourceMap)
{
LabKeyDataSource primaryDS = dataSourceMap.get(LABKEY_DATA_SOURCE);
if (null == primaryDS)
primaryDS = dataSourceMap.get(CPAS_DATA_SOURCE);

if (null == primaryDS)
throw new ConfigurationException("You must have a DataSource named \"" + LABKEY_DATA_SOURCE + "\" defined in " + AppProps.getInstance().getWebappConfigurationFilename() + ".");

// When running in devMode, allow either database. When running in production mode, throw if the
// distribution doesn't support the primary database type.
if (!AppProps.getInstance().isDevMode())
{
SqlDialect primaryDialect = primaryDS.getDialect();
SupportedDatabase primaryDatabaseType = SupportedDatabase.get(primaryDialect);
if (!AppProps.getInstance().getDistributionSupportedDatabases().contains(primaryDatabaseType))
throw new ConfigurationException("This distribution (" + AppProps.getInstance().getDistributionFilename() + ") does not support " + primaryDialect.getProductName());
}

primaryDS.setPrimary();

if (primaryDS.isLogQueries())
{
LOG.warn("Ignoring unsupported parameter in " + AppProps.getInstance().getWebappConfigurationFilename() + " to log queries for LabKey DataSource \"" + primaryDS.getDsName() + "\"");
LOG.warn("Ignoring unsupported parameter in {} to log queries for LabKey DataSource \"{}\"", AppProps.getInstance().getWebappConfigurationFilename(), primaryDS.getDsName());
primaryDS.setLogQueries(false);
}

Expand Down Expand Up @@ -1563,12 +1571,12 @@ public static void initializeDataSources()
}
}

// Ensure that the labkeyDataSource (or cpasDataSource, for old installations) exists
// and create the associated database if it doesn't already exist.
// Ensure that the labkeyDataSource is defined and supported; designate it as the primary data source.
LabKeyDataSource primaryDS = LabKeyDataSource.setPrimaryDataSource(dataSources);
labkeyDsName = primaryDS.getDsName();
// Now that we've tagged the primary datasource we can prepare them all
dataSources.values().forEach(ds -> ds.getDialect().prepare(ds));
// Create the primary database if it doesn't already exist
ensureDatabase(primaryDS);
}
catch (Exception e)
Expand Down
29 changes: 6 additions & 23 deletions api/src/org/labkey/api/module/DefaultModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
package org.labkey.api.module;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -68,9 +71,6 @@
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.servlet.mvc.Controller;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -371,13 +371,11 @@ public final void clearWebPartFactories()
}
}


@Override
public void destroy()
{
}


@Override
@NotNull
public Collection<String> getSummary(Container c)
Expand Down Expand Up @@ -539,7 +537,6 @@ public final Set<SupportedDatabase> getSupportedDatabasesSet()
return _supportedDatabases;
}


// Used by Spring configuration reflection
@SuppressWarnings("UnusedDeclaration")
public final String getSupportedDatabases()
Expand All @@ -548,26 +545,16 @@ public final String getSupportedDatabases()
return StringUtils.join(set, ",");
}


// Used by Spring configuration reflection
@SuppressWarnings("UnusedDeclaration")
public final void setSupportedDatabases(String list)
{
Set<SupportedDatabase> supported = new HashSet<>();
String[] dbs = StringUtils.split(list, ',');

for (String db : dbs)
{
if (StringUtils.isEmpty(db))
continue;
supported.add(SupportedDatabase.valueOf(db));
}
Set<SupportedDatabase> supported = SupportedDatabase.parseSupportedDatabases(list);

if (!supported.isEmpty())
_supportedDatabases = supported;
}


@Override
public String getName()
{
Expand All @@ -582,7 +569,7 @@ public final void setName(String name)
if (!StringUtils.isEmpty(_name))
{
if (!_name.equals(name))
_log.error("Attempt to change name of module from " + _name + " to " + name + ".");
_log.error("Attempt to change name of module from {} to {}.", _name, name);
return;
}
_name = name;
Expand All @@ -602,7 +589,7 @@ public final void setSchemaVersion(Double schemaVersion)
if (null != _schemaVersion)
{
if (!_schemaVersion.equals(schemaVersion))
_log.error("Attempt to change version of module from " + _schemaVersion + " to " + schemaVersion + ".");
_log.error("Attempt to change version of module from {} to {}.", _schemaVersion, schemaVersion);
return;
}
_schemaVersion = schemaVersion;
Expand Down Expand Up @@ -1154,7 +1141,6 @@ public Controller getController(HttpServletRequest request, String name)
return getController(request, cls);
}


public Controller getController(@Nullable HttpServletRequest request, Class<? extends Controller> cls)
{
try
Expand Down Expand Up @@ -1272,7 +1258,6 @@ protected File computeResourceDirectory()
return null;
}


protected File getResourceDirectory(File dir)
{
File resourcesDir = new File(dir, "resources");
Expand All @@ -1295,14 +1280,12 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
_applicationContext = applicationContext;
}


@JsonIgnore
public ApplicationContext getApplicationContext()
{
return _applicationContext;
}


@Override
public boolean isAutoUninstall()
{
Expand Down
17 changes: 0 additions & 17 deletions api/src/org/labkey/api/module/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,6 @@ enum TabDisplayMode
DISPLAY_FOLDER_TYPE
}

enum SupportedDatabase
{
mssql, pgsql;

public static SupportedDatabase get(SqlDialect dialect)
{
if (dialect.isSqlServer())
return mssql;

if (dialect.isPostgreSQL())
return pgsql;

throw new IllegalStateException("Dialect not supported");
}
}


/**
* Perform any post-constructor initialization
*/
Expand Down
2 changes: 1 addition & 1 deletion api/src/org/labkey/api/module/ModuleLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ private void pruneModules(List<Module> modules)
{
Module core = getCoreModule();

Module.SupportedDatabase coreType = Module.SupportedDatabase.get(CoreSchema.getInstance().getSqlDialect());
SupportedDatabase coreType = SupportedDatabase.get(CoreSchema.getInstance().getSqlDialect());
for (Module module : modules)
{
if (module == core)
Expand Down
38 changes: 38 additions & 0 deletions api/src/org/labkey/api/module/SupportedDatabase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.labkey.api.module;

import org.apache.commons.lang3.EnumUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.labkey.api.data.dialect.SqlDialect;

import java.util.Arrays;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

public enum SupportedDatabase
{
mssql, pgsql;

public static SupportedDatabase get(SqlDialect dialect)
{
if (dialect.isPostgreSQL())
return pgsql;

if (dialect.isSqlServer())
return mssql;

throw new IllegalStateException("Dialect not supported: " + dialect.getProductName());
}

// databases parameter is a comma-separated list of databases: "pgsql, mssql", "mssql", "pgsql", etc.
public static @NotNull Set<SupportedDatabase> parseSupportedDatabases(@NotNull String databases)
{
return Arrays.stream(databases.split(","))
.map(StringUtils::trimToNull)
.filter(Objects::nonNull)
.map(db -> EnumUtils.getEnum(SupportedDatabase.class, db))
.filter(Objects::nonNull)
.collect(Collectors.toSet());
}
}
10 changes: 9 additions & 1 deletion api/src/org/labkey/api/settings/AppProps.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.jetbrains.annotations.Nullable;
import org.labkey.api.data.ContainerManager;
import org.labkey.api.module.DefaultModule;
import org.labkey.api.module.SupportedDatabase;
import org.labkey.api.util.ExceptionReportingLevel;
import org.labkey.api.util.Path;
import org.labkey.api.util.UsageReportingLevel;
Expand All @@ -28,6 +29,7 @@
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Stores basic site-wide configuration.
Expand Down Expand Up @@ -236,5 +238,11 @@ static WriteableAppProps getWriteableInstance()
@NotNull
List<String> getExternalRedirectHosts();

Map<StashedStartupProperties, StartupPropertyEntry> getStashedProperties();
Map<StashedStartupProperties, StartupPropertyEntry> getStashedStartupProperties();

@NotNull String getDistributionName();

@NotNull String getDistributionFilename();

@NotNull Set<SupportedDatabase> getDistributionSupportedDatabases();
}
Loading

0 comments on commit 42b18ec

Please sign in to comment.