Renames class to match pattern used in Tiles
Project: http://git-wip-us.apache.org/repos/asf/struts/repo Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/6f98d776 Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/6f98d776 Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/6f98d776 Branch: refs/heads/master Commit: 6f98d776c398a036c563e863eacd59dedc9cd375 Parents: e56d8b2 Author: Lukasz Lenart <lukasz.len...@gmail.com> Authored: Tue Jan 12 22:16:06 2016 +0100 Committer: Lukasz Lenart <lukasz.len...@gmail.com> Committed: Tue Jan 12 22:16:06 2016 +0100 ---------------------------------------------------------------------- .../struts2/tiles/StrutsTilesInitializer.java | 2 +- ...StrutsWildcardServletApplicationContext.java | 117 +++++++++++++++++++ ...sWildcardServletTilesApplicationContext.java | 116 ------------------ 3 files changed, 118 insertions(+), 117 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts/blob/6f98d776/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 37fe1d1..d0f32f0 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 @@ -34,7 +34,7 @@ public class StrutsTilesInitializer extends AbstractTilesInitializer { @Override protected ApplicationContext createTilesApplicationContext(ApplicationContext preliminaryContext) { LOG.debug("Initializing Tiles wildcard support ..."); - return new StrutsWildcardServletTilesApplicationContext((ServletContext) preliminaryContext.getContext()); + return new StrutsWildcardServletApplicationContext((ServletContext) preliminaryContext.getContext()); } @Override http://git-wip-us.apache.org/repos/asf/struts/blob/6f98d776/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java ---------------------------------------------------------------------- diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java new file mode 100644 index 0000000..abd78a2 --- /dev/null +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java @@ -0,0 +1,117 @@ +/* + * 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.logging.log4j.message.MessageFormatMessage; +import org.apache.tiles.request.ApplicationResource; +import org.apache.tiles.request.locale.URLApplicationResource; +import org.apache.tiles.request.servlet.ServletApplicationContext; + +import javax.servlet.ServletContext; +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Collection; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.Locale; +import java.util.Map; +import java.util.Set; +import java.util.regex.Pattern; + +public class StrutsWildcardServletApplicationContext extends ServletApplicationContext { + + private static final Logger LOG = LogManager.getLogger(StrutsWildcardServletApplicationContext.class); + + private ResourceFinder finder; + + public StrutsWildcardServletApplicationContext(ServletContext context) { + super(context); + + Set<URL> 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<URL> 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 Collection<ApplicationResource> getResources(String path) { + Set<ApplicationResource> resources = new HashSet<>(); + + if (path.startsWith("/")) { + LOG.trace("Using ServletContext to load resource {}", path); + ApplicationResource resource = getResource(path); + if (resource != null) { + resources.add(resource); + } + } + + try { + resources.addAll(findResources(path)); + } catch (IOException e) { + LOG.error(new MessageFormatMessage("Cannot find resources for [{}]", path), e); + } + + return resources; + } + + protected Set<ApplicationResource> findResources(String path) throws IOException { + Set<ApplicationResource> resources = new HashSet<>(); + + LOG.trace("Using ResourceFinder to find matches for {}", path); + + Pattern pattern = WildcardUtil.compileWildcardPattern(path); + Map<String, URL> matches = finder.getResourcesMap(""); + + for (String resource : matches.keySet()) { + if (pattern.matcher(resource).matches()) { + URL url = matches.get(resource); + resources.add(new URLApplicationResource(url.getPath(), url)); + } + } + + LOG.trace("Found resources {} for path {}", resources, path); + return resources; + } + +} http://git-wip-us.apache.org/repos/asf/struts/blob/6f98d776/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 deleted file mode 100644 index d942e96..0000000 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletTilesApplicationContext.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * 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.logging.log4j.message.MessageFormatMessage; -import org.apache.tiles.request.ApplicationResource; -import org.apache.tiles.request.locale.URLApplicationResource; -import org.apache.tiles.request.servlet.ServletApplicationContext; - -import javax.servlet.ServletContext; -import java.io.File; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.Collection; -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 ServletApplicationContext { - - private static final Logger LOG = LogManager.getLogger(StrutsWildcardServletTilesApplicationContext.class); - - private ResourceFinder finder; - - public StrutsWildcardServletTilesApplicationContext(ServletContext context) { - super(context); - - Set<URL> 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<URL> 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 Collection<ApplicationResource> getResources(String path) { - Set<ApplicationResource> resources = new HashSet<>(); - - if (path.startsWith("/")) { - LOG.trace("Using ServletContext to load resource {}", path); - ApplicationResource resource = getResource(path); - if (resource != null) { - resources.add(resource); - } - } - - try { - resources.addAll(findResources(path)); - } catch (IOException e) { - LOG.error(new MessageFormatMessage("Cannot find resources for [{}]", path), e); - } - - return resources; - } - - protected Set<ApplicationResource> findResources(String path) throws IOException { - Set<ApplicationResource> resources = new HashSet<>(); - - LOG.trace("Using ResourceFinder to find matches for {}", path); - - Pattern pattern = WildcardUtil.compileWildcardPattern(path); - Map<String, URL> matches = finder.getResourcesMap(""); - - for (String resource : matches.keySet()) { - if (pattern.matcher(resource).matches()) { - URL url = matches.get(resource); - resources.add(new URLApplicationResource("", url)); - } - } - - LOG.trace("Found resources {} for path {}", resources, path); - return resources; - } - -}