Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id B2859200B45 for ; Fri, 15 Jul 2016 15:18:38 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id B1253160A89; Fri, 15 Jul 2016 13:18:38 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id BA9AF160A61 for ; Fri, 15 Jul 2016 15:18:37 +0200 (CEST) Received: (qmail 32878 invoked by uid 500); 15 Jul 2016 13:18:30 -0000 Mailing-List: contact commits-help@struts.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@struts.apache.org Delivered-To: mailing list commits@struts.apache.org Received: (qmail 31966 invoked by uid 99); 15 Jul 2016 13:18:29 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 15 Jul 2016 13:18:29 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 2D4FAEE68B; Fri, 15 Jul 2016 13:18:29 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: lukaszlenart@apache.org To: commits@struts.apache.org Date: Fri, 15 Jul 2016 13:18:40 -0000 Message-Id: In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [13/50] [abbrv] struts git commit: Ports solution from 2.3.x branch archived-at: Fri, 15 Jul 2016 13:18:38 -0000 Ports solution from 2.3.x branch Project: http://git-wip-us.apache.org/repos/asf/struts/repo Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/bdf4f0b5 Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/bdf4f0b5 Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/bdf4f0b5 Branch: refs/heads/master Commit: bdf4f0b534fa3f6c620be6e4c500b1bad3579baf Parents: adb8a13 Author: Lukasz Lenart Authored: Tue Jan 12 08:51:03 2016 +0100 Committer: Lukasz Lenart Committed: Tue Jan 19 16:18:01 2016 +0100 ---------------------------------------------------------------------- .../tiles/StrutsTilesContainerFactory.java | 37 ++++--- .../struts2/tiles/StrutsTilesInitializer.java | 3 +- ...sWildcardServletTilesApplicationContext.java | 106 +++++++++++++++++++ 3 files changed, 129 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts/blob/bdf4f0b5/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java ---------------------------------------------------------------------- diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java index 1b01a8d..785082b 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java @@ -27,6 +27,7 @@ import org.apache.tiles.TilesContainer; import org.apache.tiles.context.ChainedTilesRequestContextFactory; import org.apache.tiles.context.TilesRequestContext; import org.apache.tiles.context.TilesRequestContextFactory; +import org.apache.tiles.definition.DefinitionsFactory; import org.apache.tiles.definition.DefinitionsFactoryException; import org.apache.tiles.definition.pattern.DefinitionPatternMatcherFactory; import org.apache.tiles.definition.pattern.PatternDefinitionResolver; @@ -59,6 +60,7 @@ import org.apache.tiles.renderer.AttributeRenderer; import org.apache.tiles.renderer.TypeDetectingAttributeRenderer; import org.apache.tiles.renderer.impl.BasicRendererFactory; import org.apache.tiles.renderer.impl.ChainedDelegateAttributeRenderer; +import org.apache.tiles.servlet.context.ServletUtil; import org.apache.tiles.util.URLUtil; import javax.el.ArrayELResolver; @@ -68,9 +70,9 @@ import javax.el.ELResolver; import javax.el.ListELResolver; import javax.el.MapELResolver; import javax.el.ResourceBundleELResolver; +import javax.servlet.ServletContext; import java.io.IOException; import java.net.URL; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -98,9 +100,17 @@ public class StrutsTilesContainerFactory extends BasicTilesContainerFactory { public static final String PATTERN_WILDCARD = "WILDCARD"; public static final String PATTERN_REGEXP = "REGEXP"; + /** + * Default pattern to be used to collect Tiles definitions if user didn't configure any + */ + public static final String TILES_DEFAULT_PATTERN = "tiles*.xml"; + @Override protected BasicTilesContainer instantiateContainer(TilesApplicationContext applicationContext) { - return new CachingTilesContainer(); + CachingTilesContainer tilesContainer = new CachingTilesContainer(); + ServletContext servletContext = (ServletContext) applicationContext.getContext(); + ServletUtil.setContainer(servletContext, tilesContainer); + return tilesContainer; } @Override @@ -181,7 +191,7 @@ public class StrutsTilesContainerFactory extends BasicTilesContainerFactory { DefinitionPatternMatcherFactory wildcardFactory = new WildcardDefinitionPatternMatcherFactory(); DefinitionPatternMatcherFactory regexpFactory = new RegexpDefinitionPatternMatcherFactory(); - PrefixedPatternDefinitionResolver resolver = new PrefixedPatternDefinitionResolver<>(); + PrefixedPatternDefinitionResolver resolver = new PrefixedPatternDefinitionResolver(); resolver.registerDefinitionPatternMatcherFactory(PATTERN_WILDCARD, wildcardFactory); resolver.registerDefinitionPatternMatcherFactory(PATTERN_REGEXP, regexpFactory); @@ -189,19 +199,9 @@ public class StrutsTilesContainerFactory extends BasicTilesContainerFactory { } @Override - protected List getSourceURLs(TilesApplicationContext applicationContext, - TilesRequestContextFactory contextFactory) { + protected List getSourceURLs(TilesApplicationContext applicationContext, TilesRequestContextFactory contextFactory) { try { - Set finalSet = new HashSet<>(); - Set webINFSet = applicationContext.getResources("/WEB-INF/**/tiles*.xml"); - Set metaINFSet = applicationContext.getResources("classpath*:META-INF/**/tiles*.xml"); - - if (webINFSet != null) { - finalSet.addAll(webINFSet); - } - if (metaINFSet != null) { - finalSet.addAll(metaINFSet); - } + Set finalSet = applicationContext.getResources(getTilesDefinitionPattern(applicationContext.getInitParams())); return URLUtil.getBaseTilesDefinitionURLs(finalSet); } catch (IOException e) { @@ -209,6 +209,13 @@ public class StrutsTilesContainerFactory extends BasicTilesContainerFactory { } } + protected String getTilesDefinitionPattern(Map params) { + if (params.containsKey(DefinitionsFactory.DEFINITIONS_CONFIG)) { + return params.get(DefinitionsFactory.DEFINITIONS_CONFIG); + } + return TILES_DEFAULT_PATTERN; + } + protected ELAttributeEvaluator createELEvaluator(TilesApplicationContext applicationContext) { ELAttributeEvaluator evaluator = new ELAttributeEvaluator(); http://git-wip-us.apache.org/repos/asf/struts/blob/bdf4f0b5/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesInitializer.java ---------------------------------------------------------------------- diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesInitializer.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesInitializer.java index b2e2700..0ff33c7 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesInitializer.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesInitializer.java @@ -23,7 +23,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.tiles.TilesApplicationContext; import org.apache.tiles.factory.AbstractTilesContainerFactory; -import org.apache.tiles.servlet.wildcard.WildcardServletTilesApplicationContext; import org.apache.tiles.startup.AbstractTilesInitializer; import javax.servlet.ServletContext; @@ -35,7 +34,7 @@ public class StrutsTilesInitializer extends AbstractTilesInitializer { @Override protected TilesApplicationContext createTilesApplicationContext(TilesApplicationContext preliminaryContext) { LOG.debug("Initializing Tiles wildcard support ..."); - return new WildcardServletTilesApplicationContext((ServletContext) preliminaryContext.getContext()); + return new StrutsWildcardServletTilesApplicationContext((ServletContext) preliminaryContext.getContext()); } @Override http://git-wip-us.apache.org/repos/asf/struts/blob/bdf4f0b5/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..2328f06 --- /dev/null +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletTilesApplicationContext.java @@ -0,0 +1,106 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.tiles; + +import com.opensymphony.xwork2.config.ConfigurationException; +import com.opensymphony.xwork2.util.WildcardUtil; +import com.opensymphony.xwork2.util.finder.ResourceFinder; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +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.Enumeration; +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 = LogManager.getLogger(StrutsWildcardServletTilesApplicationContext.class); + + private ResourceFinder finder; + + public StrutsWildcardServletTilesApplicationContext(ServletContext context) { + super(context); + + Set urls = new HashSet<>(); + + 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); + } + } + + try { + Enumeration resources = getClass().getClassLoader().getResources("/"); + while (resources.hasMoreElements()) { + URL resource = resources.nextElement(); + urls.add(resource); + } + } catch (IOException e) { + throw new ConfigurationException(e); + } + + finder = new ResourceFinder(urls.toArray(new URL[urls.size()])); + } + + public Set getResources(String path) throws IOException { + Set resources = new HashSet<>(); + + 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 findResources(String path) throws IOException { + Set resources = new HashSet<>(); + + LOG.trace("Using ResourceFinder to find matches for #0", path); + + Pattern pattern = WildcardUtil.compileWildcardPattern(path); + Map 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; + } + +}