Author: lukaszlenart Date: Tue Dec 17 07:52:13 2013 New Revision: 1551478 URL: http://svn.apache.org/r1551478 Log: WW-4260 Uses ServletContext to load themes.properties from
Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/template/BaseTemplateEngine.java Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/template/BaseTemplateEngine.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/template/BaseTemplateEngine.java?rev=1551478&r1=1551477&r2=1551478&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/template/BaseTemplateEngine.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/template/BaseTemplateEngine.java Tue Dec 17 07:52:13 2013 @@ -24,7 +24,9 @@ package org.apache.struts2.components.te import com.opensymphony.xwork2.util.ClassLoaderUtil; import com.opensymphony.xwork2.util.logging.Logger; import com.opensymphony.xwork2.util.logging.LoggerFactory; +import org.apache.struts2.ServletActionContext; +import javax.servlet.ServletContext; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -33,6 +35,7 @@ import java.io.InputStream; import java.util.HashMap; import java.util.Map; import java.util.Properties; +import java.util.concurrent.ConcurrentHashMap; /** * Base class for template engines. @@ -46,17 +49,17 @@ public abstract class BaseTemplateEngine */ public static final String DEFAULT_THEME_PROPERTIES_FILE_NAME = "theme.properties"; - final Map<String, Properties> themeProps = new HashMap<String, Properties>(); + private final Map<String, Properties> themeProps = new ConcurrentHashMap<String, Properties>(); public Map getThemeProps(Template template) { - synchronized (themeProps) { - Properties props = themeProps.get(template.getTheme()); - if (props == null) { + Properties props = themeProps.get(template.getTheme()); + if (props == null) { + synchronized (themeProps) { props = readNewProperties(template); themeProps.put(template.getTheme(), props); } - return props; } + return props; } private Properties readNewProperties(Template template) { @@ -78,9 +81,23 @@ public abstract class BaseTemplateEngine if (is == null) { is = readPropertyFromClasspath(propName); } + if (is == null) { + is = readPropertyUsingServletContext(propName); + } return is; } + private InputStream readPropertyUsingServletContext(String propName) { + ServletContext servletContext = ServletActionContext.getServletContext(); + String path = propName.startsWith("/") ? propName : "/" + propName; + if (servletContext != null) { + return servletContext.getResourceAsStream(path); + } else { + LOG.warn("ServletContext is null, cannot obtain #0", path); + return null; + } + } + /** * if its not in filesystem. let's try the classpath */ @@ -109,11 +126,7 @@ public abstract class BaseTemplateEngine } private String buildPropertyFilename(Template template) { - return new StringBuilder().append(template.getDir()) - .append("/") - .append(template.getTheme()) - .append("/") - .append(getThemePropertiesFileName()).toString(); + return template.getDir() + "/" + template.getTheme() + "/" + getThemePropertiesFileName(); } /**