[jboss-cvs] JBossAS SVN: r80632 - in projects/vfs/trunk/src: main/java/org/jboss/virtual/spi/cache and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Nov 7 05:35:20 EST 2008


Author: alesj
Date: 2008-11-07 05:35:20 -0500 (Fri, 07 Nov 2008)
New Revision: 80632

Modified:
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/cache/CachePolicyVFSCache.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/cache/LRUVFSCache.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/cache/TimedVFSCache.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/cache/VFSCacheFactory.java
   projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/LRUCacheTestCase.java
   projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/MapVFSCacheTest.java
   projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/TimedCacheTestCase.java
   projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/VFSCacheTest.java
Log:
[JBVFS-69]; allow Map parameter in vfs cache impl.

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/cache/CachePolicyVFSCache.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/cache/CachePolicyVFSCache.java	2008-11-07 10:08:50 UTC (rev 80631)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/cache/CachePolicyVFSCache.java	2008-11-07 10:35:20 UTC (rev 80632)
@@ -24,6 +24,7 @@
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.util.Collections;
+import java.util.Map;
 
 import org.jboss.util.CachePolicy;
 import org.jboss.virtual.spi.VFSContext;
@@ -36,7 +37,17 @@
 public abstract class CachePolicyVFSCache extends PathMatchingVFSCache
 {
    private CachePolicy policy;
+   private Map<Object, Object> properties;
 
+   protected CachePolicyVFSCache()
+   {
+   }
+
+   protected CachePolicyVFSCache(Map<Object, Object> properties)
+   {
+      this.properties = properties;
+   }
+
    public Iterable<VFSContext> getCachedContexts()
    {
       // cannot pull all cache entries from policy
@@ -134,6 +145,34 @@
    protected abstract CachePolicy createCachePolicy();
 
    /**
+    * Read instance properties.
+    *
+    * @param key the property key
+    * @param defaultValue the default value
+    * @param useSystemProperties do we fallback to system properties
+    * @return property or default value
+    */
+   protected Object readInstanceProperties(final String key, final Object defaultValue, final boolean useSystemProperties)
+   {
+      Object result = null;
+      if (properties != null && properties.isEmpty() == false)
+      {
+         result = properties.get(key);
+      }
+      if (result == null)
+      {
+         if (useSystemProperties)
+         {
+            String stringDefaultValue = defaultValue != null ? defaultValue.toString() : null;
+            result = readSystemProperty(key, stringDefaultValue);
+         }
+         else
+            result = defaultValue;
+      }
+      return result;
+   }
+
+   /**
     * Read system property.
     *
     * @param key          the property key
@@ -159,7 +198,7 @@
     * Parse integer.
     *
     * @param value the string int value
-    * @return integer value of null
+    * @return integer value or null
     */
    protected static Integer parseInteger(String value)
    {
@@ -168,4 +207,20 @@
 
       return Integer.parseInt(value);
    }
+
+   /**
+    * Get integer from value.
+    *
+    * @param value the value
+    * @return integer value or null
+    */
+   protected static Integer getInteger(Object value)
+   {
+      if (value == null)
+         return null;
+      else if (value instanceof Number)
+         return Number.class.cast(value).intValue();
+      else
+         return Integer.parseInt(value.toString());
+   }
 }
\ No newline at end of file

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/cache/LRUVFSCache.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/cache/LRUVFSCache.java	2008-11-07 10:08:50 UTC (rev 80631)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/cache/LRUVFSCache.java	2008-11-07 10:35:20 UTC (rev 80632)
@@ -21,6 +21,8 @@
 */
 package org.jboss.virtual.plugins.cache;
 
+import java.util.Map;
+
 import org.jboss.util.CachePolicy;
 import org.jboss.util.LRUCachePolicy;
 import org.jboss.virtual.VFSUtils;
@@ -45,12 +47,17 @@
       this.max = max;
    }
 
