Commits

Anonymous committed aad2e85

Issue number: CACHE-222 new jsp tag addGroups added
Obtained from: Lars Torunski

Comments (0)

Files changed (4)

src/etc/oscache.tld

         </attribute>
     </tag>
 
+    <tag>
+        <name>addgroups</name>
+        <tag-class>com.opensymphony.oscache.web.tag.GroupsTag</tag-class>
+        <description>A tag to add a comma-delimited list of groups to an ancestor cache tag</description>
+        <attribute>
+            <name>groups</name>
+            <required>true</required>
+            <rtexprvalue>true</rtexprvalue>
+        </attribute>
+    </tag>
+    
 </taglib>

src/java/com/opensymphony/oscache/web/tag/CacheTag.java

      * @param groups A comma-delimited list of groups that the cache entry belongs to.
      */
     public void setGroups(String groups) {
+    	// FIXME: ArrayList doesn't avoid duplicates
         this.groups = StringUtil.split(groups, ',');
     }
 
      */
     void addGroup(String group) {
         if (groups == null) {
+        	// FIXME: ArrayList doesn't avoid duplicates
             groups = new ArrayList();
         }
 
     }
 
     /**
+     * Adds comma-delimited list of groups that the cache entry belongs to.
+     *
+     * @param groups A comma-delimited list of groups that the cache entry belongs to also.
+     */
+    void addGroups(String groupsString) {
+        if (groups == null) {
+        	// FIXME: ArrayList doesn't avoid duplicates
+            groups = new ArrayList();
+        }
+
+        groups.addAll(StringUtil.split(groupsString, ','));
+    }
+
+    /**
     * Set the key for this cache entry.
     *
     * @param key The key for this cache entry.

src/java/com/opensymphony/oscache/web/tag/GroupsTag.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.oscache.web.tag;
+
+import javax.servlet.jsp.JspTagException;
+import javax.servlet.jsp.tagext.TagSupport;
+
+/**
+ * GroupsTag is a tag that add a comma-delimited list of groups to an ancestor CacheTag's groups.<p>
+ *
+ * @author <a href="mailto:ltorunski@t-online.de">Lars Torunski</a>
+ */
+public class GroupsTag extends TagSupport {
+    private Object groups = null;
+
+    public int doStartTag() throws JspTagException {
+        CacheTag ancestorCacheTag = (CacheTag) TagSupport.findAncestorWithClass(this, CacheTag.class);
+
+        if (ancestorCacheTag == null) {
+            throw new JspTagException("GroupTag cannot be used from outside a CacheTag");
+        }
+
+        ancestorCacheTag.addGroups(String.valueOf(groups));
+        
+        return EVAL_BODY_INCLUDE;
+    }
+
+    public void setGroups(Object groups) {
+        this.groups = groups;
+    }
+}

src/webapp/groupTest.jsp

 <hr>
 <cache:cache key='test1' groups='group1,group2' duration='5s'>
     <b>Cache Time</b>: <%= (new Date()).getTime() %><br>
-    This is some cache content that is in 'group1' and 'group2'. Normally it would refresh if it
+    This is some cache content test1 that is in 'group1' and 'group2'. Normally it would refresh if it
     was more than 5 seconds old, however the &lt;cache:flush group='group2' scope='application'&gt;
     tag causes this entry to be flushed on every page refresh.<br>
 </cache:cache>
 <hr>
 <cache:cache key='test2' groups='group1' duration='5s'>
     <b>Cache Time</b>: <%= (new Date()).getTime() %><br>
-    This is some cache content that is in 'group1' (refreshes if more than 5 seconds old)<br>
+    This is some cache content test2 that is in 'group1' (refreshes if more than 5 seconds old)<br>
 </cache:cache>
 <hr>
 <cache:cache key='test3' duration='20s'>
     <b>Cache Time</b>: <%= (new Date()).getTime() %><br>
-    This is some cache content that is in 'group1' and 'group2'. The groups are added using the &lt;cache:addgroup /&gt; tag.<br>
+    This is some cache content test3 that is in 'group1' and 'group2'. The groups are added using the &lt;cache:addgroup /&gt; tag.<br>
     <cache:addgroup group='group1'/>
     <cache:addgroup group='group2'/>
 </cache:cache>
 <hr>
+<cache:cache key='test4' duration='20s'>
+    <b>Cache Time</b>: <%= (new Date()).getTime() %><br>
+    This is some cache content test4 that is in 'group1' and 'group2'. The groups are added using the &lt;cache:addgroups /&gt; tag.<br>
+    <cache:addgroups groups='group1,group2'/>
+</cache:cache>
+<hr>
 </body>
 </html>