Skip to content

Commit

Permalink
Add support for custom webapp resources
Browse files Browse the repository at this point in the history
  • Loading branch information
labkey-tchad committed Jan 31, 2024
1 parent 38bf5fc commit f61d11e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
8 changes: 8 additions & 0 deletions server/configs/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ management.endpoints.web.exposure.include=*
# Don't show the Spring banner on startup
spring.main.banner-mode=off

# Optional - JMS configuration for remote ActiveMQ message management for distributed pipeline jobs
# https://www.labkey.org/Documentation/wiki-page.view?name=jmsQueue
#context.resources.jms.name=jms/ConnectionFactory
#context.resources.jms.type=org.apache.activemq.ActiveMQConnectionFactory
#context.resources.jms.factory=org.apache.activemq.jndi.JNDIReferenceFactory
#context.resources.jms.description=JMS Connection Factory
#context.resources.jms.brokerURL=vm://localhost?broker.persistent=false&broker.useJmx=false
#context.resources.jms.brokerName=LocalActiveMQBroker

# Turn on JSON-formatted HTTP access logging to stdout. See issue 48565
# https://tomcat.apache.org/tomcat-9.0-doc/config/valve.html#JSON_Access_Log_Valve
Expand Down
58 changes: 58 additions & 0 deletions server/embedded/src/org/labkey/embedded/LabKeyServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.apache.catalina.loader.WebappLoader;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.valves.JsonAccessLogValve;
import org.apache.tomcat.util.collections.CaseInsensitiveKeyMap;
import org.apache.tomcat.util.descriptor.web.ContextResource;
import org.labkey.bootstrap.ConfigException;
import org.springframework.boot.SpringApplication;
Expand All @@ -25,8 +26,10 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
Expand Down Expand Up @@ -144,6 +147,9 @@ protected TomcatWebServer getTomcatWebServer(Tomcat tomcat)
// Push the JDBC connection for the primary DB into the context so that the LabKey webapp finds them
getDataSourceResources(contextProperties).forEach(contextResource -> context.getNamingResources().addResource(contextResource));

// Add extra resources to context (e.g. LDAP, JMS)
getExtraContextResources(contextProperties).forEach(contextResource -> context.getNamingResources().addResource(contextResource));

// Add the SMTP config
context.getNamingResources().addResource(getMailResource());

Expand Down Expand Up @@ -246,6 +252,47 @@ private List<ContextResource> getDataSourceResources(ContextProperties props) th
return dataSourceResources;
}

private List<ContextResource> getExtraContextResources(ContextProperties contextProperties) throws ConfigException
{
List<ContextResource> contextResources = new ArrayList<>();
Map<String, Map<String, String>> resourceMaps = Objects.requireNonNullElse(contextProperties.getResources(), Collections.emptyMap());

for (Map.Entry<String, Map<String, String>> entry : resourceMaps.entrySet())
{
Map<String, String> resourceMap = new CaseInsensitiveKeyMap<>();
resourceMap.putAll(entry.getValue());
if (!resourceMap.containsKey("name"))
{
logger.error("Resource configuration error: Ignoring unnamed resource 'context.resources.%s'".formatted(entry.getKey()));
continue;
}
if (!resourceMap.containsKey("type"))
{
logger.error("Resource configuration error: type is not defined for resource '%s'".formatted(resourceMap.get("name")));
continue;
}

ContextResource contextResource = new ContextResource();
contextResource.setName(resourceMap.remove("name"));
contextResource.setType(resourceMap.remove("type"));
contextResource.setDescription(resourceMap.remove("description"));
contextResource.setLookupName(resourceMap.remove("lookupName"));
if (resourceMap.containsKey("scope"))
{
contextResource.setScope(resourceMap.remove("scope"));
}
contextResource.setAuth(Objects.requireNonNullElse(resourceMap.remove("auth"), "Container"));
for (Map.Entry<String, String> prop : resourceMap.entrySet())
{
contextResource.setProperty(prop.getKey(), prop.getValue());
}

contextResources.add(contextResource);
}

return contextResources;
}

private String getPropValue(Map<Integer, String> propValues, Integer resourceKey, String defaultValue, String propName)
{
if (propValues == null)
Expand Down Expand Up @@ -484,6 +531,7 @@ public static class ContextProperties
private Map<Integer, String> maxWaitMillis;
private Map<Integer, String> accessToUnderlyingConnectionAllowed;
private Map<Integer, String> validationQuery;
private Map<String, Map<String, String>> resources;

public List<String> getDataSourceName()
{
Expand Down Expand Up @@ -655,6 +703,16 @@ public void setValidationQuery(Map<Integer, String> validationQuery)
{
this.validationQuery = validationQuery;
}

public Map<String, Map<String, String>> getResources()
{
return resources;
}

public void setResources(Map<String, Map<String, String>> resources)
{
this.resources = resources;
}
}

@Configuration
Expand Down

0 comments on commit f61d11e

Please sign in to comment.