Implements ApplicationContext which supports wildcards
Project: http://git-wip-us.apache.org/repos/asf/struts/repo Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/f721d549 Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/f721d549 Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/f721d549 Branch: refs/heads/support-2-3 Commit: f721d5490cfc1ed9f1a441ea90de82e31bb32351 Parents: db318c2 Author: Lukasz Lenart <lukasz.len...@gmail.com> Authored: Sat Dec 19 10:13:57 2015 +0100 Committer: Lukasz Lenart <lukasz.len...@gmail.com> Committed: Sat Dec 19 10:13:57 2015 +0100 ---------------------------------------------------------------------- ...sWildcardServletTilesApplicationContext.java | 75 ++++++++++++++++++++ 1 file changed, 75 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts/blob/f721d549/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletTilesApplicationContext.java ---------------------------------------------------------------------- diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletTilesApplicationContext.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletTilesApplicationContext.java new file mode 100644 index 0000000..9482259 --- /dev/null +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletTilesApplicationContext.java @@ -0,0 +1,75 @@ +package org.apache.struts2.tiles; + +import com.opensymphony.xwork2.config.ConfigurationException; +import com.opensymphony.xwork2.util.WildcardUtil; +import com.opensymphony.xwork2.util.finder.ResourceFinder; +import com.opensymphony.xwork2.util.logging.Logger; +import com.opensymphony.xwork2.util.logging.LoggerFactory; +import org.apache.tiles.servlet.context.ServletTilesApplicationContext; + +import javax.servlet.ServletContext; +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.regex.Pattern; + +public class StrutsWildcardServletTilesApplicationContext extends ServletTilesApplicationContext { + + private static final Logger LOG = LoggerFactory.getLogger(StrutsWildcardServletTilesApplicationContext.class); + + private ResourceFinder finder; + + public StrutsWildcardServletTilesApplicationContext(ServletContext context) { + super(context); + + Set<URL> urls = new HashSet<URL>(); + for (Object path : context.getResourcePaths("/")) { + try { + URL url = new File(context.getRealPath(String.valueOf(path))).toURI().toURL(); + urls.add(url); + } catch (MalformedURLException e) { + throw new ConfigurationException(e); + } + } + + finder = new ResourceFinder(urls.toArray(new URL[urls.size()])); + } + + public Set<URL> getResources(String path) throws IOException { + Set<URL> resources = new HashSet<URL>(); + + if (path.startsWith("/")) { + LOG.trace("Using ServletContext to load resource #0", path); + URL resource = getResource(path); + if (resource != null) { + resources.add(resource); + } + } + resources.addAll(findResources(path)); + + return resources; + } + + protected Set<URL> findResources(String path) throws IOException { + Set<URL> resources = new HashSet<URL>(); + + LOG.trace("Using ResourceFinder to find matches for #0", path); + + Pattern pattern = WildcardUtil.compileWildcardPattern(path); + Map<String, URL> matches = finder.getResourcesMap(""); + + for (String resource : matches.keySet()) { + if (pattern.matcher(resource).matches()) { + resources.add(matches.get(resource)); + } + } + + LOG.trace("Found resources #0 for path #1", resources, path); + return resources; + } + +}