[jboss-cvs] JBossAS SVN: r112480 - projects/jboss-cl/branches/2.0.9.GA_JBPAPP-7576/classloader/src/main/java/org/jboss/classloader/spi/base.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Nov 28 10:30:45 EST 2011


Author: aogburn
Date: 2011-11-28 10:30:44 -0500 (Mon, 28 Nov 2011)
New Revision: 112480

Modified:
   projects/jboss-cl/branches/2.0.9.GA_JBPAPP-7576/classloader/src/main/java/org/jboss/classloader/spi/base/BaseClassLoader.java
Log:
[JBPAPP-7576] committing changes to BaseClassLoader.java to implement caching for getResourcesLocally()

Modified: projects/jboss-cl/branches/2.0.9.GA_JBPAPP-7576/classloader/src/main/java/org/jboss/classloader/spi/base/BaseClassLoader.java
===================================================================
--- projects/jboss-cl/branches/2.0.9.GA_JBPAPP-7576/classloader/src/main/java/org/jboss/classloader/spi/base/BaseClassLoader.java	2011-11-28 14:16:26 UTC (rev 112479)
+++ projects/jboss-cl/branches/2.0.9.GA_JBPAPP-7576/classloader/src/main/java/org/jboss/classloader/spi/base/BaseClassLoader.java	2011-11-28 15:30:44 UTC (rev 112480)
@@ -34,7 +34,9 @@
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.HashSet;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -42,7 +44,6 @@
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.locks.ReentrantLock;
-
 import javax.management.ObjectName;
 
 import org.jboss.classloader.plugins.ClassLoaderUtils;
@@ -62,6 +63,7 @@
  * BaseClassLoader.
  *
  * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @author <a href="ales.justin at jboss.org">Ales Justin</a>
  * @version $Revision: 1.1 $
  */
 public class BaseClassLoader extends SecureClassLoader implements BaseClassLoaderMBean, RealClassLoader
@@ -84,6 +86,9 @@
    /** Our resource cache */
    private Map<String, URL> resourceCache;
 
+   /** Our resources cache */
+   private Map<String, Set<URL>> resourcesCache;
+
    /** Our black list */
    private Set<String> blackList;
 
@@ -107,7 +112,10 @@
       loader = new DelegateLoader(policy);
 
       if (basePolicy.isCacheable())
+      {
          resourceCache = new ConcurrentHashMap<String, URL>();
+         resourcesCache = new ConcurrentHashMap<String, Set<URL>>();
+      }
 
       if (basePolicy.isBlackListable())
          blackList = new ConcurrentSet<String>();
@@ -211,18 +219,35 @@
 
    public Set<String> listLoadedResourceNames()
    {
-      if (resourceCache == null)
-         return Collections.emptySet();
-      return new HashSet<String>(resourceCache.keySet());
+      Set<String> names = new HashSet<String>();
+
+      Map<String, URL> resourceCache = this.resourceCache;
+      if (resourceCache != null)
+         names.addAll(resourceCache.keySet());
+
+      Map<String, Set<URL>> resourcesCache = this.resourcesCache;
+      if (resourcesCache != null)
+         names.addAll(resourcesCache.keySet());
+
+      return names;
    }
 
    // FindBugs: The Set doesn't use equals/hashCode
    public Set<URL> listLoadedResources()
    {
-      if (resourceCache == null)
-         return Collections.emptySet();
       Set<URL> result = new TreeSet<URL>(ClassLoaderUtils.URLComparator.INSTANCE);
-      result.addAll(resourceCache.values());
+
+      Map<String, URL> resourceCache = this.resourceCache;
+      if (resourceCache != null)
+         result.addAll(resourceCache.values());
+
+      Map<String, Set<URL>> resourcesCache = this.resourcesCache;
+      if (resourcesCache != null)
+      {
+         for (Set<URL> urls : resourcesCache.values())
+            result.addAll(urls);
+      }
+
       return result;
    }
 
