Author: ddewolf
Date: Wed Dec 13 13:08:41 2006
New Revision: 486838

URL: http://svn.apache.org/viewvc?view=rev&rev=486838
Log:
Introducing ComponentContextMutator to allow for pluggable approach to context 
manipulation

Added:
    
struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/web/ComponentContextMutator.java
   (with props)
Modified:
    
struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/web/TilesDecorationFilter.java
    
struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/web/TilesDispatchServlet.java

Added: 
struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/web/ComponentContextMutator.java
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/web/ComponentContextMutator.java?view=auto&rev=486838
==============================================================================
--- 
struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/web/ComponentContextMutator.java
 (added)
+++ 
struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/web/ComponentContextMutator.java
 Wed Dec 13 13:08:41 2006
@@ -0,0 +1,32 @@
+/*
+ * $Id$
+ *
+ * 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.tiles.web;
+
+import org.apache.tiles.ComponentContext;
+
+import javax.servlet.ServletRequest;
+
+public interface ComponentContextMutator {
+
+    void mutate(ComponentContext context, ServletRequest request);
+
+}

Propchange: 
struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/web/ComponentContextMutator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/web/ComponentContextMutator.java
------------------------------------------------------------------------------
    svn:keywords = Id Author Date Rev

Modified: 
struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/web/TilesDecorationFilter.java
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/web/TilesDecorationFilter.java?view=diff&rev=486838&r1=486837&r2=486838
==============================================================================
--- 
struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/web/TilesDecorationFilter.java
 (original)
+++ 
struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/web/TilesDecorationFilter.java
 Wed Dec 13 13:08:41 2006
@@ -42,32 +42,31 @@
 /**
  * Decoration Filter.  Intercepts all requests and decorates them
  * with the configured definition.
- *
+ * <p/>
  * For example, given the following config:
  * <xmp>
-    <filter>
-        <filter-name>Tiles Decoration Filter</filter-name>
-        <filter-class>org.apache.tiles.web.TilesDecorationFilter</filter-class>
-        <init-param>
-            <param-name>definition</param-name>
-            <param-value>test.definition</param-value>
-        </init-param>
-        <init-param>
-            <param-name>attribute-name</param-name>
-            <param-value>body</param-value>
-        </init-param>
-    </filter>
-
-    <filter-mapping>
-        <filter-name>Tiles Decoration Filter</filter-name>
-        <url-pattern>/testdecorationfilter.jsp</url-pattern>
-        <dispatcher>REQUEST</dispatcher>
-    </filter-mapping>
+ * <filter>
+ * <filter-name>Tiles Decoration Filter</filter-name>
+ * <filter-class>org.apache.tiles.web.TilesDecorationFilter</filter-class>
+ * <init-param>
+ * <param-name>definition</param-name>
+ * <param-value>test.definition</param-value>
+ * </init-param>
+ * <init-param>
+ * <param-name>attribute-name</param-name>
+ * <param-value>body</param-value>
+ * </init-param>
+ * </filter>
+ * <p/>
+ * <filter-mapping>
+ * <filter-name>Tiles Decoration Filter</filter-name>
+ * <url-pattern>/testdecorationfilter.jsp</url-pattern>
+ * <dispatcher>REQUEST</dispatcher>
+ * </filter-mapping>
  * </xmp>
  * The filter will intercept all requests to the indicated url pattern
  * store the initial request path as the "body" component attribute
  * and then render the "test.definition" definition.
- *
  */
 public class TilesDecorationFilter implements Filter {
 
@@ -87,6 +86,7 @@
 
     private String definitionName = "layout";
 
+    private ComponentContextMutator mutator = null;
 
     public FilterConfig getFilterConfig() {
         return filterConfig;
@@ -99,14 +99,25 @@
     public void init(FilterConfig config) throws ServletException {
         filterConfig = config;
         String temp = config.getInitParameter("attribute-name");
-        if(temp != null) {
+        if (temp != null) {
             componentAttributeName = temp;
         }
 
         temp = config.getInitParameter("definition");
-        if(temp != null) {
+        if (temp != null) {
             definitionName = temp;
         }
+
+        temp = config.getInitParameter("mutator");
+        if(temp != null) {
+            try {
+                mutator = 
(ComponentContextMutator)Class.forName(temp).newInstance();
+            } catch (Exception e) {
+                throw new ServletException("Unable to instantiate specified 
context mutator.", e);
+            }
+        } else {
+            mutator = new DefaultMutator();
+        }
     }
 
     public void destroy() {
@@ -117,7 +128,7 @@
     public void doFilter(ServletRequest req, ServletResponse res, FilterChain 
filterChain)
         throws IOException, ServletException {
         TilesContainer container = 
TilesAccess.getContainer(getServletContext());
-        prepareComponentAttributes(req, container.getComponentContext(req, 
res));
+        mutator.mutate(container.getComponentContext(req, res), req);
         try {
             container.render(req, res, definitionName);
         } catch (TilesException e) {
@@ -125,13 +136,6 @@
         }
     }
 
-    protected void prepareComponentAttributes(ServletRequest req, 
ComponentContext ctx) {
-        ComponentAttribute attr = new ComponentAttribute();
-        attr.setType(ComponentAttribute.TEMPLATE);
-        attr.setName(componentAttributeName);
-        attr.setValue(getRequestBase(req));
-        ctx.putAttribute(componentAttributeName, attr);
-    }
 
 
     private String getRequestBase(ServletRequest request) {
@@ -144,6 +148,16 @@
         // As opposed to includes, if a forward occurs, it will update the 
servletPath property
         // and include the original as the request attribute.
         return ((HttpServletRequest) request).getServletPath();
+    }
+
+    class DefaultMutator implements ComponentContextMutator {
+        public void mutate(ComponentContext ctx, ServletRequest req) {
+            ComponentAttribute attr = new ComponentAttribute();
+            attr.setType(ComponentAttribute.TEMPLATE);
+            attr.setName(componentAttributeName);
+            attr.setValue(getRequestBase(req));
+            ctx.putAttribute(componentAttributeName, attr);
+        }
     }
 
 }

Modified: 
struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/web/TilesDispatchServlet.java
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/web/TilesDispatchServlet.java?view=diff&rev=486838&r1=486837&r2=486838
==============================================================================
--- 
struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/web/TilesDispatchServlet.java
 (original)
+++ 
struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/web/TilesDispatchServlet.java
 Wed Dec 13 13:08:41 2006
@@ -25,9 +25,11 @@
 import org.apache.commons.logging.LogFactory;
 import org.apache.tiles.TilesContainer;
 import org.apache.tiles.TilesException;
+import org.apache.tiles.ComponentContext;
 import org.apache.tiles.access.TilesAccess;
 
 import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -42,10 +44,28 @@
     private static final Log LOG =
         LogFactory.getLog(TilesDispatchServlet.class);
 
+    private ComponentContextMutator mutator;
+
+
+    public void init() throws ServletException {
+        super.init();
+        String temp = getInitParameter("mutator");
+        if(temp != null) {
+            try {
+                mutator = 
(ComponentContextMutator)Class.forName(temp).newInstance();
+            } catch (Exception e) {
+                throw new ServletException("Unable to instantiate specified 
context mutator.", e);
+            }
+        } else {
+            mutator = new DefaultMutator();
+        }
+    }
+
     protected void doGet(HttpServletRequest req, HttpServletResponse res)
         throws ServletException, IOException {
 
         TilesContainer container = 
TilesAccess.getContainer(getServletContext());
+        mutator.mutate(container.getComponentContext(req, res), req);
         try {
             String definition = getDefinitionName(req);
             if (LOG.isDebugEnabled()) {
@@ -73,5 +93,11 @@
         throws ServletException, IOException {
         LOG.info("Tiles dispatch request received. Redirecting POST to GET.");
         doGet(req, res);
+    }
+
+    class DefaultMutator implements ComponentContextMutator {
+        public void mutate(ComponentContext context, ServletRequest request) {
+            // noop;
+        }
     }
 }


Reply via email to