Author: apetrelli
Date: Mon May 19 11:51:45 2008
New Revision: 657917

URL: http://svn.apache.org/viewvc?rev=657917&view=rev
Log:
TILES-275
Added methods to store the current container.
Updated to EasyMock 2.3.

Added:
    
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/NoSuchContainerException.java
   (with props)
Modified:
    tiles/framework/trunk/tiles-api/pom.xml
    
tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/access/TilesAccess.java
    
tiles/framework/trunk/tiles-api/src/test/java/org/apache/tiles/access/TilesAccessTest.java
    tiles/framework/trunk/tiles-compat/pom.xml
    tiles/framework/trunk/tiles-core/pom.xml
    
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/servlet/context/ServletUtil.java
    tiles/framework/trunk/tiles-jsp/pom.xml
    
tiles/framework/trunk/tiles-jsp/src/main/java/org/apache/tiles/jsp/context/JspUtil.java

Modified: tiles/framework/trunk/tiles-api/pom.xml
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-api/pom.xml?rev=657917&r1=657916&r2=657917&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-api/pom.xml (original)
+++ tiles/framework/trunk/tiles-api/pom.xml Mon May 19 11:51:45 2008
@@ -120,7 +120,7 @@
           <dependency>
               <groupId>org.easymock</groupId>
               <artifactId>easymock</artifactId>
-              <version>2.2</version>
+              <version>2.3</version>
               <scope>test</scope>
           </dependency>
     </dependencies>

Modified: 
tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/access/TilesAccess.java
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/access/TilesAccess.java?rev=657917&r1=657916&r2=657917&view=diff
==============================================================================
--- 
tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/access/TilesAccess.java
 (original)
+++ 
tiles/framework/trunk/tiles-api/src/main/java/org/apache/tiles/access/TilesAccess.java
 Mon May 19 11:51:45 2008
@@ -62,7 +62,8 @@
         "org.apache.tiles.APPLICATION_CONTEXT";
 
     /**
-     * Finds and returns a Tiles container object, if it was previously 
initialized.
+     * Finds and returns the default Tiles container object, if it was
+     * previously initialized.
      *
      * @param context The (application) context object to use.
      * @return The container if it has been configured previously, otherwise
@@ -74,6 +75,23 @@
     }
 
     /**
+     * Finds and returns a Tiles container object, if it was previously 
initialized.
+     *
+     * @param context The (application) context object to use.
+     * @return The container if it has been configured previously, otherwise
+     * <code>null</code>.
+     * @param key The key under which the container is stored.
+     * @see #setContainer(Object, TilesContainer)
+     */
+    public static TilesContainer getContainer(Object context, String key) {
+        if (key == null) {
+            key = CONTAINER_ATTRIBUTE;
+        }
+
+        return (TilesContainer) getAttribute(context, key);
+    }
+
+    /**
      * Configures the container to be used in the application.
      *
      * @param context The (application) context object to use.
@@ -82,17 +100,33 @@
      * context.
      */
     public static void setContainer(Object context, TilesContainer container) {
+        setContainer(context, container, CONTAINER_ATTRIBUTE);
+    }
+
+    /**
+     * Configures the container to be used in the application.
+     *
+     * @param context The (application) context object to use.
+     * @param container The container object to set.
+     * @param key The key under which the container will be stored.
+     * @throws TilesAccessException If something goes wrong during 
manipulation of the
+     * context.
+     */
+    public static void setContainer(Object context, TilesContainer container, 
String key) {
+        if (key == null) {
+            key = CONTAINER_ATTRIBUTE;
+        }
 
         if (container == null) {
             if (LOG.isInfoEnabled()) {
                 LOG.info("Removing TilesContext for context: " + 
context.getClass().getName());
             }
-            removeAttribute(context, CONTAINER_ATTRIBUTE);
+            removeAttribute(context, key);
         }
         if (container != null && LOG.isInfoEnabled()) {
             LOG.info("Publishing TilesContext for context: " + 
context.getClass().getName());
         }
-        setAttribute(context, CONTAINER_ATTRIBUTE, container);
+        setAttribute(context, key, container);
     }
 
     /**

Modified: 
tiles/framework/trunk/tiles-api/src/test/java/org/apache/tiles/access/TilesAccessTest.java
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-api/src/test/java/org/apache/tiles/access/TilesAccessTest.java?rev=657917&r1=657916&r2=657917&view=diff
==============================================================================
--- 
tiles/framework/trunk/tiles-api/src/test/java/org/apache/tiles/access/TilesAccessTest.java
 (original)
+++ 
tiles/framework/trunk/tiles-api/src/test/java/org/apache/tiles/access/TilesAccessTest.java
 Mon May 19 11:51:45 2008
@@ -54,6 +54,17 @@
     }
 
     /**
+     * Tests the setting of the context.
+     */
+    public void testSetContextWithKey() {
+        TilesContainer container = EasyMock.createMock(TilesContainer.class);
+        context.setAttribute("myKey", container);
+        EasyMock.replay(context);
+        TilesAccess.setContainer(context, container, "myKey");
+        EasyMock.verify(context);
+    }
+
+    /**
      * Tests the getting of the context.
      */
     public void testGetContext() {
@@ -64,4 +75,15 @@
         EasyMock.verify(context);
     }
 