+   public LRUVFSCache(Map<Object, Object> properties)
+   {
+      super(properties);
+   }
+
    protected CachePolicy createCachePolicy()
    {
       if (min == null)
-         min = parseInteger(readSystemProperty(VFSUtils.VFS_CACHE_KEY + ".LRUPolicyCaching.min", null));
+         min = getInteger(readInstanceProperties(VFSUtils.VFS_CACHE_KEY + ".LRUPolicyCaching.min", null, true));
       if (max == null)
-         max = parseInteger(readSystemProperty(VFSUtils.VFS_CACHE_KEY + ".LRUPolicyCaching.max", null));
+         max = getInteger(readInstanceProperties(VFSUtils.VFS_CACHE_KEY + ".LRUPolicyCaching.max", null, true));
 
       if (min == null || max == null)
          throw new IllegalArgumentException("Missing min (" + min + ") or max (" + max + ").");

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/cache/TimedVFSCache.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/cache/TimedVFSCache.java	2008-11-07 10:08:50 UTC (rev 80631)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/cache/TimedVFSCache.java	2008-11-07 10:35:20 UTC (rev 80632)
@@ -21,6 +21,8 @@
 */
 package org.jboss.virtual.plugins.cache;
 
+import java.util.Map;
+
 import org.jboss.util.CachePolicy;
 import org.jboss.util.TimedCachePolicy;
 import org.jboss.virtual.VFSUtils;
@@ -52,14 +54,19 @@
       this.resolution = resolution;
    }
 
+   public TimedVFSCache(Map<Object, Object> properties)
+   {
+      super(properties);
+   }
+
    protected CachePolicy createCachePolicy()
    {
       if (defaultLifetime == null)
-         defaultLifetime = parseInteger(readSystemProperty(VFSUtils.VFS_CACHE_KEY + ".TimedPolicyCaching.lifetime", null));
+         defaultLifetime = getInteger(readInstanceProperties(VFSUtils.VFS_CACHE_KEY + ".TimedPolicyCaching.lifetime", null, true));
       if (threadSafe == null)
-         threadSafe = Boolean.valueOf(readSystemProperty(VFSUtils.VFS_CACHE_KEY + ".TimedPolicyCaching.threadSafe", Boolean.TRUE.toString()));
+         threadSafe = Boolean.valueOf(readInstanceProperties(VFSUtils.VFS_CACHE_KEY + ".TimedPolicyCaching.threadSafe", Boolean.TRUE, true).toString());
       if (resolution == null)
-         resolution = parseInteger(readSystemProperty(VFSUtils.VFS_CACHE_KEY + ".TimedPolicyCaching.resolution", null));
+         resolution = getInteger(readInstanceProperties(VFSUtils.VFS_CACHE_KEY + ".TimedPolicyCaching.resolution", null, true));
 
       log.debug("Creating timed cache policy, lifetime: " + defaultLifetime + ", threadSafe: " + threadSafe + ", resolution: " + resolution);
 

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/cache/VFSCacheFactory.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/cache/VFSCacheFactory.java	2008-11-07 10:08:50 UTC (rev 80631)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/cache/VFSCacheFactory.java	2008-11-07 10:35:20 UTC (rev 80632)
@@ -21,8 +21,10 @@
 */
 package org.jboss.virtual.spi.cache;
 
+import java.lang.reflect.Constructor;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
+import java.util.Map;
 
 import org.jboss.logging.Logger;
 import org.jboss.virtual.VFSUtils;
@@ -53,8 +55,9 @@
     */
    public static VFSCache getInstance()
    {
-      return getInstance(null);
+      return getInstance(null, null);
    }
+
    /**
     * 
     * Get VFS cache instance.
@@ -62,17 +65,33 @@
     * @param defaultCacheImpl - the possibly null name of the VFSCache
     * implementation to use. If null, the {@linkplain VFSUtils.VFS_CACHE_KEY}
     * system property will be used.
-    * 
+    *
     * @return the vfs cache instance
     */
    public static VFSCache getInstance(String defaultCacheImpl)
    {
+      return getInstance(defaultCacheImpl, null);
+   }
+
+   /**
+    *
+    * Get VFS cache instance.
+    *
+    * @param defaultCacheImpl - the possibly null name of the VFSCache
+    * implementation to use. If null, the {@linkplain VFSUtils.VFS_CACHE_KEY}
+    * system property will be used.
+    * @param properties the possible vfs cache impl properties
+    *
+    * @return the vfs cache instance
+    */
+   public static VFSCache getInstance(String defaultCacheImpl, Map<Object, Object> properties)
+   {
       if (instance == null)
       {
          synchronized (lock)
          {
             if (instance == null)
-               instance = AccessController.doPrivileged(new VFSCacheCreatorAction(defaultCacheImpl));
+               instance = AccessController.doPrivileged(new VFSCacheCreatorAction(defaultCacheImpl, properties));
          }
       }
       return instance;
@@ -97,9 +116,12 @@
    private static class VFSCacheCreatorAction implements PrivilegedAction<VFSCache>
    {
       private String defaultCacheImpl;
-      VFSCacheCreatorAction(String defaultCacheImpl)
+      private Map<Object, Object> properties;
+
+      VFSCacheCreatorAction(String defaultCacheImpl, Map<Object, Object> properties)
       {
          this.defaultCacheImpl = defaultCacheImpl;
+         this.properties = properties;
       }
 
       public VFSCache run()
@@ -115,12 +137,22 @@
             }
             if (className != null)
             {
-               log.info("Initializing VFSCache [" + className + "] ...");
+               log.info("Initializing VFSCache [" + className + "]");
                ClassLoader cl = VFSCacheFactory.class.getClassLoader();
                Class<?> clazz = cl.loadClass(className);
-               VFSCache cache = VFSCache.class.cast(clazz.newInstance());
+               Object result;
+               if (properties != null)
+               {
+                  Constructor<?> constructor = clazz.getConstructor(Map.class);
+                  result = constructor.newInstance(properties);
+               }
+               else
+               {
+                  result = clazz.newInstance();
+               }
+               VFSCache cache = VFSCache.class.cast(result);
                cache.start(); // start here, so we fall back to default no-op in case start fails
-               log.info("Using VFSCache [" + cache + "] ...");
+               log.info("Using VFSCache [" + cache + "]");
                return cache;
             }
          }
@@ -128,7 +160,7 @@
          {
             log.warn("Exception instantiating VFS cache: ", t);
          }
-         log.info("Using VFSCache [NoopVFSCache] ...");
+         log.info("Using VFSCache [NoopVFSCache]");
          return new NoopVFSCache();
       }
    }

