struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject [13/50] [abbrv] struts git commit: Ports solution from 2.3.x branch
Date Fri, 15 Jul 2016 13:18:40 GMT
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 <lukasz.lenart@gmail.com>
Authored: Tue Jan 12 08:51:03 2016 +0100
Committer: Lukasz Lenart <lukasz.lenart@gmail.com>
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<T> resolver = new PrefixedPatternDefinitionResolver<>();
+        PrefixedPatternDefinitionResolver<T> resolver = new PrefixedPatternDefinitionResolver<T>();
         resolver.registerDefinitionPatternMatcherFactory(PATTERN_WILDCARD, wildcardFactory);
         resolver.registerDefinitionPatternMatcherFactory(PATTERN_REGEXP, regexpFactory);
 
@@ -189,19 +199,9 @@ public class StrutsTilesContainerFactory extends BasicTilesContainerFactory
{
     }
 
     @Override
-    protected List<URL> getSourceURLs(TilesApplicationContext applicationContext,
-                                      TilesRequestContextFactory contextFactory) {
+    protected List<URL> getSourceURLs(TilesApplicationContext applicationContext, TilesRequestContextFactory
contextFactory) {
         try {
-            Set<URL> finalSet = new HashSet<>();
-            Set<URL> webINFSet = applicationContext.getResources("/WEB-INF/**/tiles*.xml");
-            Set<URL> metaINFSet = applicationContext.getResources("classpath*:META-INF/**/tiles*.xml");
-
-            if (webINFSet != null) {
-                finalSet.addAll(webINFSet);
-            }
-            if (metaINFSet != null) {
-                finalSet.addAll(metaINFSet);
-            }
+            Set<URL> 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<String, String> 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<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 Set<URL> getResources(String path) throws IOException {
+        Set<URL> 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<URL> findResources(String path) throws IOException {
+        Set<URL> resources = new HashSet<>();
+
+        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;
+    }
+
+}


Mime
View raw message