From d80a2d14f97e12ec6a72e77388795e01ec8dc9c3 Mon Sep 17 00:00:00 2001
From: Scott Murphy Heiberg
Date: Mon, 22 Jan 2024 17:22:14 -0800
Subject: [PATCH] Ability to load sitemesh3.xml from classpath. Ability to
configure via closure
---
.../config/ConfigurableSiteMeshFilter.java | 56 +++++++++++++++----
1 file changed, 45 insertions(+), 11 deletions(-)
diff --git a/sitemesh/src/main/java/org/sitemesh/config/ConfigurableSiteMeshFilter.java b/sitemesh/src/main/java/org/sitemesh/config/ConfigurableSiteMeshFilter.java
index f29dad0..ab90051 100644
--- a/sitemesh/src/main/java/org/sitemesh/config/ConfigurableSiteMeshFilter.java
+++ b/sitemesh/src/main/java/org/sitemesh/config/ConfigurableSiteMeshFilter.java
@@ -210,11 +210,10 @@ public void destroy() {
* To read from another place, override this.
*/
protected Map getConfigProperties(FilterConfig filterConfig) {
- Map initParams = new HashMap();
- for (Enumeration initParameterNames = filterConfig.getInitParameterNames(); initParameterNames.hasMoreElements();) {
- String key = (String) initParameterNames.nextElement();
- String value = filterConfig.getInitParameter(key).trim();
- initParams.put(key, value);
+ Map initParams = new HashMap<>();
+ for (Enumeration initParameterNames = filterConfig.getInitParameterNames(); initParameterNames.hasMoreElements();) {
+ String key = initParameterNames.nextElement();
+ initParams.put(key, filterConfig.getInitParameter(key).trim());
}
return initParams;
}
@@ -279,6 +278,30 @@ protected ObjectFactory getObjectFactory() {
return new ObjectFactory.Default();
}
+ private File loadFile(ServletContext servletContext, String fileName) {
+ if (fileName.startsWith("classpath:")) {
+ return null;
+ }
+ String filePath = servletContext.getRealPath(fileName);
+ return filePath != null? new File(filePath) : null;
+ }
+
+ private InputStream loadStream(ServletContext servletContext, String fileName) throws IOException {
+ if (fileName.startsWith("classpath:")) {
+ return loadClasspathStream(fileName.substring("classpath:".length()));
+ }
+ return servletContext.getResourceAsStream(fileName);
+ }
+
+ private InputStream loadClasspathStream(String classPathResource) {
+ // load the configuration
+ InputStream is = getClass().getClassLoader().getResourceAsStream(classPathResource);
+ if (is == null){ // load the configuration using another classloader
+ is = Thread.currentThread().getContextClassLoader().getResourceAsStream(classPathResource);
+ }
+ return is;
+ }
+
/**
* Load the XML config file. Will try a number of locations until it finds the file.
*
@@ -292,12 +315,10 @@ protected Element loadConfigXml(FilterConfig filterConfig, String configFilePath
try {
DocumentBuilder documentBuilder = Xml.getSecureDocumentBuilder();
- xmlConfigFile = new File(configFilePath);
-
ServletContext servletContext = filterConfig.getServletContext();
-
- if (servletContext.getRealPath(configFilePath) != null) {
- xmlConfigFile = new File(servletContext.getRealPath(configFilePath));
+ xmlConfigFile = loadFile(servletContext, configFilePath);
+ if (xmlConfigFile == null) {
+ xmlConfigFile = new File(configFilePath);
}
if (xmlConfigFile.canRead()) {
@@ -310,7 +331,7 @@ protected Element loadConfigXml(FilterConfig filterConfig, String configFilePath
throw new ServletException("Could not parse " + xmlConfigFile.getAbsolutePath(), e);
}
} else {
- InputStream stream = servletContext.getResourceAsStream(configFilePath);
+ InputStream stream = loadStream(servletContext, configFilePath);
if (stream == null) {
logger.config("No config file present - using defaults and init-params. Tried: "
+ xmlConfigFile.getAbsolutePath() + " and ServletContext:" + configFilePath);
@@ -368,5 +389,18 @@ public DecoratorSelector getDecoratorSelector() {
return ((SiteMeshFilter) filter).getDecoratorSelector();
}
+ public interface CustomConfiguration {
+ void applyCustomConfiguration(SiteMeshFilterBuilder builder);
+ }
+
+ public static ConfigurableSiteMeshFilter create(CustomConfiguration config) {
+ return new ConfigurableSiteMeshFilter() {
+ @Override
+ protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
+ config.applyCustomConfiguration(builder);
+ }
+ };
+ }
+
}
\ No newline at end of file