Modified: projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/LRUCacheTestCase.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/LRUCacheTestCase.java	2008-11-07 10:08:50 UTC (rev 80631)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/LRUCacheTestCase.java	2008-11-07 10:35:20 UTC (rev 80632)
@@ -21,7 +21,12 @@
 */
 package org.jboss.test.virtual.test;
 
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+
 import junit.framework.Test;
+import org.jboss.virtual.VFSUtils;
 import org.jboss.virtual.plugins.cache.LRUVFSCache;
 import org.jboss.virtual.spi.cache.VFSCache;
 
@@ -46,4 +51,19 @@
    {
       return new LRUVFSCache(2, 10);
    }
+
+   protected Iterable<String> populateRequiredSystemProperties()
+   {
+      System.setProperty(VFSUtils.VFS_CACHE_KEY + ".LRUPolicyCaching.min", "2");
+      System.setProperty(VFSUtils.VFS_CACHE_KEY + ".LRUPolicyCaching.max", "10");      
+      return Arrays.asList(VFSUtils.VFS_CACHE_KEY + ".LRUPolicyCaching.min", VFSUtils.VFS_CACHE_KEY + ".LRUPolicyCaching.max");
+   }
+
+   protected Map<Object, Object> getMap()
+   {
+      Map<Object, Object> map = new HashMap<Object, Object>();
+      map.put(VFSUtils.VFS_CACHE_KEY + ".LRUPolicyCaching.min", 2);
+      map.put(VFSUtils.VFS_CACHE_KEY + ".LRUPolicyCaching.max", 10);
+      return map;
+   }
 }
\ No newline at end of file

Modified: projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/MapVFSCacheTest.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/MapVFSCacheTest.java	2008-11-07 10:08:50 UTC (rev 80631)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/MapVFSCacheTest.java	2008-11-07 10:35:20 UTC (rev 80632)
@@ -21,6 +21,8 @@
 */
 package org.jboss.test.virtual.test;
 
