You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by dd...@apache.org on 2006/11/10 20:14:35 UTC

svn commit: r473432 - in /struts/sandbox/trunk/tiles: tiles-api/src/main/java/org/apache/tiles/ tiles-core/src/main/java/org/apache/tiles/context/ tiles-core/src/main/java/org/apache/tiles/context/jsp/ tiles-core/src/main/java/org/apache/tiles/context/...

Author: ddewolf
Date: Fri Nov 10 11:14:35 2006
New Revision: 473432

URL: http://svn.apache.org/viewvc?view=rev&rev=473432
Log:
Refactoring AttributeTag.  Removing more logic from tags and putting it into the container

Added:
    struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/TilesRequestContextWrapper.java   (with props)
Modified:
    struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/TilesContainer.java
    struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/TilesRequestContext.java
    struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/jsp/JspTilesRequestContext.java
    struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/portlet/PortletTilesRequestContext.java
    struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/servlet/ServletTilesRequestContext.java
    struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java
    struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/taglib/AttributeTag.java
    struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/definition/MockOnlyLocaleTilesContext.java

Modified: struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/TilesContainer.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/TilesContainer.java?view=diff&rev=473432&r1=473431&r2=473432
==============================================================================
--- struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/TilesContainer.java (original)
+++ struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/TilesContainer.java Fri Nov 10 11:14:35 2006
@@ -21,6 +21,7 @@
 
 import javax.servlet.jsp.PageContext;
 import java.util.Map;
+import java.io.IOException;
 
 /**
  * An encapsulation of the tiles framework.  This interface is
@@ -96,6 +97,15 @@
      * @throws TilesException is processing fails.
      */
     void render(PageContext pageContext, String definition) throws TilesException;
+
+    /**
+     * Render the given ComponentAttribute.
+     * @param pageContext
+     * @param attribute
+     * @throws TilesException
+     */
+    void render(PageContext pageContext, ComponentAttribute attribute)
+        throws TilesException, IOException;
 
     /**
      * Determine whether or not the definition exists.

Modified: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/TilesRequestContext.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/TilesRequestContext.java?view=diff&rev=473432&r1=473431&r2=473432
==============================================================================
--- struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/TilesRequestContext.java (original)
+++ struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/TilesRequestContext.java Fri Nov 10 11:14:35 2006
@@ -59,12 +59,12 @@
     /**
      * Dispatches the request to a specified path.
      */
-    void dispatch(String path) throws IOException, Exception;
+    void dispatch(String path) throws IOException;
 
     /**
      * Includes the response from the specified URL in the current response output.
      */
-    void include(String path) throws IOException, Exception;
+    void include(String path) throws IOException;
 
     /**
      * Return an immutable Map that maps request parameter names to the first
@@ -89,4 +89,14 @@
      * @return
      */
     boolean isUserInRole(String role);
+
+    /**
+     * Get the underlying request.
+     */
+    Object getRequest();
+
+    /**
+     * Get the underlying response.
+     */
+    Object getResponse();
 }

Added: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/TilesRequestContextWrapper.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/TilesRequestContextWrapper.java?view=auto&rev=473432
==============================================================================
--- struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/TilesRequestContextWrapper.java (added)
+++ struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/TilesRequestContextWrapper.java Fri Nov 10 11:14:35 2006
@@ -0,0 +1,89 @@
+/*
+ * 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.context;
+
+import java.util.Map;
+import java.util.Locale;
+import java.io.IOException;
+
+/**
+ * Delegate for ease of customization.
+ *
+ * @since Tiles 2.0
+ * @version $Rev$
+ */
+public class TilesRequestContextWrapper implements TilesRequestContext {
+
+    private TilesRequestContext context;
+
+
+    public TilesRequestContextWrapper(TilesRequestContext context) {
+        this.context = context;
+    }
+
+    public Map getHeader() {
+        return context.getHeader();
+    }
+
+    public Map getHeaderValues() {
+        return context.getHeaderValues();
+    }
+
+    public Map getRequestScope() {
+        return context.getRequestScope();
+    }
+
+    public Map getSessionScope() {
+        return context.getSessionScope();
+    }
+
+    public void dispatch(String path) throws IOException {
+        context.dispatch(path);
+    }
+
+    public void include(String path) throws IOException {
+        context.include(path);
+    }
+
+    public Map getParam() {
+        return context.getParam();
+    }
+
+    public Map getParamValues() {
+        return context.getParamValues();
+    }
+
+    public Locale getRequestLocale() {
+        return context.getRequestLocale();
+    }
+
+    public boolean isUserInRole(String role) {
+        return context.isUserInRole(role);
+    }
+
+
+    public Object getResponse() {
+        return context.getResponse();
+    }
+
+    public Object getRequest() {
+        return context.getRequest();
+    }
+}

