You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@turbine.apache.org by tv...@apache.org on 2016/07/26 10:23:31 UTC

svn commit: r1754101 - in /turbine/fulcrum/trunk/cache/src: java/org/apache/fulcrum/cache/ java/org/apache/fulcrum/cache/impl/ test/org/apache/fulcrum/cache/

Author: tv
Date: Tue Jul 26 10:23:30 2016
New Revision: 1754101

URL: http://svn.apache.org/viewvc?rev=1754101&view=rev
Log:
Generify

Modified:
    turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/CachedObject.java
    turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/GlobalCacheService.java
    turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/RefreshableCachedObject.java
    turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultGlobalCacheService.java
    turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/EHCacheService.java
    turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/CacheTest.java

Modified: turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/CachedObject.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/CachedObject.java?rev=1754101&r1=1754100&r2=1754101&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/CachedObject.java (original)
+++ turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/CachedObject.java Tue Jul 26 10:23:30 2016
@@ -23,14 +23,14 @@ import java.io.Serializable;
 
 /**
  * Wrapper for an object you want to store in a cache for a period of time.
- * 
+ *
  * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
  * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
  * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
  * @version $Id$
  */
 
-public class CachedObject implements Serializable
+public class CachedObject<T> implements Serializable
 {
     /*
      * TODO: The old Turbine version you could set the default age from Turbine.
@@ -50,7 +50,7 @@ public class CachedObject implements Ser
     public static final int FOREVER = -1;
 
     /** The object to be cached. */
-    private Object contents = null;
+    private T contents = null;
 
     /** Default age (30 minutes). */
     private long defaultage = 1800000;
@@ -66,27 +66,25 @@ public class CachedObject implements Ser
 
     /**
      * Constructor; sets the object to expire in the default time (30 minutes).
-     * 
+     *
      * @param o
      *            The object you want to cache.
      */
-    public CachedObject(Object o)
+    public CachedObject(T o)
     {
-        this.contents = o;
-        this.expires = this.defaultage;
-        this.created = System.currentTimeMillis();
+        this(o, DEFAULT);
     }
 
     /**
      * Constructor.
-     * 
+     *
      * @param o
      *            The object to cache.
      * @param expires
      *            How long before the object expires, in ms, e.g. 1000 = 1
      *            second.
      */
-    public CachedObject(Object o, long expires)
+    public CachedObject(T o, long expires)
     {
         if (expires == DEFAULT)
         {
@@ -100,17 +98,17 @@ public class CachedObject implements Ser
 
     /**
      * Returns the cached object.
-     * 
+     *
      * @return The cached object.
      */
-    public Object getContents()
+    public T getContents()
     {
         return this.contents;
     }
 
     /**
      * Returns the creation time for the object.
-     * 
+     *
      * @return When the object was created.
      */
     public long getCreated()
@@ -120,7 +118,7 @@ public class CachedObject implements Ser
 
     /**
      * Returns the expiration time for the object.
-     * 
+     *
      * @return When the object expires.
      */
     public long getExpires()
@@ -130,7 +128,7 @@ public class CachedObject implements Ser
 
     /**
      * Set the expiration interval for the object.
-     * 
+     *
      * @param expires
      *            Expiration interval in millis ( 1 second = 1000 millis)
      */
@@ -156,7 +154,7 @@ public class CachedObject implements Ser
 
     /**
      * Set the stale status for the object.
-     * 
+     *
      * @param stale
      *            Whether the object is stale or not.
      */
@@ -167,7 +165,7 @@ public class CachedObject implements Ser
 
     /**
      * Get the stale status for the object.
-     * 
+     *
      * @return Whether the object is stale or not.
      */
     public synchronized boolean getStale()
@@ -177,7 +175,7 @@ public class CachedObject implements Ser
 
     /**
      * Is the object stale?
-     * 
+     *
      * @return True if the object is stale.
      */
     public synchronized boolean isStale()

Modified: turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/GlobalCacheService.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/GlobalCacheService.java?rev=1754101&r1=1754100&r2=1754101&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/GlobalCacheService.java (original)
+++ turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/GlobalCacheService.java Tue Jul 26 10:23:30 2016
@@ -22,44 +22,42 @@ package org.apache.fulcrum.cache;
 import java.io.IOException;
 import java.util.List;
 
-import org.apache.avalon.framework.component.Component;
-
 /**
  * GlobalCacheService interface.
- * 
+ *
  * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
  * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
  * @version $Id$
  */
-public interface GlobalCacheService extends Component
+public interface GlobalCacheService
 {
     /** Avalon role - used to id the component within the manager */
     String ROLE = GlobalCacheService.class.getName();
 
     /**
      * Gets a cached object given its id (a String).
-     * 
+     *
      * @param id
      *            The String id for the object.
      * @return A CachedObject.
      * @exception ObjectExpiredException,
      *                if the object has expired in the cache.
      */
-    CachedObject getObject(String id) throws ObjectExpiredException;
+    <T> CachedObject<T> getObject(String id) throws ObjectExpiredException;
 
     /**
      * Adds an object to the cache.
-     * 
+     *
      * @param id
      *            The String id for the object.
      * @param o
      *            The object to add to the cache.
      */
-    void addObject(String id, CachedObject o);
+    <T> void addObject(String id, CachedObject<T> o);
 
     /**
      * Removes an object from the cache.
-     * 
+     *
      * @param id
      *            The String id for the object.
      */
@@ -67,31 +65,31 @@ public interface GlobalCacheService exte
 
     /**
      * 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();
+    List<String> 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();
+    List<CachedObject<?>> getCachedObjects();
 
     /**
      * Returns the current size of the cache.
-     * 
+     *
      * @return int representing current cache size in number of bytes
      */
     int getCacheSize() throws IOException;
 
     /**
      * Returns the number of objects in the cache.
-     * 
+     *
      * @return int The current number of objects in the cache.
      */
     int getNumberOfObjects();

Modified: turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/RefreshableCachedObject.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/RefreshableCachedObject.java?rev=1754101&r1=1754100&r2=1754101&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/RefreshableCachedObject.java (original)
+++ turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/RefreshableCachedObject.java Tue Jul 26 10:23:30 2016
@@ -27,15 +27,15 @@ package org.apache.fulcrum.cache;
  * thread. You can also set a TTL (Time To Live) for the object. This way, if
  * the object hasn't been touched for the TTL period, then it will be removed
  * from the cache.
- * 
+ *
  * This extends CachedObject and provides a method for refreshing the cached
  * object, and resetting its expire time.
- * 
+ *
  * @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
  * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
  * @version $Id$
  */
-public class RefreshableCachedObject extends CachedObject
+public class RefreshableCachedObject<T extends Refreshable> extends CachedObject<T>
 {
 
     /**
@@ -56,11 +56,11 @@ public class RefreshableCachedObject ext
 
     /**
      * Constructor; sets the object to expire in the default time (30 minutes).
-     * 
+     *
      * @param o
      *            The object you want to cache.
      */
-    public RefreshableCachedObject(Refreshable o)
+    public RefreshableCachedObject(T o)
     {
         super(o);
         this.lastAccess = System.currentTimeMillis();
@@ -68,14 +68,14 @@ public class RefreshableCachedObject ext
 
     /**
      * Constructor.
-     * 
+     *
      * @param o
      *            The object to cache.
      * @param expires
      *            How long before the object expires, in ms, e.g. 1000 = 1
      *            second.
      */
-    public RefreshableCachedObject(Refreshable o, long expires)
+    public RefreshableCachedObject(T o, long expires)
     {
         super(o, expires);
         this.lastAccess = System.currentTimeMillis();
@@ -83,7 +83,7 @@ public class RefreshableCachedObject ext
 
     /**
      * Sets the timeToLive value
-     * 
+     *
      * @param timeToLive
      *            the new Value in milliseconds
      */
@@ -94,7 +94,7 @@ public class RefreshableCachedObject ext
 
     /**
      * Gets the timeToLive value.
-     * 
+     *
      * @return The current timeToLive value (in milliseconds)
      */
     public synchronized long getTTL()
@@ -136,7 +136,7 @@ public class RefreshableCachedObject ext
      */
     public void refresh()
     {
-        Refreshable r = (Refreshable) getContents();
+        Refreshable r = getContents();
         synchronized (this)
         {
             this.created = System.currentTimeMillis();

Modified: turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultGlobalCacheService.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultGlobalCacheService.java?rev=1754101&r1=1754100&r2=1754101&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultGlobalCacheService.java (original)
+++ turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultGlobalCacheService.java Tue Jul 26 10:23:30 2016
@@ -23,10 +23,8 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.ObjectOutputStream;
 import java.util.ArrayList;
-import java.util.Enumeration;
-import java.util.Hashtable;
-import java.util.Iterator;
 import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.avalon.framework.activity.Disposable;
 import org.apache.avalon.framework.activity.Initializable;
@@ -48,7 +46,7 @@ import org.apache.fulcrum.cache.Refresha
  * application. Since information about States doesn't change very often, you
  * could store this information in the Global Cache and decrease the overhead of
  * hitting the database everytime you need State information.
- * 
+ *
  * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
  * @author <a href="mailto:jon@clearink.com">Jon S. Stevens</a>
  * @author <a href="mailto:john@zenplex.com">John Thorhauer</a>
@@ -83,7 +81,7 @@ public class DefaultGlobalCacheService e
     public static final long DEFAULT_CACHE_CHECK_FREQUENCY = 5000; // 5 seconds
 
     /** The cache. * */
-    protected Hashtable cache = null;
+    protected ConcurrentHashMap<String, CachedObject<?>> cache = null;
 
     /**
      * cacheCheckFrequency (default - 5 seconds)
@@ -103,7 +101,7 @@ public class DefaultGlobalCacheService e
 
     /**
      * Get the Cache Check Frequency in milliseconds
-     * 
+     *
      * @return the time between two cache check runs in milliseconds
      */
     public long getCacheCheckFrequency()
@@ -115,7 +113,7 @@ public class DefaultGlobalCacheService e
      * Returns an item from the cache. /** Returns an item from the cache.
      * RefreshableCachedObject will be refreshed if it is expired and not
      * untouched.
-     * 
+     *
      * @param id
      *            The key of the stored object.
      * @return The object from the cache.
@@ -123,10 +121,11 @@ public class DefaultGlobalCacheService e
      *                when either the object is not in the cache or it has
      *                expired.
      */
-    public CachedObject getObject(String id) throws ObjectExpiredException
+    @Override
+    public <T> CachedObject<T> getObject(String id) throws ObjectExpiredException
     {
-        CachedObject obj = null;
-        obj = (CachedObject) this.cache.get(id);
+        @SuppressWarnings("unchecked")
+        CachedObject<T> obj = (CachedObject<T>) this.cache.get(id);
         if (obj == null)
         {
             // Not in the cache.
@@ -136,7 +135,7 @@ public class DefaultGlobalCacheService e
         {
             if (obj instanceof RefreshableCachedObject)
             {
-                RefreshableCachedObject rco = (RefreshableCachedObject) obj;
+                RefreshableCachedObject<?> rco = (RefreshableCachedObject<?>) obj;
                 if (rco.isUntouched())
                 {
                     throw new ObjectExpiredException();
@@ -157,7 +156,7 @@ public class DefaultGlobalCacheService e
         if (obj instanceof RefreshableCachedObject)
         {
             // notify it that it's being accessed.
-            RefreshableCachedObject rco = (RefreshableCachedObject) obj;
+            RefreshableCachedObject<?> rco = (RefreshableCachedObject<?>) obj;
             rco.touch();
         }
         return obj;
@@ -165,13 +164,14 @@ public class DefaultGlobalCacheService e
 
     /**
      * Adds an object to the cache.
-     * 
+     *
      * @param id
      *            The key to store the object by.
      * @param o
      *            The object to cache.
      */
-    public void addObject(String id, CachedObject o)
+    @Override
+    public <T> void addObject(String id, CachedObject<T> o)
     {
         // If the cache already contains the key, remove it and add
         // the fresh one.
@@ -184,10 +184,11 @@ public class DefaultGlobalCacheService e
 
     /**
      * Removes an object from the cache.
-     * 
+     *
      * @param id
      *            The String id for the object.
      */
+    @Override
     public void removeObject(String id)
     {
         this.cache.remove(id);
@@ -195,60 +196,54 @@ public class DefaultGlobalCacheService e
 
     /**
      * 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()
+    @Override
+    public List<String> getKeys()
     {
-        ArrayList keys = new ArrayList(this.cache.size());
-        synchronized (this)
+        ArrayList<String> keys = new ArrayList<String>(this.cache.size());
+        for (String key : this.cache.keySet())
         {
-            for (Iterator itr = this.cache.keySet().iterator(); itr.hasNext();)
+            try
             {
-                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));
+                /* CachedObject obj = */getObject(key);
             }
+            catch (ObjectExpiredException oee)
+            {
+                // this is OK we just do not want this key
+                continue;
+            }
+            keys.add(new String(key));
         }
         return 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()
+    @Override
+    public List<CachedObject<?>> getCachedObjects()
     {
-        ArrayList objects = new ArrayList(this.cache.size());
-        synchronized (this)
+        ArrayList<CachedObject<?>> objects = new ArrayList<CachedObject<?>>(this.cache.size());
+        for (String key : this.cache.keySet())
         {
-            for (Iterator itr = this.cache.keySet().iterator(); itr.hasNext();)
+            CachedObject<?> obj = null;
+            try
             {
-                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);
+                obj = getObject(key);
             }
+            catch (ObjectExpiredException oee)
+            {
+                // this is OK we just do not want this object
+                continue;
+            }
+            objects.add(obj);
         }
         return objects;
     }
@@ -257,21 +252,22 @@ public class DefaultGlobalCacheService e
      * Circle through the cache and remove stale objects. Frequency is
      * determined by the cacheCheckFrequency property.
      */
+    @Override
     public void run()
     {
         while (this.continueThread)
         {
             // Sleep for amount of time set in cacheCheckFrequency -
             // default = 5 seconds.
-            try
+            synchronized (this)
             {
-                Thread.sleep(this.cacheCheckFrequency);
-            }
-            catch (InterruptedException exc)
-            {
-                if (!this.continueThread)
+                try
                 {
-                    return;
+                    wait(this.cacheCheckFrequency);
+                }
+                catch (InterruptedException exc)
+                {
+                    // to be expected
                 }
             }
 
@@ -284,48 +280,45 @@ public class DefaultGlobalCacheService e
      */
     public void clearCache()
     {
-        List refreshThese = new ArrayList(20);
+        List<String> refreshThese = new ArrayList<String>(20);
         // Sync on this object so that other threads do not
         // change the Hashtable while enumerating over it.
-        synchronized (this)
+        for (String key : this.cache.keySet())
         {
-            for (Enumeration e = this.cache.keys(); e.hasMoreElements();)
+            CachedObject<?> co = this.cache.get(key);
+            if (co instanceof RefreshableCachedObject)
             {
-                String key = (String) e.nextElement();
-                CachedObject co = (CachedObject) this.cache.get(key);
-                if (co instanceof RefreshableCachedObject)
+                RefreshableCachedObject<?> rco = (RefreshableCachedObject<?>) co;
+                if (rco.isUntouched())
                 {
-                    RefreshableCachedObject rco = (RefreshableCachedObject) co;
-                    if (rco.isUntouched())
-                    {
-                        this.cache.remove(key);
-                    }
-                    else if (rco.isStale())
-                    {
-                        // to prolong holding the lock on this object
-                        refreshThese.add(key);
-                    }
+                    this.cache.remove(key);
                 }
-                else if (co.isStale())
+                else if (rco.isStale())
                 {
-                    this.cache.remove(key);
+                    // to prolong holding the lock on this object
+                    refreshThese.add(key);
                 }
             }
+            else if (co.isStale())
+            {
+                this.cache.remove(key);
+            }
         }
-        for (Iterator i = refreshThese.iterator(); i.hasNext();)
+
+        for (String key : refreshThese)
         {
-            String key = (String) i.next();
-            CachedObject co = (CachedObject) this.cache.get(key);
-            RefreshableCachedObject rco = (RefreshableCachedObject) co;
+            CachedObject<?> co = this.cache.get(key);
+            RefreshableCachedObject<?> rco = (RefreshableCachedObject<?>) co;
             rco.refresh();
         }
     }
 
     /**
      * Returns the number of objects currently stored in the cache
-     * 
+     *
      * @return int number of object in the cache
      */
+    @Override
     public int getNumberOfObjects()
     {
         return this.cache.size();
@@ -333,9 +326,10 @@ public class DefaultGlobalCacheService e
 
     /**
      * Returns the current size of the cache.
-     * 
+     *
      * @return int representing current cache size in number of bytes
      */
+    @Override
     public int getCacheSize() throws IOException
     {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -354,22 +348,17 @@ public class DefaultGlobalCacheService e
     /**
      * Flush the cache of all objects.
      */
+    @Override
     public void flushCache()
     {
-        synchronized (this)
-        {
-            for (Enumeration e = this.cache.keys(); e.hasMoreElements();)
-            {
-                String key = (String) e.nextElement();
-                this.cache.remove(key);
-            }
-        }
+        this.cache.clear();
     }
 
     // ---------------- Avalon Lifecycle Methods ---------------------
     /**
      * Avalon component lifecycle method
      */
+    @Override
     public void configure(Configuration conf) throws ConfigurationException
     {
         this.cacheCheckFrequency = conf.getAttributeAsLong(
@@ -381,44 +370,31 @@ public class DefaultGlobalCacheService e
     /**
      * Avalon component lifecycle method
      */
+    @Override
     public void initialize() throws Exception
     {
-        try
-        {
-            this.cache = new Hashtable(this.cacheInitialSize);
-            // Start housekeeping thread.
-            this.continueThread = true;
-            this.housekeeping = new Thread(this);
-            // Indicate that this is a system thread. JVM will quit only when
-            // there are no more active user threads. Settings threads spawned
-            // internally by Turbine as daemons allows commandline applications
-            // using Turbine to terminate in an orderly manner.
-            this.housekeeping.setDaemon(true);
-            this.housekeeping.start();
-        }
-        catch (Exception e)
-        {
-            throw new Exception(
-                    "DefaultGlobalCacheService failed to initialize", e);
-        }
+        this.cache = new ConcurrentHashMap<String, CachedObject<?>>(this.cacheInitialSize);
+        // Start housekeeping thread.
+        this.continueThread = true;
+        this.housekeeping = new Thread(this);
+        // Indicate that this is a system thread. JVM will quit only when
+        // there are no more active user threads. Settings threads spawned
+        // internally by Turbine as daemons allows commandline applications
+        // using Turbine to terminate in an orderly manner.
+        this.housekeeping.setDaemon(true);
+        this.housekeeping.start();
     }
 
     /**
      * Avalon component lifecycle method
      */
+    @Override
     public void dispose()
     {
-        this.continueThread = false;
-        this.housekeeping.interrupt();
-    }
-
-    /**
-     * The name used to specify this component in TurbineResources.properties
-     * 
-     * @deprecated part of the pre-avalon compatibility layer
-     */
-    protected String getName()
-    {
-        return "GlobalCacheService";
+        synchronized (this)
+        {
+            this.continueThread = false;
+            notify();
+        }
     }
 }

Modified: turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/EHCacheService.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/EHCacheService.java?rev=1754101&r1=1754100&r2=1754101&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/EHCacheService.java (original)
+++ turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/EHCacheService.java Tue Jul 26 10:23:30 2016
@@ -21,7 +21,6 @@ package org.apache.fulcrum.cache.impl;
 
 import java.io.IOException;
 import java.util.ArrayList;
-import java.util.Iterator;
 import java.util.List;
 
 import net.sf.ehcache.Cache;
@@ -42,10 +41,10 @@ import org.apache.fulcrum.cache.Refresha
 
 /**
  * Default implementation of EHCacheService
- * 
+ *
  * @author <a href="mailto:epughNOSPAM@opensourceconnections.com">Eric Pugh</a>
  * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
- * 
+ *
  */
 public class EHCacheService extends AbstractLogEnabled implements
         GlobalCacheService, Runnable, Configurable, Disposable, Initializable, ThreadSafe
@@ -93,6 +92,7 @@ public class EHCacheService extends Abst
     /**
      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
      */
+    @Override
     public void configure(Configuration config) throws ConfigurationException
     {
         this.cacheCheckFrequency = config.getChild("cacheCheckFrequency")
@@ -104,6 +104,7 @@ public class EHCacheService extends Abst
     /**
      * @see org.apache.avalon.framework.activity.Initializable#initialize()
      */
+    @Override
     public void initialize() throws Exception
     {
         if (this.configFile == null)
@@ -115,9 +116,9 @@ public class EHCacheService extends Abst
         {
             this.cacheManager = new CacheManager(this.configFile);
         }
-        
+
         this.cache = this.cacheManager.getCache(this.cacheName);
-        
+
         // Start housekeeping thread.
         this.continueThread = true;
         this.refreshing = new Thread(this);
@@ -129,13 +130,14 @@ public class EHCacheService extends Abst
         this.refreshing.setDaemon(true);
         this.refreshing.setName("EHCacheService Refreshing");
         this.refreshing.start();
-        
+
         getLogger().debug("EHCacheService started!");
     }
 
     /**
      * @see org.apache.avalon.framework.activity.Disposable#dispose()
      */
+    @Override
     public void dispose()
     {
         this.continueThread = false;
@@ -146,11 +148,12 @@ public class EHCacheService extends Abst
         this.cache = null;
         getLogger().debug("EHCacheService stopped!");
     }
-    
+
     /**
      * @see org.apache.fulcrum.cache.GlobalCacheService#addObject(java.lang.String, org.apache.fulcrum.cache.CachedObject)
      */
-    public void addObject(String id, CachedObject o)
+    @Override
+    public <T> void addObject(String id, CachedObject<T> o)
     {
         Element cacheElement = new Element(id, o);
 
@@ -164,14 +167,13 @@ public class EHCacheService extends Abst
             cacheElement.setTimeToLive((int)(o.getExpires() + 500) / 1000);
         }
 
-        cacheElement.setCreateTime();
-
         this.cache.put(cacheElement);
     }
 
     /**
      * @see org.apache.fulcrum.cache.GlobalCacheService#flushCache()
      */
+    @Override
     public void flushCache()
     {
         this.cache.removeAll();
@@ -180,17 +182,18 @@ public class EHCacheService extends Abst
     /**
      * @see org.apache.fulcrum.cache.GlobalCacheService#getCachedObjects()
      */
-    public List getCachedObjects()
+    @Override
+    public List<CachedObject<?>> getCachedObjects()
     {
-        ArrayList values = new ArrayList();
+        ArrayList<CachedObject<?>> values = new ArrayList<CachedObject<?>>();
 
-        for (Iterator i = getKeys().iterator(); i.hasNext();)
+        for (String key : getKeys())
         {
-            Element cachedElement = this.cache.get(i.next());
-            
+            Element cachedElement = this.cache.get(key);
+
             if (cachedElement != null)
             {
-                values.add(cachedElement.getObjectValue());
+                values.add((CachedObject<?>)cachedElement.getObjectValue());
             }
         }
 
@@ -200,6 +203,7 @@ public class EHCacheService extends Abst
     /**
      * @see org.apache.fulcrum.cache.GlobalCacheService#getCacheSize()
      */
+    @Override
     public int getCacheSize() throws IOException
     {
         return (int)this.cache.calculateInMemorySize();
@@ -208,14 +212,18 @@ public class EHCacheService extends Abst
     /**
      * @see org.apache.fulcrum.cache.GlobalCacheService#getKeys()
      */
-    public List getKeys()
+    @Override
+    public List<String> getKeys()
     {
-        return this.cache.getKeysWithExpiryCheck();
+        @SuppressWarnings("unchecked")
+        List<String> keysWithExpiryCheck = this.cache.getKeysWithExpiryCheck();
+        return keysWithExpiryCheck;
     }
 
     /**
      * @see org.apache.fulcrum.cache.GlobalCacheService#getNumberOfObjects()
      */
+    @Override
     public int getNumberOfObjects()
     {
         return getKeys().size();
@@ -224,23 +232,25 @@ public class EHCacheService extends Abst
     /**
      * @see org.apache.fulcrum.cache.GlobalCacheService#getObject(java.lang.String)
      */
-    public CachedObject getObject(String id) throws ObjectExpiredException
+    @Override
+    public <T> CachedObject<T> getObject(String id) throws ObjectExpiredException
     {
         Element cachedElement = this.cache.get(id);
-        
+
         if (cachedElement == null)
         {
             // Not in the cache.
             throw new ObjectExpiredException();
         }
 
-        CachedObject obj = (CachedObject)cachedElement.getObjectValue();
-        
+        @SuppressWarnings("unchecked")
+        CachedObject<T> obj = (CachedObject<T>)cachedElement.getObjectValue();
+
         if (obj.isStale())
         {
             if (obj instanceof RefreshableCachedObject)
             {
-                RefreshableCachedObject rco = (RefreshableCachedObject) obj;
+                RefreshableCachedObject<?> rco = (RefreshableCachedObject<?>) obj;
                 if (rco.isUntouched())
                 {
                     // Do not refresh an object that has exceeded TimeToLive
@@ -268,7 +278,7 @@ public class EHCacheService extends Abst
         if (obj instanceof RefreshableCachedObject)
         {
             // notify it that it's being accessed.
-            RefreshableCachedObject rco = (RefreshableCachedObject) obj;
+            RefreshableCachedObject<?> rco = (RefreshableCachedObject<?>) obj;
             rco.touch();
         }
 
@@ -278,6 +288,7 @@ public class EHCacheService extends Abst
     /**
      * @see org.apache.fulcrum.cache.GlobalCacheService#removeObject(java.lang.String)
      */
+    @Override
     public void removeObject(String id)
     {
         this.cache.remove(id);
@@ -287,6 +298,7 @@ public class EHCacheService extends Abst
      * Circle through the cache and refresh stale objects. Frequency is
      * determined by the cacheCheckFrequency property.
      */
+    @Override
     public void run()
     {
         while (this.continueThread)
@@ -305,23 +317,21 @@ public class EHCacheService extends Abst
                 }
             }
 
-            for (Iterator i = getKeys().iterator(); i.hasNext();)
+            for (String key : getKeys())
             {
-                String key = (String) i.next();
-                
                 Element cachedElement = this.cache.get(key);
-                
+
                 if (cachedElement == null)
                 {
                     this.cache.remove(key);
                     continue;
                 }
-                
+
                 Object o = cachedElement.getObjectValue();
-                
+
                 if (o instanceof RefreshableCachedObject)
                 {
-                    RefreshableCachedObject rco = (RefreshableCachedObject) o;
+                    RefreshableCachedObject<?> rco = (RefreshableCachedObject<?>) o;
                     if (rco.isUntouched())
                     {
                         this.cache.remove(key);

Modified: turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/CacheTest.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/CacheTest.java?rev=1754101&r1=1754100&r2=1754101&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/CacheTest.java (original)
+++ turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/CacheTest.java Tue Jul 26 10:23:30 2016
@@ -23,7 +23,6 @@ package org.apache.fulcrum.cache;
 
 import java.util.ConcurrentModificationException;
 import java.util.Date;
-import java.util.Iterator;
 import java.util.List;
 
 import org.apache.avalon.framework.component.ComponentException;
@@ -34,7 +33,7 @@ import org.apache.fulcrum.testcontainer.
 
 /**
  * CacheTest
- * 
+ *
  * @author <a href="paulsp@apache.org">Paul Spencer</a>
  * @author <a href="epugh@upstate.com">Eric Pugh</a>
  * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
@@ -55,7 +54,7 @@ public class CacheTest extends BaseUnitT
 
     /**
      * Defines the testcase name for JUnit.
-     * 
+     *
      * @param name
      *            the testcase's name.
      */
@@ -66,18 +65,19 @@ public class CacheTest extends BaseUnitT
 
     /**
      * Method to configure the role name of the service used
-     * 
+     *
      * @return the role name of the service to lookup
      */
     protected String getCacheRoleName()
     {
         return GlobalCacheService.ROLE;
     }
-    
+
+    @Override
     protected void setUp() throws Exception
     {
         super.setUp();
-        
+
         try
         {
             this.globalCache = (GlobalCacheService) this
@@ -92,16 +92,16 @@ public class CacheTest extends BaseUnitT
 
     /**
      * Simple test that verify an object can be created and deleted.
-     * 
+     *
      * @throws Exception
      */
     public void testSimpleAddGetCacheObject() throws Exception
     {
         String testString = "This is a test";
         Object retrievedObject = null;
-        CachedObject cacheObject1 = null;
+        CachedObject<String> cacheObject1 = null;
         // Create object
-        cacheObject1 = new CachedObject(testString);
+        cacheObject1 = new CachedObject<String>(testString);
         assertNotNull("Failed to create a cachable object 1", cacheObject1);
         // Add object to cache
         this.globalCache.addObject(cacheKey, cacheObject1);
@@ -137,17 +137,17 @@ public class CacheTest extends BaseUnitT
 
     /**
      * Simple test that adds, retrieves, and deletes 2 object.
-     * 
+     *
      * @throws Exception
      */
     public void test2ObjectAddGetCachedObject() throws Exception
     {
         String testString = "This is a test";
         Object retrievedObject = null;
-        CachedObject cacheObject1 = null;
-        CachedObject cacheObject2 = null;
+        CachedObject<String> cacheObject1 = null;
+        CachedObject<String> cacheObject2 = null;
         // Create and add Object #1
-        cacheObject1 = new CachedObject(testString);
+        cacheObject1 = new CachedObject<String>(testString);
         assertNotNull("Failed to create a cachable object 1", cacheObject1);
         this.globalCache.addObject(cacheKey, cacheObject1);
         retrievedObject = this.globalCache.getObject(cacheKey);
@@ -155,7 +155,7 @@ public class CacheTest extends BaseUnitT
         assertEquals("Did not retrieve correct cached object", cacheObject1,
                 retrievedObject);
         // Create and add Object #2
-        cacheObject2 = new CachedObject(testString);
+        cacheObject2 = new CachedObject<String>(testString);
         assertNotNull("Failed to create a cachable object 2", cacheObject2);
         this.globalCache.addObject(cacheKey_2, cacheObject2);
         retrievedObject = this.globalCache.getObject(cacheKey_2);
@@ -188,16 +188,16 @@ public class CacheTest extends BaseUnitT
     /**
      * Verify that an object will throw the ObjectExpiredException when it now
      * longer exists in cache.
-     * 
+     *
      * @throws Exception
      */
     public void testObjectExpiration() throws Exception
     {
         String testString = "This is a test";
         Object retrievedObject = null;
-        CachedObject cacheObject = null;
+        CachedObject<String> cacheObject = null;
         // Create and add Object that expires in 1000 millis (1 second)
-        cacheObject = new CachedObject(testString, 1000);
+        cacheObject = new CachedObject<String>(testString, 1000);
         assertNotNull("Failed to create a cachable object", cacheObject);
         long addTime = System.currentTimeMillis();
         this.globalCache.addObject(cacheKey, cacheObject);
@@ -241,17 +241,17 @@ public class CacheTest extends BaseUnitT
 
     /**
      * Verify the all object will be flushed from the cache.
-     * 
+     *
      * This test can take server minutes.
-     * 
+     *
      * @throws Exception
      */
     public void testCacheFlush() throws Exception
     {
         String testString = "This is a test";
-        CachedObject cacheObject = null;
+        CachedObject<String> cacheObject = null;
         // Create and add Object that expires in 1 turbine Refresh + 1 millis
-        cacheObject = new CachedObject(testString, (getCacheRefresh() * 5) + 1);
+        cacheObject = new CachedObject<String>(testString, (getCacheRefresh() * 5) + 1);
         assertNotNull("Failed to create a cachable object", cacheObject);
         this.globalCache.addObject(cacheKey, cacheObject);
         // 1 Refresh
@@ -269,31 +269,31 @@ public class CacheTest extends BaseUnitT
 
     /**
      * Verify the Cache count is correct.
-     * 
+     *
      * @throws Exception
      */
     public void testObjectCount() throws Exception
     {
         assertNotNull("Could not retrieve cache service.", this.globalCache);
-        
+
         long cacheRefresh = getCacheRefresh();
-        
+
         // Create and add Object that expires in 1.5 turbine Refresh
         long expireTime = cacheRefresh + cacheRefresh / 2;
-        
-        CachedObject cacheObject = new CachedObject("This is a test",
+
+        CachedObject<String> cacheObject = new CachedObject<String>("This is a test",
                 expireTime);
         assertNotNull("Failed to create a cachable object", cacheObject);
-        
+
         this.globalCache.addObject(cacheKey, cacheObject);
         assertEquals("After adding 1 Object", 1, this.globalCache
                 .getNumberOfObjects());
-        
+
         // Wait until we're passed 1 refresh, but not half way.
         Thread.sleep(cacheRefresh + cacheRefresh / 3);
         assertEquals("After one refresh", 1, this.globalCache
                 .getNumberOfObjects());
-        
+
         // Wait until we're passed 2 more refreshes
         Thread.sleep((cacheRefresh * 2) + cacheRefresh / 3);
         assertEquals("After three refreshes", 0, this.globalCache
@@ -301,20 +301,20 @@ public class CacheTest extends BaseUnitT
     }
 
     /**
-     * Verfy a refreshable object will refreshed in the following cases: o The
+     * Verify a refreshable object will refreshed in the following cases: o The
      * object is retrieved via getObject an it is stale. o The object is
-     * determied to be stale during a cache refresh
-     * 
-     * This test can take serveral minutes.
-     * 
+     * determined to be stale during a cache refresh
+     *
+     * This test can take several minutes.
+     *
      * @throws Exception
      */
     public void testRefreshableObject() throws Exception
     {
-        Object retrievedObject = null;
-        RefreshableCachedObject cacheObject = null;
+        CachedObject<RefreshableObject> retrievedObject = null;
+        RefreshableCachedObject<RefreshableObject> cacheObject = null;
         // Create and add Object that expires in TEST_EXPIRETIME millis.
-        cacheObject = new RefreshableCachedObject(new RefreshableObject(),
+        cacheObject = new RefreshableCachedObject<RefreshableObject>(new RefreshableObject(),
                 getTestExpireTime());
         assertNotNull("Failed to create a cachable object", cacheObject);
         long addTime = System.currentTimeMillis();
@@ -343,11 +343,11 @@ public class CacheTest extends BaseUnitT
             assertNotNull("Did not retrieve a cached object, after sleep",
                     retrievedObject);
             assertNotNull("Cached object has no contents, after sleep.",
-                    ((RefreshableCachedObject) retrievedObject).getContents());
+                    ((RefreshableCachedObject<?>) retrievedObject).getContents());
             assertTrue(
                     "Object did not refresh.",
-                    (((RefreshableObject) ((RefreshableCachedObject) retrievedObject)
-                            .getContents()).getRefreshCount() > 0));
+                    (((RefreshableCachedObject<RefreshableObject>) retrievedObject)
+                            .getContents().getRefreshCount() > 0));
         }
         catch (ObjectExpiredException e)
         {
@@ -357,7 +357,7 @@ public class CacheTest extends BaseUnitT
                     false);
         }
         // See if object will expires (testing every second for 100 seconds. It
-        // sould not!
+        // should not!
         for (int i = 0; i < 100; i++)
         {
             Thread.sleep(1000); // Sleep 0.5 seconds
@@ -369,12 +369,12 @@ public class CacheTest extends BaseUnitT
                 assertNotNull("Did not retrieve a cached object, after sleep",
                         retrievedObject);
                 assertNotNull("Cached object has no contents, after sleep.",
-                        ((RefreshableCachedObject) retrievedObject)
+                        ((RefreshableCachedObject<?>) retrievedObject)
                                 .getContents());
                 assertTrue(
                         "Object did not refresh.",
-                        (((RefreshableObject) ((RefreshableCachedObject) retrievedObject)
-                                .getContents()).getRefreshCount() > 0));
+                        (((RefreshableCachedObject<RefreshableObject>) retrievedObject)
+                                .getContents().getRefreshCount() > 0));
             }
             catch (ObjectExpiredException e)
             {
@@ -392,9 +392,9 @@ public class CacheTest extends BaseUnitT
     /**
      * Verify a cached object will be delete after it has been untouched beyond
      * it's TimeToLive.
-     * 
-     * This test can take serveral minutes.
-     * 
+     *
+     * This test can take several minutes.
+     *
      * @throws Exception
      */
     public void testRefreshableTimeToLive() throws Exception
@@ -414,10 +414,10 @@ public class CacheTest extends BaseUnitT
                     + SKIP_TESTS_KEY + " being false.");
         }
 
-        Object retrievedObject = null;
-        RefreshableCachedObject cacheObject = null;
+        CachedObject<RefreshableObject> retrievedObject = null;
+        RefreshableCachedObject<RefreshableObject>  cacheObject = null;
         // Create and add Object that expires in TEST_EXPIRETIME millis.
-        cacheObject = new RefreshableCachedObject(new RefreshableObject(),
+        cacheObject = new RefreshableCachedObject<RefreshableObject>(new RefreshableObject(),
                 getTestExpireTime());
         assertNotNull("Failed to create a cachable object", cacheObject);
         cacheObject.setTTL(getTestExpireTime());
@@ -451,10 +451,10 @@ public class CacheTest extends BaseUnitT
             assertNotNull("Did not retrieve a cached object, after sleep",
                     retrievedObject);
             assertNotNull("Cached object has no contents, after sleep.",
-                    ((RefreshableCachedObject) retrievedObject).getContents());
+                    ((RefreshableCachedObject<?>) retrievedObject).getContents());
             /*
              * @todo this is not working for some reason
-             * 
+             *
              * assertTrue( "Object did not refresh.", (((RefreshableObject)
              * ((RefreshableCachedObject)
              * retrievedObject).getContents()).getRefreshCount() > 0));
@@ -487,21 +487,20 @@ public class CacheTest extends BaseUnitT
 
     /**
      * Test that we can get a list of the keys in the cache
-     * 
+     *
      * @return
      */
     public void testCacheGetKeyList()
     {
         this.globalCache.flushCache();
-        this.globalCache.addObject("date1", new CachedObject(new Date()));
-        this.globalCache.addObject("date2", new CachedObject(new Date()));
-        this.globalCache.addObject("date3", new CachedObject(new Date()));
+        this.globalCache.addObject("date1", new CachedObject<Date>(new Date()));
+        this.globalCache.addObject("date2", new CachedObject<Date>(new Date()));
+        this.globalCache.addObject("date3", new CachedObject<Date>(new Date()));
         assertTrue("Did not get key list back.",
                 (this.globalCache.getKeys() != null));
-        List keys = this.globalCache.getKeys();
-        for (Iterator itr = keys.iterator(); itr.hasNext();)
+        List<String> keys = this.globalCache.getKeys();
+        for (String key : keys)
         {
-            Object key = itr.next();
             assertTrue("Key was not an instance of String.",
                     (key instanceof String));
         }
@@ -510,21 +509,20 @@ public class CacheTest extends BaseUnitT
 
     /**
      * Test that we can get a list of the keys in the cache
-     * 
+     *
      * @return
      */
     public void testCacheGetCachedObjects()
     {
         this.globalCache.flushCache();
-        this.globalCache.addObject("date1", new CachedObject(new Date()));
-        this.globalCache.addObject("date2", new CachedObject(new Date()));
-        this.globalCache.addObject("date3", new CachedObject(new Date()));
+        this.globalCache.addObject("date1", new CachedObject<Date>(new Date()));
+        this.globalCache.addObject("date2", new CachedObject<Date>(new Date()));
+        this.globalCache.addObject("date3", new CachedObject<Date>(new Date()));
         assertTrue("Did not get object list back.", (this.globalCache
                 .getCachedObjects() != null));
-        List objects = this.globalCache.getCachedObjects();
-        for (Iterator itr = objects.iterator(); itr.hasNext();)
+        List<CachedObject<?>> objects = this.globalCache.getCachedObjects();
+        for (CachedObject<?> obj : objects)
         {
-            Object obj = itr.next();
             assertNotNull("Object was null.", obj);
             assertTrue("Object was not an instance of CachedObject",
                     (obj instanceof CachedObject));
@@ -536,39 +534,37 @@ public class CacheTest extends BaseUnitT
      * 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()
     {
         this.globalCache.flushCache();
-        this.globalCache.addObject("date1", new CachedObject(new Date()));
-        this.globalCache.addObject("date2", new CachedObject(new Date()));
-        this.globalCache.addObject("date3", new CachedObject(new Date()));
+        this.globalCache.addObject("date1", new CachedObject<Date>(new Date()));
+        this.globalCache.addObject("date2", new CachedObject<Date>(new Date()));
+        this.globalCache.addObject("date3", new CachedObject<Date>(new Date()));
         assertTrue("Did not get key list back.",
                 (this.globalCache.getKeys() != null));
-        List keys = this.globalCache.getKeys();
+        List<String> keys = this.globalCache.getKeys();
         try
         {
-            for (Iterator itr = keys.iterator(); itr.hasNext();)
+            for (@SuppressWarnings("unused") String key : keys)
             {
-                Object key = itr.next();
                 this.globalCache.addObject("date4",
-                        new CachedObject(new Date()));
+                        new CachedObject<Date>(new Date()));
             }
         }
         catch (ConcurrentModificationException cme)
         {
             fail("Caught ConcurrentModificationException adding to cache.");
         }
-        List objects = this.globalCache.getCachedObjects();
+        List<CachedObject<?>> objects = this.globalCache.getCachedObjects();
         try
         {
-            for (Iterator itr = objects.iterator(); itr.hasNext();)
+            for (@SuppressWarnings("unused") CachedObject<?> obj : objects)
             {
-                Object obj = itr.next();
                 this.globalCache.addObject("date4",
-                        new CachedObject(new Date()));
+                        new CachedObject<Date>(new Date()));
             }
         }
         catch (ConcurrentModificationException cme)
@@ -578,16 +574,16 @@ public class CacheTest extends BaseUnitT
     }
 
     /**
-     * Down cast the interface to the concreate object in order to grab the
+     * Down cast the interface to the concrete object in order to grab the
      * cache check frequency.
-     * 
-     * @return the refresh requency in milliseconds
+     *
+     * @return the refresh frequency in milliseconds
      */
     private long getCacheRefresh()
     {
         try
         {
-            DefaultGlobalCacheService cache = 
+            DefaultGlobalCacheService cache =
                 (DefaultGlobalCacheService)this.lookup(GlobalCacheService.ROLE);
             return cache.getCacheCheckFrequency() * 1000L;
         }
@@ -599,7 +595,7 @@ public class CacheTest extends BaseUnitT
 
     /**
      * How long until it expires
-     * 
+     *
      * @return the cache refresh plus 1000.
      */
     private long getTestExpireTime()
@@ -617,14 +613,15 @@ public class CacheTest extends BaseUnitT
         /**
          * Increment the refresh counter
          */
+        @Override
         public void refresh()
         {
             this.refreshCount++;
         }
 
         /**
-         * Reutrn the number of time this object has been refreshed
-         * 
+         * Return the number of time this object has been refreshed
+         *
          * @return Number of times refresh() has been called
          */
         public int getRefreshCount()