+    /**
+     * Tests the getting of the context.
+     */
+    public void testGetContextWithKey() {
+        TilesContainer container = EasyMock.createMock(TilesContainer.class);
+        EasyMock.expect(context.getAttribute("myKey")).andReturn(container);
+        EasyMock.replay(context);
+        assertEquals(container, TilesAccess.getContainer(context, "myKey"));
+        EasyMock.verify(context);
+    }
+
 }

Modified: tiles/framework/trunk/tiles-compat/pom.xml
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-compat/pom.xml?rev=657917&r1=657916&r2=657917&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-compat/pom.xml (original)
+++ tiles/framework/trunk/tiles-compat/pom.xml Mon May 19 11:51:45 2008
@@ -142,7 +142,7 @@
     <dependency>
       <groupId>org.easymock</groupId>
       <artifactId>easymock</artifactId>
-      <version>2.2</version>
+      <version>2.3</version>
       <scope>test</scope>
     </dependency>
 

Modified: tiles/framework/trunk/tiles-core/pom.xml
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/pom.xml?rev=657917&r1=657916&r2=657917&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-core/pom.xml (original)
+++ tiles/framework/trunk/tiles-core/pom.xml Mon May 19 11:51:45 2008
@@ -196,7 +196,7 @@
     <dependency>
       <groupId>org.easymock</groupId>
       <artifactId>easymock</artifactId>
-      <version>2.2</version>
+      <version>2.3</version>
       <scope>test</scope>
     </dependency>
 

Added: 
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/NoSuchContainerException.java
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/NoSuchContainerException.java?rev=657917&view=auto
==============================================================================
--- 
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/NoSuchContainerException.java
 (added)
+++ 
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/NoSuchContainerException.java
 Mon May 19 11:51:45 2008
@@ -0,0 +1,72 @@
+/*
+ * $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.impl;
+
+import org.apache.tiles.TilesException;
+
+/**
+ * Indicates that a keyed container has not been found.
+ *
+ * @version $Rev$ $Date$
+ * @since 2.1.0
+ */
+public class NoSuchContainerException extends TilesException {
+
+    /**
+     * Constructor.
+     *
+     * @since 2.1.0
+     */
+    public NoSuchContainerException() {
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param message The detail message.
+     * @since 2.1.0
+     */
+    public NoSuchContainerException(String message) {
+        super(message);
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param e The exception to be wrapped.
+     * @since 2.1.0
+     */
+    public NoSuchContainerException(Exception e) {
+        super(e);
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param message The detail message.
+     * @param e The exception to be wrapped.
+     * @since 2.1.0
+     */
+    public NoSuchContainerException(String message, Exception e) {
+        super(message, e);
+    }
+
+}

Propchange: 
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/NoSuchContainerException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/impl/NoSuchContainerException.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: 
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/servlet/context/ServletUtil.java
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/servlet/context/ServletUtil.java?rev=657917&r1=657916&r2=657917&view=diff
==============================================================================
--- 
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/servlet/context/ServletUtil.java
 (original)
+++ 
tiles/framework/trunk/tiles-core/src/main/java/org/apache/tiles/servlet/context/ServletUtil.java
 Mon May 19 11:51:45 2008
@@ -21,8 +21,14 @@
 
 package org.apache.tiles.servlet.context;
 
+import javax.servlet.ServletContext;
+import javax.servlet.ServletRequest;
 import javax.servlet.http.HttpServletRequest;
 
+import org.apache.tiles.TilesContainer;
+import org.apache.tiles.access.TilesAccess;
+import org.apache.tiles.impl.NoSuchContainerException;
+
 
 /**
  * Utilities for Tiles servlet support.
@@ -40,6 +46,12 @@
         
"org.apache.tiles.servlet.context.ServletTilesRequestContext.FORCE_INCLUDE";
 
     /**
+     * Name of the attribute used to store the current used container.
+     */
+    public static final String CURRENT_CONTAINER_ATTRIBUTE_NAME =
+        
"org.apache.tiles.servlet.context.ServletTilesRequestContext.CURRENT_CONTAINER_KEY";
+
+    /**
      * Private constructor to avoid instantiation.
      */
     private ServletUtil() {
@@ -73,4 +85,60 @@
                 ServletUtil.FORCE_INCLUDE_ATTRIBUTE_NAME,
                 retValue);
     }
+
+    /**
+     * Sets the current container to use in web pages.
+     *
+     * @param request The request to use.
+     * @param context The servlet context to use.
+     * @param key The key under which the container is stored.
+     * @since 2.1.0
+     */
+    public static void setCurrentContainer(ServletRequest request,
+            ServletContext context, String key) {
+        TilesContainer container = TilesAccess.getContainer(context, key);
+        if (container != null) {
+            request.setAttribute(CURRENT_CONTAINER_ATTRIBUTE_NAME, container);
+        } else {
+            throw new NoSuchContainerException("The container with the key '"
+                    + key + "' cannot be found");
+        }
+    }
+
+    /**
+     * Sets the current container to use in web pages.
+     *
+     * @param request The request to use.
+     * @param context The servlet context to use.
+     * @param container The container to use as the current container.
+     * @since 2.1.0
+     */
+    public static void setCurrentContainer(ServletRequest request,
+            ServletContext context, TilesContainer container) {
+        if (container != null) {
+            request.setAttribute(CURRENT_CONTAINER_ATTRIBUTE_NAME, container);
+        } else {
+            throw new NoSuchContainerException("The container cannot be null");
+        }
+    }
+
+    /**
+     * Returns the current container that has been set, or the default one.
+     *
+     * @param request The request to use.
+     * @param context The servlet context to use.
+     * @return The current Tiles container to use in web pages.
+     * @since 2.1.0
+     */
+    public static TilesContainer getCurrentContainer(ServletRequest request,
+            ServletContext context) {
+        TilesContainer container = (TilesContainer) request
+                .getAttribute(CURRENT_CONTAINER_ATTRIBUTE_NAME);
+        if (container == null) {
+            container = TilesAccess.getContainer(context);
+            request.setAttribute(CURRENT_CONTAINER_ATTRIBUTE_NAME, container);
+        }
+
+        return container;
+    }
 }

Modified: tiles/framework/trunk/tiles-jsp/pom.xml
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-jsp/pom.xml?rev=657917&r1=657916&r2=657917&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-jsp/pom.xml (original)
+++ tiles/framework/trunk/tiles-jsp/pom.xml Mon May 19 11:51:45 2008
@@ -168,7 +168,14 @@
     <dependency>
       <groupId>org.easymock</groupId>
       <artifactId>easymock</artifactId>
-      <version>2.2</version>
+      <version>2.3</version>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.easymock</groupId>
+      <artifactId>easymockclassextension</artifactId>
+      <version>2.3</version>
       <scope>test</scope>
     </dependency>
 

Modified: 
tiles/framework/trunk/tiles-jsp/src/main/java/org/apache/tiles/jsp/context/JspUtil.java
URL: 
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-jsp/src/main/java/org/apache/tiles/jsp/context/JspUtil.java?rev=657917&r1=657916&r2=657917&view=diff
==============================================================================
--- 
tiles/framework/trunk/tiles-jsp/src/main/java/org/apache/tiles/jsp/context/JspUtil.java
 (original)
+++ 
tiles/framework/trunk/tiles-jsp/src/main/java/org/apache/tiles/jsp/context/JspUtil.java
 Mon May 19 11:51:45 2008
@@ -18,8 +18,12 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+
 package org.apache.tiles.jsp.context;
 
+import org.apache.tiles.TilesContainer;
+import org.apache.tiles.access.TilesAccess;
+import org.apache.tiles.impl.NoSuchContainerException;
 import org.apache.tiles.servlet.context.ServletUtil;
 
 import javax.servlet.jsp.PageContext;
@@ -65,4 +69,60 @@
                 ServletUtil.FORCE_INCLUDE_ATTRIBUTE_NAME,
                 retValue, PageContext.REQUEST_SCOPE);
     }