@@ -680,6 +705,18 @@
          }
       }
 
+      // Do we already know the answer?
+      if (resourcesCache != null)
+      {
+         Set<URL> urls = resourcesCache.get(name);
+         if (urls != null)
+         {
+            if (trace)
+               log.trace(this + " got resource from resources cache " + name);
+            return (urls.isEmpty()) ? null : urls.iterator().next();
+         }
+      }
+
       // Is this resource blacklisted?
       if (blackList != null && blackList.contains(name))
       {
@@ -745,6 +782,26 @@
       if (trace)
          log.trace(this + " get resources locally " + name);
 
+      if (resourcesCache != null)
+      {
+         Set<URL> cached = resourcesCache.get(name);
+         if (cached != null)
+         {
+            if (trace)
+               log.trace(this + " got resources from cache " + name);
+            urls.addAll(cached);
+            return;
+         }
+      }
+
+      // Is this resource blacklisted?
+      if (blackList != null && blackList.contains(name))
+      {
+         if (trace)
+            log.trace(this + " resource is blacklisted " + name);
+         return;
+      }
+
       // Look for the resources
       try
       {
@@ -752,7 +809,23 @@
          {
             public Object run() throws Exception
             {
-               policy.getResources(name, urls);
+               final Set<URL> tmp = new LinkedHashSet<URL>();
+               policy.getResources(name, tmp);
+
+               if (tmp.isEmpty())
+               {
+                  if (resourcesCache != null)
+                     resourcesCache.put(name, Collections.<URL>emptySet());
+                  if (blackList != null)
+                     blackList.add(name);
+               }
+               else
+               {
+                  urls.addAll(tmp);
+                  if (resourcesCache != null)
+                     resourcesCache.put(name, tmp);
+               }
+
                return null;
             }
          }, policy.getAccessControlContext());
@@ -913,9 +986,14 @@
    public int getResourceCacheSize()
    {
       Map<String, URL> resourceCache = this.resourceCache;
-      if (resourceCache == null)
-         return 0;
-      return resourceCache.size();
+      Map<String, Set<URL>> resourcesCache = this.resourcesCache;
+
+      int size = 0;
+      if (resourceCache != null)
+         size += resourceCache.size();
+      if (resourcesCache != null)
+         size += resourcesCache.size();
+      return size;
    }
 
    public Set<String> listResourceBlackList()
@@ -928,10 +1006,25 @@
 
    public Map<String, URL> listResourceCache()
    {
+      Map<String, URL> map = new HashMap<String, URL>();
+
       Map<String, URL> resourceCache = this.resourceCache;
-      if (resourceCache == null)
-         return Collections.emptyMap();
-      return Collections.unmodifiableMap(resourceCache);
+      if (resourceCache != null)
+         map.putAll(resourceCache);
+
+      Map<String, Set<URL>> resourcesCache = this.resourcesCache;
+      if (resourcesCache != null)
+      {
+         for (Map.Entry<String, Set<URL>> entry : resourcesCache.entrySet())
+         {
+            if (map.containsKey(entry.getKey()) == false && entry.getValue().isEmpty() == false)
+            {
+               map.put(entry.getKey(), entry.getValue().iterator().next());
+            }
+         }
+      }
+
+      return map;
    }
 
    public void clearBlackList()
@@ -980,6 +1073,8 @@
       loadedClasses.clear();
       if (resourceCache != null)
          resourceCache.clear();
+      if (resourcesCache != null)
+         resourcesCache.clear();
       if (blackList != null)
          blackList.clear();
    }


Property changes on: projects/jboss-cl/branches/2.0.9.GA_JBPAPP-7576/classloader/src/main/java/org/jboss/classloader/spi/base/BaseClassLoader.java
___________________________________________________________________
Added: svn:mergeinfo
   + /projects/jboss-cl/branches/Branch_2_0/classloader/src/main/java/org/jboss/classloader/spi/base/BaseClassLoader.java:112466



More information about the jboss-cvs-commits mailing list