Propchange: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/TilesRequestContextWrapper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/TilesRequestContextWrapper.java
------------------------------------------------------------------------------
    svn:keywords = Id Author Date Rev

Modified: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/jsp/JspTilesRequestContext.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/jsp/JspTilesRequestContext.java?view=diff&rev=473432&r1=473431&r2=473432
==============================================================================
--- struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/jsp/JspTilesRequestContext.java (original)
+++ struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/jsp/JspTilesRequestContext.java Fri Nov 10 11:14:35 2006
@@ -30,6 +30,7 @@
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.jsp.PageContext;
 import javax.servlet.jsp.JspException;
+import java.io.IOException;
 
 /**
  * Context implementation used for executing tiles within a
@@ -51,15 +52,16 @@
         this.pageContext = pageContext;
     }
 
-    public void dispatch(String path) throws TilesException {
+    public void dispatch(String path) throws IOException {
         include(path);
     }
 
-    public void include(String path) throws TilesException {
+    public void include(String path) throws IOException {
         try {
             JspUtil.doInclude(pageContext, path, false);
         } catch (JspException e) {
-            throw new TilesException(e);
+            LOG.error("JSPException while including path '"+path+"'. ", e);
+            throw new IOException("JSPException while including path '"+path+"'. "+e.getMessage());
         }
     }
 

Modified: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/portlet/PortletTilesRequestContext.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/portlet/PortletTilesRequestContext.java?view=diff&rev=473432&r1=473431&r2=473432
==============================================================================
--- struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/portlet/PortletTilesRequestContext.java (original)
+++ struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/portlet/PortletTilesRequestContext.java Fri Nov 10 11:14:35 2006
@@ -203,14 +203,18 @@
         return (sessionScope);
     }
 
-    public void dispatch(String path) throws IOException, Exception {
+    public void dispatch(String path) throws IOException {
         include(path);
     }
 
-    public void include(String path) throws IOException, Exception {
+    public void include(String path) throws IOException {
         if (isRenderRequest) {
-            context.getRequestDispatcher(path).include((RenderRequest) request,
-                (RenderResponse) response);
+            try {
+                context.getRequestDispatcher(path).include((RenderRequest) request,
+                    (RenderResponse) response);
+            } catch (PortletException e) {
+                throw new IOException("PortletException while including path '"+path+"'."+e.getMessage());
+            }
         }
     }
 

Modified: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/servlet/ServletTilesRequestContext.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/servlet/ServletTilesRequestContext.java?view=diff&rev=473432&r1=473431&r2=473432
==============================================================================
--- struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/servlet/ServletTilesRequestContext.java (original)
+++ struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/servlet/ServletTilesRequestContext.java Fri Nov 10 11:14:35 2006
@@ -154,22 +154,17 @@
 
     }
 
-    public void dispatch(String path) throws IOException, Exception {
-        RequestDispatcher rd = request.getRequestDispatcher(path);
-        try {
-            rd.include(request, response);
-        } catch (ServletException ex) {
-            LOG.error("Error including path '"+path+"'.", ex);
-            throw new Exception("Error including request.", ex);
-        }
+    public void dispatch(String path) throws IOException {
+        include(path);
     }
 
-    public void include(String path) throws IOException, Exception {
+    public void include(String path) throws IOException{
         RequestDispatcher rd = request.getRequestDispatcher(path);
         try {
             rd.include(request, response);
         } catch (ServletException ex) {
-            throw new Exception("Error including path.", ex);
+            LOG.error("Servlet Exception while including path", ex);
+            throw new IOException("Error including path '"+path+"'. " + ex.getMessage());
         }
     }
 
@@ -177,11 +172,11 @@
         return request.getLocale();
     }
 
-    public ServletRequest getRequest() {
+    public HttpServletRequest getRequest() {
         return request;
     }
 
-    public ServletResponse getResponse() {
+    public HttpServletResponse getResponse() {
         return response;
     }
 

Modified: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java?view=diff&rev=473432&r1=473431&r2=473432
==============================================================================
--- struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java (original)
+++ struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java Fri Nov 10 11:14:35 2006
@@ -21,7 +21,11 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.apache.tiles.*;
+import org.apache.tiles.ComponentAttribute;
+import org.apache.tiles.ComponentContext;
+import org.apache.tiles.TilesApplicationContext;
+import org.apache.tiles.TilesContainer;
+import org.apache.tiles.TilesException;
 import org.apache.tiles.context.BasicComponentContext;
 import org.apache.tiles.context.TilesContextFactory;
 import org.apache.tiles.context.TilesRequestContext;
@@ -93,7 +97,7 @@
         if (LOG.isInfoEnabled()) {
             LOG.info("Initializing Tiles2 container. . .");
         }
-        
+
         contextFactory.init(initParameters);
         definitionsFactory.init(initParameters);
 
@@ -105,13 +109,12 @@
             for (String resource : resources) {
                 URL resourceUrl = context.getResource(resource);
                 if (resourceUrl != null) {
-                    if(LOG.isDebugEnabled()) {
-                        LOG.debug("Adding resource '"+resourceUrl+"' to definitions factory.");
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Adding resource '" + resourceUrl + "' to definitions factory.");
                     }
                     definitionsFactory.addSource(resourceUrl);
-                }
-                else {
-                    LOG.warn("Unable to find configured definition '"+resource+"'");
+                } else {
+                    LOG.warn("Unable to find configured definition '" + resource + "'");
                 }
             }
         } catch (IOException e) {
@@ -331,7 +334,7 @@
         BasicComponentContext.setContext(subContext, request);
 
         try {
-            if(definition.getPreparer() != null) {
+            if (definition.getPreparer() != null) {
                 prepare(request, definition.getPreparer(), true);
             }
 
@@ -343,17 +346,71 @@
             }
             request.dispatch(dispatchPath);
 
+            // tiles exception so that it doesn't need to be rethrown.
         } catch (TilesException e) {
             throw e;
         } catch (Exception e) {
+            LOG.error("Error rendering tile", e);
             // TODO it would be nice to make the preparerInstance throw a more specific
-            // tiles exception so that it doesn't need to be rethrown.
             throw new TilesException(e.getMessage(), e);
         } finally {
             BasicComponentContext.setContext(originalContext, request);
         }
     }
 
+    public void render(PageContext pageContext, ComponentAttribute attr)
+        throws TilesException, IOException {
+        ComponentContext context = getComponentContext(pageContext);
+        TilesRequestContext request = getRequestContext(pageContext);
+
+        String type = calculateType(pageContext, attr);
+        if ("string".equalsIgnoreCase(type)) {
+            pageContext.getOut().print(attr.getValue());
+            return;
+
+        }
+
+        Map<String, ComponentAttribute> attrs = attr.getAttributes();
+        if (attrs != null) {
+            for (Map.Entry<String, ComponentAttribute> a : attrs.entrySet()) {
+                context.putAttribute(a.getKey(), a.getValue());
+            }
+        }
+
+        if (isDefinition(pageContext, attr)) {
+            render(request, attr.getValue().toString());
+        } else {
+            request.include(attr.getValue().toString());
+        }
+    }
+
+    private boolean isDefinition(PageContext pageContext, ComponentAttribute attr) {
+        return ComponentAttribute.DEFINITION.equals(attr.getType()) ||
+            isValidDefinition(pageContext, attr.getValue().toString());
+    }
+
+    private String calculateType(PageContext pageContext, ComponentAttribute attr) throws TilesException {
+        String type = attr.getType();
+        if (type == null) {
+            Object valueContent = attr.getValue();
+            if (valueContent instanceof String) {
+                String valueString = (String) valueContent;
+                if (isValidDefinition(pageContext, valueString)) {
+                    type = ComponentAttribute.DEFINITION;
+                } else if (valueString.startsWith("/")) {
+                    type = ComponentAttribute.TEMPLATE;
+                } else {
+                    type = ComponentAttribute.STRING;
+                }
+            }
+            if (type == null) {
+                throw new TilesException("Unrecognized type for attribute value "
+                    + attr.getValue());
+            }
+        }
+        return type;
+    }
+
     protected ComponentDefinition getDefinition(String definitionName, TilesRequestContext request) throws DefinitionsFactoryException {
         ComponentDefinition definition =
             definitionsFactory.getDefinition(definitionName, request);
@@ -361,10 +418,10 @@
     }
 
     private boolean isPermitted(TilesRequestContext request, String role) {
-        if(role == null) {
+        if (role == null) {
             return true;
         }
-        
+
         StringTokenizer st = new StringTokenizer(role, ",");
         while (st.hasMoreTokens()) {
             if (request.isUserInRole(st.nextToken())) {

Modified: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/taglib/AttributeTag.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/taglib/AttributeTag.java?view=diff&rev=473432&r1=473431&r2=473432
==============================================================================
--- struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/taglib/AttributeTag.java (original)
+++ struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/taglib/AttributeTag.java Fri Nov 10 11:14:35 2006
@@ -79,53 +79,6 @@
             throw new TilesException("Attribute '" + name + "' not found.");
         }
 
-        String type = calculateType(attr);
-        if ("string".equalsIgnoreCase(type)) {
-            pageContext.getOut().print(attr.getValue());
-
-        } else if (isDefinition(attr)) {
-            if (template != null) {
-                attr.setValue(template);
-            }
-
-            Map<String, ComponentAttribute> attrs = attr.getAttributes();
-            if (attrs != null) {
-                for (Map.Entry<String, ComponentAttribute> a : attrs.entrySet()) {
-                    context.putAttribute(a.getKey(), a.getValue());
-                }
-            }
-            container.render(pageContext, attr.getValue().toString());
-
-        } else {
-            JspUtil.doInclude(pageContext, attr.getValue().toString(), flush);
-        }
-    }
-
-    private boolean isDefinition(ComponentAttribute attr) {
-        return ComponentAttribute.DEFINITION.equals(attr.getType()) ||
-            container.isValidDefinition(pageContext,
-                attr.getValue().toString());
-    }
-
-    private String calculateType(ComponentAttribute attr) throws JspException {
-        String type = attr.getType();
-        if (type == null) {
-            Object valueContent = attr.getValue();
-            if (valueContent instanceof String) {
-                String valueString = (String) valueContent;
-                if (container.isValidDefinition(pageContext, valueString)) {
-                    type = ComponentAttribute.DEFINITION;
-                } else if (valueString.startsWith("/")) {
-                    type = ComponentAttribute.TEMPLATE;
-                } else {
-                    type = ComponentAttribute.STRING;
-                }
-            }
-            if (type == null) {
-                throw new JspException("Unrecognized type for attribute value "
-                    + attr.getValue());
-            }
-        }
-        return type;
+        container.render(pageContext, attr);
     }
 }

Modified: struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/definition/MockOnlyLocaleTilesContext.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/definition/MockOnlyLocaleTilesContext.java?view=diff&rev=473432&r1=473431&r2=473432
==============================================================================
--- struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/definition/MockOnlyLocaleTilesContext.java (original)
+++ struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/definition/MockOnlyLocaleTilesContext.java Fri Nov 10 11:14:35 2006
@@ -61,7 +61,7 @@
     // nothing or returning null, because they are not needed at all in tests
     // that use this class.
 
-    public void dispatch(String path) throws IOException, Exception {
+    public void dispatch(String path) throws IOException {
     }
 
 
@@ -105,10 +105,18 @@
         return null;
     }
 
-    public void include(String path) throws IOException, Exception {
+    public void include(String path) throws IOException {
     }
 
     public boolean isUserInRole(String role) {
         return false;
+    }
+
+    public Object getRequest() {
+        return null;  //To change body of implemented methods use File | Settings | File Templates.
+    }
+
+    public Object getResponse() {
+        return null;  //To change body of implemented methods use File | Settings | File Templates.
     }
 }