epugh       2004/05/26 08:14:40

  Modified:    cache/src/java/org/apache/fulcrum/cache
                        DefaultGlobalCacheService.java
                        GlobalCacheService.java
               cache    project.xml
               cache/src/test/org/apache/fulcrum/cache CacheTest.java
               cache/xdocs changes.xml
  Log:
        Added patch from Peter Courcoux adding new methods,

                getKeys() and getCachedObjects() together with 

                associated unit tests.
  
  Revision  Changes    Path
  1.3       +56 -1     
jakarta-turbine-fulcrum/cache/src/java/org/apache/fulcrum/cache/DefaultGlobalCacheService.java
  
  Index: DefaultGlobalCacheService.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-fulcrum/cache/src/java/org/apache/fulcrum/cache/DefaultGlobalCacheService.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DefaultGlobalCacheService.java    27 Oct 2003 16:47:05 -0000      1.2
  +++ DefaultGlobalCacheService.java    26 May 2004 15:14:40 -0000      1.3
  @@ -83,6 +83,7 @@
    * @author <a href="mailto:[EMAIL PROTECTED]">John Thorhauer</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Henning P. Schmiedehausen</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Eric Pugh</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]">Peter Courcoux</a>
    * * @version $Id$
    */
   public class DefaultGlobalCacheService
  @@ -207,6 +208,60 @@
       {
           cache.remove(id);
       }
  +    
  +    /**
  +     * Returns a copy of keys to objects in the cache as a list.
  +     * 
  +     * Note that keys to expired objects are not returned.
  +     * 
  +     * @return A List of <code>String</code>'s representing the keys to objects
  +     * in the cache.
  +     */
  +    public List getKeys() {
  +        ArrayList keys = new ArrayList(cache.size());
  +        synchronized (this) {
  +            for (Iterator itr = cache.keySet().iterator(); itr.hasNext();) 
  +            {
  +                String key = (String) itr.next();
  +                try {
  +                    CachedObject obj = getObject(key);
  +                } catch (ObjectExpiredException oee) {
  +                    // this is OK we just do not want this key 
  +                    continue;
  +                }
  +                keys.add(new String(key));
  +            }
  +        }
  +        return (List)keys;
  +    }
  +    
  +    /**
  +     * Returns a copy of the non-expired CachedObjects 
  +     * in the cache as a list.
  +     * 
  +     * @return A List of <code>CachedObject</code> objects 
  +     * held in the cache
  +     */
  +    public List getCachedObjects() {
  +        ArrayList objects = new ArrayList(cache.size());
  +        synchronized (this) {
  +            for (Iterator itr = cache.keySet().iterator(); itr.hasNext();) 
  +            {
  +                String key = (String) itr.next();
  +                CachedObject obj = null;
  +                try {
  +                    obj = getObject(key);
  +                } catch (ObjectExpiredException oee) {
  +                    // this is OK we just do not want this object 
  +                    continue;
  +                }
  +                objects.add(obj);
  +            }
  +        }
  +        return (List)objects;
  +    }
  +    
  +    
       /**
        * Circle through the cache and remove stale objects.  Frequency
        * is determined by the cacheCheckFrequency property.
  
  
  
  1.2       +25 -1     
jakarta-turbine-fulcrum/cache/src/java/org/apache/fulcrum/cache/GlobalCacheService.java
  
  Index: GlobalCacheService.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-fulcrum/cache/src/java/org/apache/fulcrum/cache/GlobalCacheService.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- GlobalCacheService.java   20 Aug 2003 00:45:45 -0000      1.1
  +++ GlobalCacheService.java   26 May 2004 15:14:40 -0000      1.2
  @@ -55,12 +55,15 @@
    */
   
   import java.io.IOException;
  +import java.util.List;
  +
   import org.apache.avalon.framework.component.Component;
   
   /**
    * GlobalCacheService interface.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Dave Bryson</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]">Peter Courcoux</a>
    * @version $Id$
    */
   public interface GlobalCacheService
  @@ -95,6 +98,27 @@
        * @param id The String id for the object.
        */
       void removeObject(String id);
  +    
  +    /**
  +     * Returns a copy of keys to objects in the cache as a list.
  +     * 
  +     * Note that keys to expired objects are not returned.
  +     * 
  +     * @return A List of <code>String</code>'s representing the keys to objects
  +     * in the cache.
  +     */
  +    public List getKeys();
  +
  +        
  +    /**
  +     * Returns a copy of the non-expired CachedObjects 
  +     * in the cache as a list.
  +     * 
  +     * @return A List of <code>CachedObject</code> objects 
  +     * held in the cache
  +     */
  +    public List getCachedObjects();
  +
   
       /**
        * Returns the current size of the cache.
  
  
  
  1.4       +1 -1      jakarta-turbine-fulcrum/cache/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-fulcrum/cache/project.xml,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- project.xml       7 Dec 2003 18:54:29 -0000       1.3
  +++ project.xml       26 May 2004 15:14:40 -0000      1.4
  @@ -3,7 +3,7 @@
     <extend>${basedir}/../project.xml</extend>
     <id>fulcrum-cache</id>
     <name>Fulcrum Cache Component</name>
  -  <currentVersion>1.0-alpha-3</currentVersion>
  +  <currentVersion>1.0-alpha-4</currentVersion>
   
     <dependencies>
           
  
  
  
  1.3       +84 -1     
jakarta-turbine-fulcrum/cache/src/test/org/apache/fulcrum/cache/CacheTest.java
  
  Index: CacheTest.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-fulcrum/cache/src/test/org/apache/fulcrum/cache/CacheTest.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- CacheTest.java    19 Nov 2003 14:03:53 -0000      1.2
  +++ CacheTest.java    26 May 2004 15:14:40 -0000      1.3
  @@ -53,6 +53,12 @@
    */
   package org.apache.fulcrum.cache;
   // Cactus and Junit imports
  +
  +import java.util.ConcurrentModificationException;
  +import java.util.Date;
  +import java.util.Iterator;
  +import java.util.List;
  +
   import junit.awtui.TestRunner;
   import junit.framework.Test;
   import junit.framework.TestSuite;
  @@ -63,10 +69,12 @@
    *
    * @author <a href="[EMAIL PROTECTED]">Paul Spencer</a>
    * @author <a href="[EMAIL PROTECTED]">Eric Pugh</a> 
  + * @author <a href="mailto:[EMAIL PROTECTED]">Peter Courcoux</a>
    * @version $Id$
    */
   public class CacheTest extends BaseUnitTest
   {
  +    
       private GlobalCacheService globalCache = null;
       private static final String cacheKey = new String("CacheKey");
       private static final String cacheKey_2 = new String("CacheKey_2");
  @@ -496,6 +504,81 @@
               throw e;
           }
       }
  +    /**
  +     * Test that we can get a list of the keys in the cache
  +     * 
  +     * @return
  +     */
  +    public void testCacheGetKeyList() {
  +        globalCache.flushCache();
  +        globalCache.addObject("date1", new CachedObject(new Date()));
  +        globalCache.addObject("date2", new CachedObject(new Date()));
  +        globalCache.addObject("date3", new CachedObject(new Date()));
  +        assertTrue("Did not get key list back.", (globalCache.getKeys() != null));
  +        List keys = globalCache.getKeys();
  +        for (Iterator itr = keys.iterator(); itr.hasNext();) {
  +            Object key = itr.next();
  +            assertTrue("Key was not an instance of String.", (key instanceof 
String));
  +        }
  +        
  +    }
  +    
  +    /**
  +     * Test that we can get a list of the keys in the cache
  +     * 
  +     * @return
  +     */
  +    public void testCacheGetCachedObjects() {
  +        globalCache.flushCache();
  +        globalCache.addObject("date1", new CachedObject(new Date()));
  +        globalCache.addObject("date2", new CachedObject(new Date()));
  +        globalCache.addObject("date3", new CachedObject(new Date()));
  +        assertTrue("Did not get object list back.", (globalCache.getCachedObjects() 
!= null));
  +        List objects = globalCache.getCachedObjects();
  +        for (Iterator itr = objects.iterator(); itr.hasNext();) {
  +            Object obj = itr.next();
  +            assertNotNull("Object was null.", obj);
  +            assertTrue("Object was not an instance of CachedObject", (obj 
instanceof CachedObject));
  +        }
  +        
  +    }
  +    
  +    /**
  +     * Test that the retrieved list is safe from 
  +     * ConcurrentModificationException's being thrown if the cache
  +     * is updated while we are iterating over the List.
  +     * 
  +     * @return
  +     */
  +    public void testCacheModification() {
  +        globalCache.flushCache();
  +        globalCache.addObject("date1", new CachedObject(new Date()));
  +        globalCache.addObject("date2", new CachedObject(new Date()));
  +        globalCache.addObject("date3", new CachedObject(new Date()));
  +        assertTrue("Did not get key list back.", (globalCache.getKeys() != null));
  +        List keys = globalCache.getKeys();
  +        try {
  +             for (Iterator itr = keys.iterator(); itr.hasNext();) {
  +                 Object key = itr.next();
  +                 globalCache.addObject("date4", new CachedObject(new Date()));      
     
  +             }
  +        } catch (ConcurrentModificationException cme)
  +        {
  +            fail("Caught ConcurrentModificationException adding to cache.");
  +        }
  +        List objects = globalCache.getCachedObjects();
  +        try {
  +             for (Iterator itr = objects.iterator(); itr.hasNext();) {
  +                 Object obj = itr.next();
  +                 globalCache.addObject("date4", new CachedObject(new Date()));      
     
  +             }
  +        } catch (ConcurrentModificationException cme)
  +        {
  +            fail("Caught ConcurrentModificationException adding to cache.");
  +        }
  +    }
  +    
  +    
       /**
        * Down cast the interface to the concreate object in order to grab the 
        * cache check frequency.
  
  
  
  1.3       +8 -1      jakarta-turbine-fulcrum/cache/xdocs/changes.xml
  
  Index: changes.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-fulcrum/cache/xdocs/changes.xml,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- changes.xml       27 Oct 2003 16:47:05 -0000      1.2
  +++ changes.xml       26 May 2004 15:14:40 -0000      1.3
  @@ -6,7 +6,14 @@
     </properties>
   
     <body>
  -    <release version="1.0-alpha-3" date="in CVS">
  +    <release version="1.0-alpha-4" date="">
  +      <action dev="epugh" type="add">
  +             Added patch from Peter Courcoux adding new methods,
  +             getKeys() and getCachedObjects() together with 
  +             associated unit tests.
  +      </action>  
  +    </release>
  +    <release version="1.0-alpha-3" date="">
        <action dev="epugh" type="add">
           Added CacheServiceFacade class.
         </action>
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to