+
+    /**
+     * Sets the current container to use in web pages.
+     *
+     * @param context The page context to use.
+     * @param key The key under which the container is stored.
+     * @since 2.1.0
+     */
+    public static void setCurrentContainer(PageContext context, String key) {
+        TilesContainer container = TilesAccess.getContainer(context
+                .getServletContext(), key);
+        if (container != null) {
+            context.setAttribute(ServletUtil.CURRENT_CONTAINER_ATTRIBUTE_NAME,
+                    container, PageContext.REQUEST_SCOPE);
+        } else {
+            throw new NoSuchContainerException("The container with the key '"
+                    + key + "' cannot be found");
+        }
+    }
+
+    /**
+     * Sets the current container to use in web pages.
+     *
+     * @param context The page context to use.
+     * @param container The container to use as the current container.
+     * @since 2.1.0
+     */
+    public static void setCurrentContainer(PageContext context,
+            TilesContainer container) {
+        if (container != null) {
+            context.setAttribute(ServletUtil.CURRENT_CONTAINER_ATTRIBUTE_NAME,
+                    container, PageContext.REQUEST_SCOPE);
+        } else {
+            throw new NoSuchContainerException("The container cannot be null");
+        }
+    }
+
+    /**
+     * Returns the current container that has been set, or the default one.
+     *
+     * @param context The page context to use.
+     * @return The current Tiles container to use in web pages.
+     * @since 2.1.0
+     */
+    public static TilesContainer getCurrentContainer(PageContext context) {
+        TilesContainer container = (TilesContainer) context.getAttribute(
+                ServletUtil.CURRENT_CONTAINER_ATTRIBUTE_NAME,
+                PageContext.REQUEST_SCOPE);
+        if (container == null) {
+            container = TilesAccess.getContainer(context.getServletContext());
+            context.setAttribute(ServletUtil.CURRENT_CONTAINER_ATTRIBUTE_NAME,
+                    container, PageContext.REQUEST_SCOPE);
+        }
+
+        return container;
+    }
 }


Reply via email to