Author: mrdon Date: Sat Sep 8 03:56:10 2007 New Revision: 573816 URL: http://svn.apache.org/viewvc?rev=573816&view=rev Log: Adding ability to load templates from classpath, in addition to web context WW-2146
Modified: struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/codebehind/CodebehindUnknownHandler.java struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/codebehind/CodebehindUnknownHandlerTest.java Modified: struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/codebehind/CodebehindUnknownHandler.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/codebehind/CodebehindUnknownHandler.java?rev=573816&r1=573815&r2=573816&view=diff ============================================================================== --- struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/codebehind/CodebehindUnknownHandler.java (original) +++ struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/codebehind/CodebehindUnknownHandler.java Sat Sep 8 03:56:10 2007 @@ -21,6 +21,7 @@ package org.apache.struts2.codebehind; import java.net.MalformedURLException; +import java.net.URL; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -32,6 +33,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.struts2.util.ClassLoaderUtils; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; @@ -107,7 +109,7 @@ } String path = string(pathPrefix, actionName, "." , ext); try { - if (servletContext.getResource(path) != null) { + if (locateTemplate(path) != null) { actionConfig = buildActionConfig(path, namespace, actionName, resultsByExtension.get(ext)); break; } @@ -148,7 +150,7 @@ } String path = string(pathPrefix, actionName, "-", resultCode, "." , ext); try { - if (servletContext.getResource(path) != null) { + if (locateTemplate(path) != null) { result = buildResult(path, resultCode, resultsByExtension.get(ext), actionContext); break; } @@ -158,7 +160,7 @@ path = string(pathPrefix, actionName, "." , ext); try { - if (servletContext.getResource(path) != null) { + if (locateTemplate(path) != null) { result = buildResult(path, resultCode, resultsByExtension.get(ext), actionContext); break; } @@ -209,6 +211,22 @@ } return prefix + ns; } + + URL locateTemplate(String path) throws MalformedURLException { + URL template = servletContext.getResource(path); + if (template != null) { + if (LOG.isDebugEnabled()) { + LOG.debug("Loaded template '" + path + "' from servlet context."); + } + } else { + template = ClassLoaderUtils.getResource(path, getClass()); + if (template != null && LOG.isDebugEnabled()) { + LOG.debug("Loaded template '" + path + "' from class path."); + } + } + return template; + } + /** * Not used Modified: struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/codebehind/CodebehindUnknownHandlerTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/codebehind/CodebehindUnknownHandlerTest.java?rev=573816&r1=573815&r2=573816&view=diff ============================================================================== --- struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/codebehind/CodebehindUnknownHandlerTest.java (original) +++ struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/codebehind/CodebehindUnknownHandlerTest.java Sat Sep 8 03:56:10 2007 @@ -20,6 +20,8 @@ */ package org.apache.struts2.codebehind; +import java.net.MalformedURLException; +import java.net.URL; import java.util.HashMap; import javax.servlet.ServletContext; @@ -28,6 +30,7 @@ import org.apache.struts2.config.NullResult; import org.apache.struts2.dispatcher.ServletDispatcherResult; +import com.mockobjects.dynamic.C; import com.mockobjects.dynamic.Mock; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; @@ -71,6 +74,22 @@ assertEquals("/foo/", handler.determinePath("/", "/foo")); assertEquals("/foo/", handler.determinePath("/", "/foo/")); assertEquals("/foo/", handler.determinePath("/", "foo")); + } + + public void testLocateTemplate() throws MalformedURLException { + URL url = new URL("file:/foo.xml"); + mockServletContext.expectAndReturn("getResource", C.args(C.eq("/foo.xml")), url); + assertEquals(url, handler.locateTemplate("/foo.xml")); + mockServletContext.verify(); + + } + + public void testLocateTemplateFromClasspath() throws MalformedURLException { + mockServletContext.expectAndReturn("getResource", C.args(C.eq("struts-plugin.xml")), null); + URL url = handler.locateTemplate("struts-plugin.xml"); + assertNotNull(url); + assertTrue(url.toString().endsWith("struts-plugin.xml")); + mockServletContext.verify(); } public static class SomeResult implements Result {