+import java.util.Map;
+
 import org.jboss.virtual.spi.VFSContext;
 
 /**
@@ -35,6 +37,11 @@
       super(name);
    }
 
+   protected Map<Object, Object> getMap()
+   {
+      return null;
+   }
+
    protected void testCachedContexts(Iterable<VFSContext> iter)
    {
       VFSContext context = iter.iterator().next();

Modified: projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/TimedCacheTestCase.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/TimedCacheTestCase.java	2008-11-07 10:08:50 UTC (rev 80631)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/TimedCacheTestCase.java	2008-11-07 10:35:20 UTC (rev 80632)
@@ -21,8 +21,12 @@
 */
 package org.jboss.test.virtual.test;
 
+import java.util.Map;
+import java.util.HashMap;
+
 import org.jboss.virtual.plugins.cache.TimedVFSCache;
 import org.jboss.virtual.spi.cache.VFSCache;
+import org.jboss.virtual.VFSUtils;
 import junit.framework.Test;
 
 /**
@@ -44,6 +48,13 @@
 
    protected VFSCache createCache()
    {
-      return new TimedVFSCache(60);
+      return new TimedVFSCache(5);
    }
+
+   protected Map<Object, Object> getMap()
+   {
+      Map<Object, Object> map = new HashMap<Object, Object>();
+      map.put(VFSUtils.VFS_CACHE_KEY + ".TimedPolicyCaching.lifetime", 5);
+      return map;
+   }
 }
\ No newline at end of file

Modified: projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/VFSCacheTest.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/VFSCacheTest.java	2008-11-07 10:08:50 UTC (rev 80631)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/VFSCacheTest.java	2008-11-07 10:35:20 UTC (rev 80632)
@@ -24,13 +24,16 @@
 import java.io.IOException;
 import java.net.URI;
 import java.net.URL;
+import java.util.Collections;
+import java.util.Map;
 
 import org.jboss.virtual.VFS;
 import org.jboss.virtual.VirtualFile;
+import org.jboss.virtual.spi.VFSContext;
+import org.jboss.virtual.spi.cache.CacheStatistics;
 import org.jboss.virtual.spi.cache.VFSCache;
 import org.jboss.virtual.spi.cache.VFSCacheFactory;
-import org.jboss.virtual.spi.cache.CacheStatistics;
-import org.jboss.virtual.spi.VFSContext;
+import org.jboss.virtual.spi.cache.helpers.NoopVFSCache;
 
 /**
  * VFSCache Test.
@@ -123,6 +126,54 @@
       }
    }
 
+   protected Class<? extends VFSCache> getCacheClass()
+   {
+      VFSCache cache = createCache();
+      return cache.getClass();
+   }
+
+   protected abstract Map<Object, Object> getMap();
+
+   public void testCacheFactory() throws Exception
+   {
+      VFSCache cache;
+      String cacheClassName = getCacheClass().getName();
+
+      VFSCacheFactory.setInstance(null);
+      try
+      {
+         Iterable<String> keys = populateRequiredSystemProperties();
+         try
+         {
+            cache = VFSCacheFactory.getInstance(cacheClassName);
+            assertNotNull(cache);
+            assertTrue(cache instanceof NoopVFSCache == false);
+            cache.flush();
+         }
+         finally
+         {
+            for (String key : keys)
+               System.clearProperty(key);
+         }
+
+         VFSCacheFactory.setInstance(null);
+
+         cache = VFSCacheFactory.getInstance(cacheClassName, getMap());
+         assertNotNull(cache);
+         assertTrue(cache instanceof NoopVFSCache == false);
+         cache.flush();
+      }
+      finally
+      {
+         VFSCacheFactory.setInstance(null);
+      }
+   }
+
+   protected Iterable<String> populateRequiredSystemProperties()
+   {
+      return Collections.emptySet();
+   }
+
    private class WrapperVFSCache implements VFSCache
    {
       private VFSCache delegate;




More information about the jboss-cvs-commits mailing list