Skip to content

Commit

Permalink
Ability to load sitemesh3.xml from classpath. Ability to configure vi…
Browse files Browse the repository at this point in the history
…a closure
  • Loading branch information
codeconsole committed Jan 23, 2024
1 parent a4192fb commit d80a2d1
Showing 1 changed file with 45 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,10 @@ public void destroy() {
* To read from another place, override this.</p>
*/
protected Map<String, String> getConfigProperties(FilterConfig filterConfig) {
Map<String, String> initParams = new HashMap<String, String>();
for (Enumeration initParameterNames = filterConfig.getInitParameterNames(); initParameterNames.hasMoreElements();) {
String key = (String) initParameterNames.nextElement();
String value = filterConfig.getInitParameter(key).trim();
initParams.put(key, value);
Map<String, String> initParams = new HashMap<>();
for (Enumeration<String> initParameterNames = filterConfig.getInitParameterNames(); initParameterNames.hasMoreElements();) {
String key = initParameterNames.nextElement();
initParams.put(key, filterConfig.getInitParameter(key).trim());
}
return initParams;
}
Expand Down Expand Up @@ -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.
* <pre>
Expand All @@ -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()) {
Expand All @@ -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);
Expand Down Expand Up @@ -368,5 +389,18 @@ public DecoratorSelector<WebAppContext> 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);
}
};
}

}

0 comments on commit d80a2d1

Please sign in to comment.