Author: tmjee Date: Sun Jul 2 01:41:25 2006 New Revision: 418563 URL: http://svn.apache.org/viewvc?rev=418563&view=rev Log: WW-1368 - theme.properties now read from filesystem, if this fails it will try to read from the classpath
Added: struts/struts2/trunk/core/src/test/java/org/apache/struts2/components/template/ struts/struts2/trunk/core/src/test/java/org/apache/struts2/components/template/BaseTemplateEngineTest.java (with props) struts/struts2/trunk/core/src/test/resources/org/apache/struts2/components/template/ struts/struts2/trunk/core/src/test/resources/org/apache/struts2/components/template/dummy.properties (with props) struts/struts2/trunk/core/src/test/resources/org/apache/struts2/components/template/theme1/ struts/struts2/trunk/core/src/test/resources/org/apache/struts2/components/template/theme1/themeThroughClassPath.properties (with props) struts/struts2/trunk/core/src/test/resources/org/apache/struts2/components/template/theme1/themeThroughFileSystem.properties (with props) 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=418563&r1=418562&r2=418563&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 Sun Jul 2 01:41:25 2006 @@ -21,6 +21,9 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; @@ -31,16 +34,38 @@ * Base class for template engines. */ public abstract class BaseTemplateEngine implements TemplateEngine { + private static final Log LOG = LogFactory.getLog(BaseTemplateEngine.class); + + /** The default theme properties file name. Default is 'theme.properties' */ + public static final String DEFAULT_THEME_PROPERTIES_FILE_NAME = "theme.properties"; - final Map themeProps = new HashMap(); + final Map<String, Properties> themeProps = new HashMap<String, Properties>(); public Map getThemeProps(Template template) { synchronized (themeProps) { Properties props = (Properties) themeProps.get(template.getTheme()); if (props == null) { - String propName = template.getDir() + "/" + template.getTheme() + "/theme.properties"; - InputStream is = ClassLoaderUtil.getResourceAsStream(propName, getClass()); + String propName = template.getDir() + "/" + template.getTheme() + "/"+getThemePropertiesFileName(); + +// WW-1292 + // let's try getting it from the filesystem + File propFile = new File(propName); + InputStream is = null; + try { + if (propFile.exists()) { + is = new FileInputStream(propFile); + } + } + catch(FileNotFoundException e) { + LOG.warn("Unable to find file in filesystem ["+propFile.getAbsolutePath()+"]"); + } + + if (is == null) { + // if its not in filesystem. let's try the classpath + is = ClassLoaderUtil.getResourceAsStream(propName, getClass()); + } + props = new Properties(); if (is != null) { @@ -65,6 +90,10 @@ } return t; + } + + protected String getThemePropertiesFileName() { + return DEFAULT_THEME_PROPERTIES_FILE_NAME; } protected abstract String getSuffix(); Added: struts/struts2/trunk/core/src/test/java/org/apache/struts2/components/template/BaseTemplateEngineTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/components/template/BaseTemplateEngineTest.java?rev=418563&view=auto ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/components/template/BaseTemplateEngineTest.java (added) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/components/template/BaseTemplateEngineTest.java Sun Jul 2 01:41:25 2006 @@ -0,0 +1,88 @@ +/* + * $Id: BaseTemplateEngine.java 418521 2006-07-01 23:36:50Z tmjee $ + * + * Copyright 2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.struts2.components.template; + +import java.io.File; +import java.net.URL; +import java.util.Map; + +import org.apache.struts2.components.template.BaseTemplateEngine; +import org.apache.struts2.components.template.Template; +import org.apache.struts2.components.template.TemplateEngine; +import org.apache.struts2.components.template.TemplateRenderingContext; + +import junit.framework.TestCase; + +/** + * Test case for BaseTemplateEngine + */ +public class BaseTemplateEngineTest extends TestCase { + +public void testGetThemePropsThroughFileSystem() throws Exception { + + URL dummyResourceUrl = getClass().getResource("dummy.properties"); + File dummyResourceFile = new File(dummyResourceUrl.getFile()); + String themePropertiesDir = dummyResourceFile.getParent(); + + System.out.println("dummy resource url="+dummyResourceUrl); + System.out.println("resource file="+dummyResourceFile); + System.out.println("theme properties dir="+themePropertiesDir); + + assertTrue(dummyResourceFile.exists()); + assertNotNull(themePropertiesDir); + + Template template = new Template(themePropertiesDir, "theme1", "template1"); + + TemplateEngine templateEngine = new InnerBaseTemplateEngine("themeThroughFileSystem.properties"); + Map propertiesMap = templateEngine.getThemeProps(template); + + assertNotNull(propertiesMap); + assertTrue(propertiesMap.size() > 0); + + } + + public void testGetThemePropsThroughClasspath() throws Exception { + + Template template = new Template("org/apache/struts2/components/template", "theme1", "template2"); + TemplateEngine templateEngine = new InnerBaseTemplateEngine("themeThroughClassPath.properties"); + Map propertiesMap = templateEngine.getThemeProps(template); + + assertNotNull(propertiesMap); + assertTrue(propertiesMap.size() > 0); + } + + public class InnerBaseTemplateEngine extends BaseTemplateEngine { + + private String themePropertiesFileName; + + public InnerBaseTemplateEngine(String themePropertiesFileName) { + this.themePropertiesFileName = themePropertiesFileName; + } + + protected String getSuffix() { + return "ftl"; + } + + public void renderTemplate(TemplateRenderingContext templateContext) throws Exception { + } + + protected String getThemePropertiesFileName() { + return this.themePropertiesFileName; + } + } +} Propchange: struts/struts2/trunk/core/src/test/java/org/apache/struts2/components/template/BaseTemplateEngineTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: struts/struts2/trunk/core/src/test/resources/org/apache/struts2/components/template/dummy.properties URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/resources/org/apache/struts2/components/template/dummy.properties?rev=418563&view=auto ============================================================================== (empty) Propchange: struts/struts2/trunk/core/src/test/resources/org/apache/struts2/components/template/dummy.properties ------------------------------------------------------------------------------ svn:eol-style = native Added: struts/struts2/trunk/core/src/test/resources/org/apache/struts2/components/template/theme1/themeThroughClassPath.properties URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/resources/org/apache/struts2/components/template/theme1/themeThroughClassPath.properties?rev=418563&view=auto ============================================================================== --- struts/struts2/trunk/core/src/test/resources/org/apache/struts2/components/template/theme1/themeThroughClassPath.properties (added) +++ struts/struts2/trunk/core/src/test/resources/org/apache/struts2/components/template/theme1/themeThroughClassPath.properties Sun Jul 2 01:41:25 2006 @@ -0,0 +1 @@ +parent=xhtml Propchange: struts/struts2/trunk/core/src/test/resources/org/apache/struts2/components/template/theme1/themeThroughClassPath.properties ------------------------------------------------------------------------------ svn:eol-style = native Added: struts/struts2/trunk/core/src/test/resources/org/apache/struts2/components/template/theme1/themeThroughFileSystem.properties URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/resources/org/apache/struts2/components/template/theme1/themeThroughFileSystem.properties?rev=418563&view=auto ============================================================================== --- struts/struts2/trunk/core/src/test/resources/org/apache/struts2/components/template/theme1/themeThroughFileSystem.properties (added) +++ struts/struts2/trunk/core/src/test/resources/org/apache/struts2/components/template/theme1/themeThroughFileSystem.properties Sun Jul 2 01:41:25 2006 @@ -0,0 +1 @@ +parent=xhtml Propchange: struts/struts2/trunk/core/src/test/resources/org/apache/struts2/components/template/theme1/themeThroughFileSystem.properties ------------------------------------------------------------------------------ svn:eol-style = native